溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Keras中Leaky ReLU等高級激活函數怎么用

發布時間:2020-07-06 11:30:58 來源:億速云 閱讀:1092 作者:清晨 欄目:開發技術

這篇文章主要介紹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, (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?hl=en

Arguments
xA tensor or variable.
alphaA scalar, slope of negative section (default=0.).
max_valuefloat. Saturation threshold.
thresholdfloat. 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等高級激活函數怎么用的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女