GELU(Gaussian Error Linear Unit)激活函數簡介

GELU 在深度學習領域有不少應用,尤其常見於 BERT、GPT 等大型語言模型。本文介紹 GELU(Gaussian Error Linear Unit)活化函數的理論與應用,從機率論的角度推導 GELU 的數學形式及其導數,並透過與 ReLU、ELU 等經典活化函數的比較,分析 GELU 的特性。

1. 簡介

在深度學習的發展過程中,活化函數扮演著至關重要的角色。從早期的 Sigmoid 到廣泛使用的 ReLU,活化函數的選擇直接影響神經網路的效能。GeLU (Gaussian Error Linear Unit) 是一種新型活化函數,在多個領域都展現出優異的效能,並廣泛應用於 BERT、GPT 等大型語言模型。

機率觀點

從機率的角度來看,GELU 可視為隨機正規化過程的確定性近似。可以用一些常見的方法來類比:ReLU 可視為以確定的方式將輸入乘以 0 或 1;Dropout 則是隨機將輸入乘以 0。同樣地,對於輸入 x,GELU 的輸出可視為下列隨機過程的期望值:

\begin{equation}\text{GELU}(x) = x \cdot \mathbb{E}[m], \text{ where } m \sim \text{Bernoulli}(\Phi(x))\end{equation}

這樣的解釋讓 GELU 在理論上的統計意義更為明確。

2. GELU 的數學表示

2.1 基本定義


GELU 的數學表示式為:

\begin{equation}\text{GELU}(x) = x \cdot \Phi(x)\end{equation}

其中 \Phi(x) 是標準常態分布的累積分布函數(CDF)。

標準常態分布的機率密度函數為:

\begin{equation}f(x)=\frac1{\sqrt{2\pi}}\mathrm{~exp}{\left(-\frac{x^2}2\right)}\end{equation}

則累積分布函數 {P}\left(X\leq{x}\right) 為:

\begin{equation}\Phi(x)=F(x;0,1)=\frac1{\sqrt{2\pi}}\int_{-\infty}^x\exp\left(-\frac{t^2}2\right)dt.\end{equation}

根據誤差函數 erf 的定義:
\begin{equation}\mathrm{erf}(x)=\frac1{\sqrt{\pi}}\int_{-x}^xe^{-t^2}\mathrm{~d}t=\frac2{\sqrt{\pi}}\int_0^xe^{-t^2}\mathrm{~d}t.\end{equation}

推導如下:
\begin{equation}\begin{aligned}\Phi(x)&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{x}e^{-\frac{t^{2}}{2}}dt \\&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{0}e^{-\frac{t^{2}}{2}}dt\quad+\quad\frac{1}{\sqrt{2\pi}}\int_{0}^{x}e^{-\frac{t^{2}}{2}}dt \\&\overset{z=\frac{t}{\sqrt{2}}}{\Longrightarrow } \frac{1}{2}+\frac{1}{\sqrt{2\pi}}\int_{0}^{x}e^{-z^{2}}\cdot\sqrt{2}\cdot dz \\&= \frac{1}{2}\left[1+\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-z^{2}}\cdot dz \right] \\&=\frac{1}{2}\left[ 1+\operatorname{erf}(\frac{x}{\sqrt{2}}) \right]\end{aligned}\end{equation}

則 \Phi(x) 可簡化為:
\begin{equation}\Phi(z)=\frac{1}{2}\left[1+\mathrm{erf}{\left(\frac{z}{\sqrt{2}}\right)}\right]\end{equation}

根據上述公式,GELU 的完整形式可表示為:

\begin{equation}\begin{align*}\text{GELU}\left(x\right) &= x{P}\left(X\leq{x}\right) \\&= x\frac1{\sqrt{2\pi}}\int_{-\infty}^x\exp\left(-\frac{t^2}2\right)dt \\&= x\Phi\left(x\right) \\&= x \cdot \frac{1}{2}\left[1 + \text{erf}(x/\sqrt{2})\right],\end{align*}\end{equation}

ReLU 根據輸入的正負決定是否讓輸入通過(x\mathbf{1}_{x>0}),而 GELU 則以輸入值 x 在標準常態分布中的累積機率(即百分位數)來加權,因此活化曲線比 ReLU 更平滑。

2.2 近似計算

由於 GELU 的精確計算較為複雜,實際應用中常採用以下兩種近似方法:

近似方法 1(以雙曲正切函數為基礎,又稱 tanh 近似):

