脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2024/5/20 浏览:3 次 )

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

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)来使用,举例如下:

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"htmlcode">

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等高级激活函数的用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:完美解决TensorFlow和Keras大数据量内存溢出的问题
下一篇:Keras 在fit_generator训练方式中加入图像random_crop操作
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网