1. 程式人生 > 程式設計 >Keras 中Leaky ReLU等高階啟用函式的用法

Keras 中Leaky ReLU等高階啟用函式的用法

在用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,activation='relu')) 
model.add(layers.MaxPooling2D((2,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,1)))
model.add(LeakyReLU(alpha=0.05))
model.add(layers.MaxPooling2D((2,2))) 
 
model.add(layers.Conv2D(64,3))) 
model.add(LeakyReLU(alpha=0.05))
model.add(layers.MaxPooling2D((2,2)))
 
model.add(layers.Conv2D(64,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函式實現的:

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等高階啟用函式的用法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。