gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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 。

最后此篇关于Keras 中Leaky ReLU等高级激活函数的用法的文章就讲到这里了,如果你想了解更多关于Keras 中Leaky ReLU等高级激活函数的用法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com