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ù)的用法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python3實現(xiàn)的騰訊微博自動發(fā)帖小工具
這篇文章主要為大家分享下騰訊微博自動發(fā)帖的Python3代碼,需要的朋友可以參考下2013-11-11python數(shù)據(jù)預處理 :樣本分布不均的解決(過采樣和欠采樣)
今天小編就為大家分享一篇python數(shù)據(jù)預處理 :樣本分布不均的解決(過采樣和欠采樣),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python函數(shù)也可以是一個對象,可以存放在列表中并調用方式
這篇文章主要介紹了python函數(shù)也可以是一個對象,可以存放在列表中并調用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02python實現(xiàn)銀行實戰(zhàn)系統(tǒng)
這篇文章主要為大家詳細介紹了python實現(xiàn)銀行實戰(zhàn)系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02python 實現(xiàn)多維數(shù)組(array)排序
今天小編就為大家分享一篇python 實現(xiàn)多維數(shù)組(array)排序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02