激活函數可以透過 Activation
層使用,也可以透過所有前饋層支援的 activation
參數使用
model.add(layers.Dense(64, activation=activations.relu))
這等同於
from keras import layers
from keras import activations
model.add(layers.Dense(64))
model.add(layers.Activation(activations.relu))
所有內建的激活函數也可以透過它們的字串識別符號傳遞
model.add(layers.Dense(64, activation='relu'))
relu
函數keras.activations.relu(x, negative_slope=0.0, max_value=None, threshold=0.0)
應用修正線性單元激活函數。
使用預設值時,這會傳回標準 ReLU 激活函數:max(x, 0)
,即 0 和輸入張量的逐元素最大值。
修改預設參數可讓您使用非零閾值、變更激活的最大值,並針對低於閾值的值使用輸入的非零倍數。
範例
>>> x = [-10, -5, 0.0, 5, 10]
>>> keras.activations.relu(x)
[ 0., 0., 0., 5., 10.]
>>> keras.activations.relu(x, negative_slope=0.5)
[-5. , -2.5, 0. , 5. , 10. ]
>>> keras.activations.relu(x, max_value=5.)
[0., 0., 0., 5., 5.]
>>> keras.activations.relu(x, threshold=5.)
[-0., -0., 0., 0., 10.]
引數
float
,控制低於閾值的值的斜率。float
,設定飽和閾值(函數將傳回的最大值)。float
,指定激活函數的閾值,低於該閾值的值將會被抑制或設為零。傳回
一個與輸入 x
具有相同形狀和資料類型的張量。
sigmoid
函數keras.activations.sigmoid(x)
Sigmoid 激活函數。
其定義為:sigmoid(x) = 1 / (1 + exp(-x))
。
對於小值 (< -5),sigmoid
會傳回接近零的值,對於大值 (> 5),函數的結果會接近 1。
Sigmoid 等同於一個 2 元素 softmax,其中第二個元素假設為零。sigmoid 函數始終傳回 0 到 1 之間的值。
引數
softmax
函數keras.activations.softmax(x, axis=-1)
Softmax 將一個值向量轉換為機率分佈。
輸出向量的元素範圍在 [0, 1]
之間,且總和為 1。
每個輸入向量都會獨立處理。axis
引數設定函數應用於輸入的軸。
Softmax 通常用作分類網路最後一層的激活函數,因為結果可以解釋為機率分佈。
每個向量 x 的 softmax 計算為 exp(x) / sum(exp(x))
。
輸入值是所得機率的對數優勢比。
引數
softplus
函數keras.activations.softplus(x)
Softplus 激活函數。
其定義為:softplus(x) = log(exp(x) + 1)
。
引數
softsign
函數keras.activations.softsign(x)
Softsign 激活函數。
Softsign 定義為:softsign(x) = x / (abs(x) + 1)
。
引數
tanh
函數keras.activations.tanh(x)
雙曲正切激活函數。
其定義為:tanh(x) = sinh(x) / cosh(x)
,即 tanh(x) = ((exp(x) - exp(-x)) / (exp(x) + exp(-x)))
。
引數
selu
函數keras.activations.selu(x)
縮放指數線性單元 (SELU)。
縮放指數線性單元 (SELU) 激活函數定義為
x > 0
,則為 scale * x
x < 0
,則為 scale * alpha * (exp(x) - 1)
其中 alpha
和 scale
是預定義的常數 (alpha=1.67326324
和 scale=1.05070098
)。
基本上,SELU 激活函數會將 scale
(> 1) 乘以 keras.activations.elu
函數的輸出,以確保正輸入的斜率大於 1。
選擇 alpha
和 scale
的值,以便在兩個連續層之間保留輸入的平均值和變異數,只要權重正確初始化 (請參閱 keras.initializers.LecunNormal
初始化器) 且輸入單元的數量「足夠大」(如需更多資訊,請參閱參考論文)。
引數
注意事項
keras.initializers.LecunNormal
初始化器搭配使用。keras.layers.AlphaDropout
(而不是常規 dropout) 搭配使用。參考
elu
函數keras.activations.elu(x, alpha=1.0)
指數線性單元。
具有 alpha > 0
的指數線性單元 (ELU) 定義為
x > 0
,則為 x
x < 0
,則為 alpha * exp(x) - 1
ELU 具有負值,這會將激活的平均值推向零。
平均激活值越接近零,學習速度就越快,因為它們會使梯度更接近自然梯度。當引數變小時,ELU 會飽和至負值。飽和表示導數小,這會減少變化和傳播到下一層的資訊。
引數
參考
exponential
函數keras.activations.exponential(x)
指數激活函數。
引數
leaky_relu
函數keras.activations.leaky_relu(x, negative_slope=0.2)
Leaky relu 激活函數。
引數
float
,控制低於閾值的值的斜率。relu6
函數keras.activations.relu6(x)
Relu6 激活函數。
它是 ReLU 函數,但截斷為最大值 6。
引數
silu
函數keras.activations.silu(x)
Swish (或 Silu) 激活函數。
其定義為:swish(x) = x * sigmoid(x)
。
Swish (或 Silu) 激活函數是一個平滑、非單調函數,其上方無界且下方有界。
引數
參考
hard_silu
函數keras.activations.hard_silu(x)
Hard SiLU 激活函數,也稱為 Hard Swish。
其定義為
if x < -3
,則為 0
x > 3
,則為 x
-3 <= x <= 3
,則為 x * (x + 3) / 6
它是 silu 激活函數的更快、分段線性近似值。
引數
參考
gelu
函數keras.activations.gelu(x, approximate=False)
高斯誤差線性單元 (GELU) 激活函數。
高斯誤差線性單元 (GELU) 定義為
gelu(x) = x * P(X <= x)
,其中 P(X) ~ N(0, 1)
,即 gelu(x) = 0.5 * x * (1 + erf(x / sqrt(2)))
。
GELU 會根據輸入的值加權,而不是像 ReLU 那樣根據輸入的符號選通輸入。
引數
bool
,是否啟用近似值。參考
hard_sigmoid
函數keras.activations.hard_sigmoid(x)
Hard sigmoid 激活函數。
Hard sigmoid 激活函數定義為
if x <= -3
,則為 0
x >= 3
,則為 1
-3 < x < 3
,則為 (x/6) + 0.5
它是 sigmoid 激活函數的更快、分段線性近似值。
引數
參考
linear
函數keras.activations.linear(x)
線性激活函數 (直通)。
「線性」激活函數是恆等函數:它會傳回未修改的輸入。
引數
mish
函數keras.activations.mish(x)
Mish 激活函數。
其定義為
mish(x) = x * tanh(softplus(x))
其中 softplus
定義為
softplus(x) = log(exp(x) + 1)
引數
參考
log_softmax
函數keras.activations.log_softmax(x, axis=-1)
Log-Softmax 激活函數。
每個輸入向量都會獨立處理。axis
引數設定函數應用於輸入的軸。
引數
celu
函數keras.activations.celu(x, alpha=1.0)
連續可微分指數線性單元。
CeLU 激活函數定義為
celu(x) = alpha * (exp(x / alpha) - 1)
,適用於 x < 0
,celu(x) = x
,適用於 x >= 0
。
其中 alpha
是一個縮放參數,可控制激活的形狀。
引數
1.0
。參考
squareplus
函數keras.activations.squareplus(x, b=4)
Squareplus 激活函數。
Squareplus 激活函數定義為
f(x) = (x + sqrt(x^2 + b)) / 2
其中 b
是一個平滑參數。
引數
參考
soft_shrink
函數keras.activations.soft_shrink(x, threshold=0.5)
Soft Shrink 激活函數。
其定義為
如果 x > threshold
,則為 soft_shrink(x) = x - threshold
,如果 x < -threshold
,則為 soft_shrink(x) = x + threshold
,否則為 soft_shrink(x) = 0
。
引數
glu
函數keras.activations.glu(x, axis=-1)
閘門線性單元 (Gated Linear Unit, GLU) 激活函數。
GLU 激活函數定義為
glu(x) = a * sigmoid(b)
,
其中 x
沿著給定的軸分割成兩個相等的部分 a
和 b
。
引數
-1
。參考
tanh_shrink
函數keras.activations.tanh_shrink(x)
Tanh 收縮 (Tanh shrink) 激活函數。
其定義為
f(x) = x - tanh(x)
.
引數
hard_tanh
函數keras.activations.hard_tanh(x)
HardTanh 激活函數。
定義為:hard_tanh(x) = -1 當 x < -1
,hard_tanh(x) = x 當 -1 <= x <= 1
,hard_tanh(x) = 1 當 x > 1
。
引數
hard_shrink
函數keras.activations.hard_shrink(x, threshold=0.5)
Hard Shrink 激活函數。
其定義為
hard_shrink(x) = x
若 |x| > threshold
,hard_shrink(x) = 0
否則。
引數
log_sigmoid
函數keras.activations.log_sigmoid(x)
Sigmoid 激活函數的對數。
定義為 f(x) = log(1 / (1 + exp(-x)))
。
引數
您也可以使用可調用對象作為激活函數(在這種情況下,它應該接收一個張量並返回一個相同形狀和 dtype 的張量)。
model.add(layers.Dense(64, activation=keras.ops.tanh))
比簡單函數更複雜的激活函數(例如,可學習的激活函數,它們維護一個狀態)可以作為進階激活層使用。