亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Keras 中Leaky ReLU等高級激活函數(shù)的用法

 更新時間:2020年07月05日 08:50:36   作者:Forizon  
這篇文章主要介紹了Keras 中Leaky ReLU等高級激活函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在用Keras來實現(xiàn)CNN等一系列網(wǎng)絡時,我們經(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'))

上面這段代碼實現(xiàn)了一個基本的卷積神經(jīng)網(wǎng)絡,用ReLU作為激活函數(shù),關于ReLU具體內容不做詳細介紹。還有一些常用的主流激活函數(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ù),最簡單的。

主流的激活函數(shù)可以如上述例子一樣通過名稱直接使用,但是還有一些復雜的激活函數(shù)如:Leaky ReLU、PReLU是不可以這樣直接使用的,必須使用add方法將高級激活函數(shù)作為層(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))

這里我們在卷積層中去掉激活函數(shù)的參數(shù),并在卷積層后加入高級激活層,下面來測試:

>>model.summary()

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

補充知識:Keras 調用leaky_relu

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

參考官方文檔:

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

查看源碼,在Keras.backbend 中,也是調用tensorflow.python.ops庫nn中的leaky_relu函數(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)  ##在這里調用了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等高級激活函數(shù)的用法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論