\begin{equation}\text{GELU}(x) \approx 0.5x\left(1 + \tanh\left[\sqrt{2/\pi}(x + 0.044715x^3)\right]\right)\end{equation}

近似方法 2(以 Sigmoid 函數為基礎,又稱 sigmoid 近似):

\begin{equation}\text{GELU}(x) \approx x \cdot \sigma(1.702x)\end{equation}

其中 \sigma(x) 是 sigmoid 函數。

2.3 導數的推導過程

使用鏈鎖律

要計算 GELU 的導數,我們需要使用鏈鎖律。首先將 GELU 函數展開:

\begin{equation}\text{GELU}(x) = \frac{x}{2} + \frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\end{equation}

對這個表示式求導時,需要使用乘積法則:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) = \frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\right) + \frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\right)\end{equation}

逐步推導

第一項很簡單:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\right) = \frac{1}{2}\end{equation}

第二項需要使用乘積法則:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\left(\frac{x}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\right) = \frac{1}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right) + \frac{x}{2}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}\left(\frac{x}{\sqrt{2}}\right)\end{equation}

誤差函數的導數

誤差函數的導數為:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}(x) = \frac{2}{\sqrt{\pi}}e^{-x^2}\end{equation}

使用鏈鎖律可得:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{erf}\left(\frac{x}{\sqrt{2}}\right) = \frac{2}{\sqrt{\pi}}e^{-x^2/2} \cdot \frac{1}{\sqrt{2}} = \sqrt{\frac{2}{\pi}}e^{-x^2/2}\end{equation}

合併結果

合併各項後可得:

\begin{equation}\begin{aligned}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) &= \frac{1}{2} + \frac{1}{2}\text{erf}\left(\frac{x}{\sqrt{2}}\right) + \frac{x}{2} \cdot \sqrt{\frac{2}{\pi}}e^{-x^2/2} \\&= \frac{1}{2}\left[1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right] + \frac{x}{\sqrt{2\pi}}e^{-x^2/2}\end{aligned}\end{equation}

導數的另一種表示形式

利用標準常態分布的累積分布函數 \Phi(x) 和機率密度函數 \phi(x),可將 GELU 的導數寫成更簡潔的形式:

\begin{equation}\frac{\mathrm{d}}{\mathrm{dx}}\text{GELU}(x) = \Phi(x) + x\phi(x)\end{equation}

其中:

\begin{equation}\begin{aligned}\Phi(x) &= \frac{1}{2}\left[1 + \text{erf}\left(\frac{x}{\sqrt{2}}\right)\right] \\\phi(x) &= \frac{1}{\sqrt{2\pi}}e^{-x^2/2}\end{aligned}\end{equation}

導數的性質分析

GELU 的導數具有以下重要性質:

  • 處處連續且可微分
  • 當 x \to +\infty 時,導數趨近於 1
  • 當 x \to -\infty 時,導數趨近於 0
  • 在 x = 0 處,導數值為 0.5

3. GELU 與其他激活函數的比較

3.1 與 ReLU 比較


ReLU 的激活函數表示式:
\begin{equation}\operatorname{ReLU}(x)=\begin{cases}x &\quad \text{ if } x>0\\0 &\quad \text{ if } x\leq0\end{cases}\end{equation}

GELU 可以視為 ReLU 的機率化推廣:

  • 當 x \gg 0 時,\Phi(x) \approx 1,此時 \operatorname{GELU}(x) \approx x,與 ReLU 類似
  • 當 x \ll 0 時,\Phi(x) \approx 0,此時 \operatorname{GELU}(x) \approx 0,與 ReLU 類似

3.2 與 ELU 比較


ELU 的激活函數表示式:
\begin{equation}\operatorname{ELU}(x)=\begin{cases}x &\quad \text{ if } x \geq 0 \\\alpha(e^x-1) &\quad \text{ if } x < 0\end{cases}\end{equation}

  • ELU 可以視為 xP(X \leq x), X \sim \operatorname{Cauchy}(0, 1)(Cauchy distribution 柯西分布)的變型,形式上與 GELU 相近。
  • 在 0 附近,GELU 提供了平滑的過渡,這項特性與 ELU 類似

參考資料

  1. Gaussian Error Linear Units (GELUs)
  2. GeLU – Papers with Code
  3. PyTorch Documentation – torch.nn.GELU
  4. WIKI- 誤差函數
  5. GELU 激活函數介紹與筆記
  6. [Deep Learning] GELU (Gaussian Error Linerar Units)

Add a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *