文章目錄
- Softplus
- 函數+導函數
- 函數和導函數圖像
- 優缺點
- PyTorch 中的 Softplus 函數
- TensorFlow 中的 Softplus 函數
Softplus
函數+導函數
-
Softplus
函數
Softplus?(x)=ln?(1+ex)\begin{aligned} \operatorname{Softplus}(x) &= \ln \bigl(1 + e^{\,x}\bigr) \end{aligned} Softplus(x)?=ln(1+ex)? -
Softplus
函數導數
ddxSoftplus?(x)=ddxln??(1+ex)=11+ex?ex=ex1+ex=ex1+ex?e?xe?x=11+e?x=σ(x)\begin{aligned} \frac{d}{dx}\operatorname{Softplus}(x) &=\frac{d}{dx}\ln\!\left(1+e^{x}\right)\\ &=\frac{1}{1+e^{x}}\cdot e^{x}\\ &=\frac{e^{x}}{1+e^{x}}\\ &=\frac{e^{x}}{1+e^{x}} \cdot \frac{e^{-x}}{e^{-x}}\\ &=\frac{1}{1+e^{-x}}\\ &=\sigma(x) \end{aligned} dxd?Softplus(x)?=dxd?ln(1+ex)=1+ex1??ex=1+exex?=1+exex??e?xe?x?=1+e?x1?=σ(x)?
其中,σ(x)=11+e?x\sigma(x)=\dfrac{1}{1+e^{-x}}σ(x)=1+e?x1? 是 sigmoid 函數。Softplus 處處可導,并且導數恰好是 sigmoid。
函數和導函數圖像
-
畫圖
import numpy as np from matplotlib import pyplot as plt# Softplus 函數 def softplus(x):return np.log1p(np.exp(x))# Softplus 的導數 = sigmoid def softplus_derivative(x):return 1 / (1 + np.exp(-x))# 生成數據 x = np.linspace(-6, 6, 1000) y = softplus(x) y1 = softplus_derivative(x)# 繪圖 plt.figure(figsize=(12, 8)) ax = plt.gca() plt.plot(x, y, label='Softplus') plt.plot(x, y1, label='Derivative (Sigmoid)') plt.title('Softplus and Derivative')# 去邊框 ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data', 0)) ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data', 0))plt.legend() plt.savefig('./softplus.jpg',dpe=300) plt.show()3
優缺點
-
Softplus 的優點
- 平滑處處可導:Softplus 是 ReLU 的光滑近似,沒有折點,避免了 ReLU 在 0 處不可導的問題。
- 梯度不消失:對于任意輸入,梯度始終為正,并且隨輸入增大趨近于 1,有效緩解梯度消失。
- 解析形式簡單:公式簡潔,易于實現,且與 sigmoid 有天然聯系。
- 連續可導:在需要二階導數或高階導數的場景(如 Hessian、自然梯度)中更容易處理。
-
Softplus 的缺點
- 計算開銷:相比 ReLU 的逐位最大值操作,Softplus 需要計算指數和對數,計算量更大。
- 輸出始終為正:當需要負激活值時(如殘差網絡中的負值路徑),Softplus 無法提供。
- 邊緣飽和:當輸入為很大的負數時,Softplus 會趨于 0,雖然比 sigmoid 緩解,但仍可能帶來梯度衰減。
- 超參數敏感:在部分任務中需要額外調整初始化或學習率,以抵消其非零均值的副作用。
PyTorch 中的 Softplus 函數
-
代碼
import torch import torch.nn.functional as F# 使用 PyTorch 自帶的 Softplus sp = F.softplusx = torch.tensor([-2.0, 0.0, 2.0]) y = sp(x)print("x :", x) print("softplus(x):", y)"""輸出""" x : tensor([-2., 0., 2.]) softplus(x): tensor([0.1269, 0.6931, 2.1269])
TensorFlow 中的 Softplus 函數
-
環境
python: 3.10.9
tensorflow: 2.19.0
-
代碼
import tensorflow as tfsoftplus = tf.keras.activations.softplusx = tf.constant([-2.0, 0.0, 2.0]) y = softplus(x)print("x :", x.numpy()) print("softplus(x):", y.numpy())"""輸出""" x : [-2. 0. 2.] softplus(x): [0.12692805 0.6931472 2.126928 ]