GeLU,(Gaussian Error Linerar Units),高斯誤差線性單元
1 Gelu數學原理
G(x) = xΦ(X<=x)
其中,Φ(X)爲正態分佈。
累積分佈函數是指隨機變量XX小於或等於xx的概率,用概率密度函數表示爲:
正態分佈的累積分佈函數可以用“誤差函數” erf 表示:
其中誤差函數的表達式爲:
2 代碼實現:
def gelu(x):
"""Implementation of the gelu activation function.
For information: OpenAI GPT's gelu is slightly different (and gives slightly different results):
0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))
Also see https://arxiv.org/abs/1606.08415
"""
return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
3 GeLU與ReLU、sigmoid的區別
GeLU激活函數的最大特點是將非線性與依賴輸入數據分佈的隨機正則化器相結合在一個激活函數的表達中。與以往dropout指定隨機概率值或ReLU根據輸入值的正負進行mask的方式不同,GeLU根據當前input大於其餘inputs的概率進行隨機正則化,即爲在mask時依賴輸入的數據分佈。
GeLU可以看做是ReLU、zoneout、dropout的一個綜合。