Swish 啟用函數簡介
Swish 激活函數是一種廣泛應用於深度學習的激活函數,最早在 GELU 論文中以 Sigmoid-weighted Linear Unit (SiLU) 的名稱提出。本文將介紹 Swish 的數學原理及其應用,並透過與其他激活函數的比較,分析其性質。
1. 簡介
在深度神經網路中,激活函數的選擇對模型效能有重要影響。從 Sigmoid 到 ReLU,再到 GELU,激活函數的發展反映了深度學習領域對非線性變換的持續探索。Swish 是一種相對較新的激活函數,最早在 GELU 論文中以 Sigmoid-weighted Linear Unit (SiLU) 的形式提出,後來由 Google Brain 團隊重新發現並推廣。
發展歷程
Swish/SiLU 的發展經歷了以下幾個重要階段:
- 2016 年:在 GELU 論文中首次提出 SiLU
- 2017 年:在強化學習領域以 Sigmoid-weighted Linear Unit (SiL) 的名稱再次提出
- 2017 年底:Google Brain 團隊提出 Swish 函數,並引入可學習的參數 \beta
2. Swish 的數學表示
2.1 基本定義
Swish 函數是一族帶有參數 \beta 的函數,其數學表示式為:
\begin{equation}\text{Swish}_\beta(x) = x \cdot \sigma(\beta x) = \frac{x}{1 + e^{-\beta x}}\end{equation}其中 \sigma(x) 是 Sigmoid 函數:
\begin{equation}\sigma(x) = \frac{1}{1 + e^{-x}}\end{equation}當 \beta = 1 時,Swish 簡化為 SiLU:
\begin{equation}\text{SiLU}(x) = \text{Swish}_1(x) = \frac{x}{1 + e^{-x}}\end{equation}Swish 函數的特性會隨 \beta 的取值而改變:
- 當 \beta = 0 時,函數變為線性函數:f(x) = x/2
- 當 \beta = 1 時,函數即為 SiLU
- 當 \beta \to \infty 時,函數趨近於 ReLU
2.2 導數計算
回顧基本形式
Swish 函數的基本形式為:
\begin{equation}\text{Swish}(x) = x \cdot \sigma(\beta x)\end{equation}其中 \sigma(x) 為 Sigmoid 函數:
\begin{equation}\sigma(x) = \frac{1}{1 + e^{-x}}\end{equation}利用乘積法則推導導數
根據乘積法則:
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}[f(x)g(x)] = f'(x)g(x) + f(x)g'(x)\end{equation}對 Swish 函數求導:
\begin{equation}\begin{aligned}\frac{\mathrm{d}}{\mathrm{dx}}\text{Swish}(x) &= \frac{\mathrm{d}}{\mathrm{dx}}[x \cdot \sigma(\beta x)] \\&= 1 \cdot \sigma(\beta x) + x \cdot \frac{\mathrm{d}}{\mathrm{dx}}\sigma(\beta x)\end{aligned}\end{equation}Sigmoid 函數的導數為:
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\sigma(x) = \sigma(x)(1-\sigma(x))\end{equation}因此:
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\sigma(\beta x) = \beta\sigma(\beta x)(1-\sigma(\beta x))\end{equation}將上式代入,即可得到 Swish 的導數:
\begin{equation}\begin{aligned}\frac{\mathrm{d}}{\mathrm{dx}}\text{Swish}(x) &= \sigma(\beta x) + x\beta\sigma(\beta x)(1-\sigma(\beta x)) \\&= \sigma(\beta x) + \beta x\sigma(\beta x) - \beta x\sigma^2(\beta x) \\&= \sigma(\beta x)(1 + \beta x(1-\sigma(\beta x)))\end{aligned}\end{equation}對於最常用的 SiLU,將 \beta = 1 代入上述公式,即可得到:
\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{SiLU}(x) = \sigma(x)(1 + x(1-\sigma(x)))\end{equation}
導數的性質
Swish 的導數具有以下重要特性:
- 導數在 x = 0 處的值為 1/2
- 導數在某些區間可以大於 1,因此梯度可能在反向傳播時被放大
- 導數是平滑的,有助於提升最佳化過程的穩定性
3. Swish 與其他激活函數的比較
3.1 與 ReLU 的比較
ReLU 的表示式為:
\begin{equation}\text{ReLU}(x) = \max(0,x)\end{equation}
與 ReLU 相比,Swish 具有以下特點:
- 平滑性:Swish 在所有點皆平滑,而 ReLU 在 x = 0 處不可微分
- 負值區域:Swish 允許少量負值通過,這可能有助於資訊流動
- 有界性:當 x \to -\infty 時,Swish 趨近於 0,而不像 ReLU 那樣直接截斷
3.2 與 GELU 的比較
GELU 的表示式為:
\begin{equation}\text{GELU}(x) = x\cdot\Phi(x)\end{equation}
Swish 和 GELU 有一些共同點:
- 都是平滑函數
- 都可視為以某個機率函數對輸入值加權的結果
- 在實際應用中的效能相近
主要差異:
- 計算複雜度:Swish 的計算相對簡單
- 機率解釋:GELU 使用高斯分布,而 Swish 使用 Sigmoid 函數
- 參數化:Swish 引入了可學習的參數 \beta
參考資料
- Gaussian Error Linear Units (GELUs)
- Searching for Activation Functions
- Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning
- PyTorch Documentation – torch.nn.SiLU
- Wikipedia – Swish function
- Some recent activation functions mimicking ReLU
- 淺談神經網路中的啟用函數設計
- Google 大腦提出新型啟用函數 Swish 引發爭議:能直接取代 ReLU 並有更好的表現嗎?
- GELU(Gaussian Error Linear Unit)啟用函數簡介




