Keras 中Leaky ReLU等高級(jí)激活函數(shù)的用法
在用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()
這里從整個(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 | |
---|---|
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ù))值控制負(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ù)的用法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3實(shí)現(xiàn)的騰訊微博自動(dòng)發(fā)帖小工具
這篇文章主要為大家分享下騰訊微博自動(dòng)發(fā)帖的Python3代碼,需要的朋友可以參考下2013-11-11python數(shù)據(jù)預(yù)處理 :樣本分布不均的解決(過采樣和欠采樣)
今天小編就為大家分享一篇python數(shù)據(jù)預(yù)處理 :樣本分布不均的解決(過采樣和欠采樣),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-02-02python函數(shù)也可以是一個(gè)對(duì)象,可以存放在列表中并調(diào)用方式
這篇文章主要介紹了python函數(shù)也可以是一個(gè)對(duì)象,可以存放在列表中并調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python批量生成身份證號(hào)到Excel的兩種方法實(shí)例
這篇文章主要給大家介紹了關(guān)于python批量生成身份證號(hào)到Excel的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python實(shí)現(xiàn)銀行實(shí)戰(zhàn)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)銀行實(shí)戰(zhàn)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02python 實(shí)現(xiàn)多維數(shù)組(array)排序
今天小編就為大家分享一篇python 實(shí)現(xiàn)多維數(shù)組(array)排序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-02-02Python 實(shí)現(xiàn)12306登錄功能實(shí)例代碼
這篇文章主要介紹了Python 實(shí)現(xiàn)12306登錄功能的完整代碼,需要的朋友可以參考下2018-02-02