溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Keras中Leaky ReLU等高級(jí)激活函數(shù)怎么用

發(fā)布時(shí)間:2020-07-06 11:30:58 來(lái)源:億速云 閱讀:1047 作者:清晨 欄目:開發(fā)技術(shù)

這篇文章主要介紹Keras中Leaky ReLU等高級(jí)激活函數(shù)怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

在用Keras來(lái)實(shí)現(xiàn)CNN等一系列網(wǎng)絡(luò)時(shí),我們經(jīng)常用ReLU作為激活函數(shù),一般寫法如下:

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'))

上面這段代碼實(shí)現(xiàn)了一個(gè)基本的卷積神經(jīng)網(wǎng)絡(luò),用ReLU作為激活函數(shù),關(guān)于ReLU具體內(nèi)容不做詳細(xì)介紹。還有一些常用的主流激活函數(shù):

softmax: 在多分類中常用的激活函數(shù),是基于邏輯回歸的。

Softplus:softplus(x)=log(1+e^x),近似生物神經(jīng)激活函數(shù),最近出現(xiàn)的。

Relu:近似生物神經(jīng)激活函數(shù),最近出現(xiàn)的。

tanh:雙曲正切激活函數(shù),也是很常用的。

sigmoid:S型曲線激活函數(shù),最常用的。

hard_sigmoid:基于S型激活函數(shù)。

linear:線性激活函數(shù),最簡(jiǎn)單的。

主流的激活函數(shù)可以如上述例子一樣通過名稱直接使用,但是還有一些復(fù)雜的激活函數(shù)如:Leaky ReLU、PReLU是不可以這樣直接使用的,必須使用add方法將高級(jí)激活函數(shù)作為層(layer)來(lái)使用,舉例如下:

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))

這里我們?cè)诰矸e層中去掉激活函數(shù)的參數(shù),并在卷積層后加入高級(jí)激活層,下面來(lái)測(cè)試:

>>model.summary()

Keras中Leaky ReLU等高級(jí)激活函數(shù)怎么用

這里從整個(gè)網(wǎng)絡(luò)結(jié)構(gòu)的結(jié)果可以看出,卷積層后確實(shí)加入了一層新的激活層,使用的是LeakyReLU函數(shù)。

補(bǔ)充知識(shí):Keras 調(diào)用leaky_relu

Keras 中有l(wèi)eaky_relu的實(shí)現(xiàn)。leaky_relu被整合進(jìn)了relu函數(shù)。

參考官方文檔:

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(超參數(shù))值控制負(fù)數(shù)部分線性函數(shù)的梯度。當(dāng)alpha = 0 ,是原始的relu函數(shù)。當(dāng)alpha >0,即為leaky_relu。

查看源碼,在Keras.backbend 中,也是調(diào)用tensorflow.python.ops庫(kù)nn中的leaky_relu函數(shù)實(shí)現(xiàn)的:

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)  ##在這里調(diào)用了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等高級(jí)激活函數(shù)怎么用的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI