脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Keras 中Leaky ReLU等高级激活函数的用法

Keras 中Leaky ReLU等高级激活函数的用法

2020-07-05 11:41Forizon Python

这篇文章主要介绍了Keras 中Leaky ReLU等高级激活函数的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在用Keras来实现CNN等一系列网络时,我们经常用ReLU作为激活函数,一般写法如下:

?
1
2
3
4
5
6
7
8
9
from keras import layers
from keras import models
 
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

上面这段代码实现了一个基本的卷积神经网络,用ReLU作为激活函数,关于ReLU具体内容不做详细介绍。还有一些常用的主流激活函数:

softmax: 在多分类中常用的激活函数,是基于逻辑回归的。

Softplus:softplus(x)=log(1+e^x),近似生物神经激活函数,最近出现的。

Relu:近似生物神经激活函数,最近出现的。

tanh:双曲正切激活函数,也是很常用的。

sigmoid:S型曲线激活函数,最常用的。

hard_sigmoid:基于S型激活函数。

linear:线性激活函数,最简单的。

主流的激活函数可以如上述例子一样通过名称直接使用,但是还有一些复杂的激活函数如:Leaky ReLU、PReLU是不可以这样直接使用的,必须使用add方法将高级激活函数作为层(layer)来使用,举例如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from keras import layers
from keras import models
from keras.layers import LeakyReLU
 
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
model.add(LeakyReLU(alpha=0.05))
model.add(layers.MaxPooling2D((2, 2)))
 
model.add(layers.Conv2D(64, (3, 3)))
model.add(LeakyReLU(alpha=0.05))
model.add(layers.MaxPooling2D((2, 2)))
 
model.add(layers.Conv2D(64, (3, 3))
model.add(LeakyReLU(alpha=0.05))

这里我们在卷积层中去掉激活函数的参数,并在卷积层后加入高级激活层,下面来测试:

>>model.summary()

Keras 中Leaky ReLU等高级激活函数的用法

这里从整个网络结构的结果可以看出,卷积层后确实加入了一层新的激活层,使用的是LeakyReLU函数。

补充知识:Keras 调用leaky_relu

Keras 中有leaky_relu的实现。leaky_relu被整合进了relu函数。

参考官方文档:

https://tensorflow.google.cn/api_docs/python/tf/keras/backend/relu?hl=en

Arguments  
x A tensor or variable.
alpha A scalar, slope of negative section (default=0.).
max_value float. Saturation threshold.
threshold float. Threshold value for thresholded activation.

alpha(超参数)值控制负数部分线性函数的梯度。当alpha = 0 ,是原始的relu函数。当alpha >0,即为leaky_relu。

查看源码,在Keras.backbend 中,也是调用tensorflow.python.ops库nn中的leaky_relu函数实现的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def relu(x, alpha=0., max_value=None, threshold=0):
 """Rectified linear unit.
 With default values, it returns element-wise `max(x, 0)`.
 Otherwise, it follows:
 `f(x) = max_value` for `x >= max_value`,
 `f(x) = x` for `threshold <= x < max_value`,
 `f(x) = alpha * (x - threshold)` otherwise.
 Arguments:
   x: A tensor or variable.
   alpha: A scalar, slope of negative section (default=`0.`).
   max_value: float. Saturation threshold.
   threshold: float. Threshold value for thresholded activation.
 Returns:
   A tensor.
 """
 
 if alpha != 0.:
  if max_value is None and threshold == 0:
   return nn.leaky_relu(x, alpha=alpha)  ##在这里调用了leaky_relu
 
  if threshold != 0:
   negative_part = nn.relu(-x + threshold)
  else:
   negative_part = nn.relu(-x)
 
 clip_max = max_value is not None
 
 if threshold != 0:
  # computes x for x > threshold else 0
  x = x * math_ops.cast(math_ops.greater(x, threshold), floatx())
 elif max_value == 6:
  # if no threshold, then can use nn.relu6 native TF op for performance
  x = nn.relu6(x)
  clip_max = False
 else:
  x = nn.relu(x)
 
 if clip_max:
  max_value = _constant_to_tensor(max_value, x.dtype.base_dtype)
  zero = _constant_to_tensor(0, x.dtype.base_dtype)
  x = clip_ops.clip_by_value(x, zero, max_value)
 
 if alpha != 0.:
  alpha = _to_tensor(alpha, x.dtype.base_dtype)
  x -= alpha * negative_part
 return x

以上这篇Keras 中Leaky ReLU等高级激活函数的用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/hesongzefairy/article/details/86707352

延伸 · 阅读

精彩推荐