激活函数及其梯度

激活函数及其梯度

sigmoid

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oWQDSPNI-1644334735092)(H:\codes\pytorch\Deep_Learning_PyTorch_note\激活函数.assets\image-20220124174300923.png)]

Derivative

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6LnisWYL-1644334735095)(H:\codes\pytorch\Deep_Learning_PyTorch_note\激活函数.assets\image-20220124174355850.png)]

>>> a=torch.linspace(-100,100,10)
>>> a
tensor([-100.0000,  -77.7778,  -55.5556,  -33.3333,  -11.1111,   11.1111,
          33.3333,   55.5556,   77.7778,  100.0000])
>>> torch.sigmoid(a)
tensor([0.0000e+00, 1.6655e-34, 7.4564e-25, 3.3382e-15, 1.4945e-05, 9.9999e-01,
        1.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00])

Tanh

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kpXOeNqk-1644334735096)(H:\codes\pytorch\Deep_Learning_PyTorch_note\激活函数.assets\image-20220124174333037.png)]

Derivative

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QWxehowX-1644334735097)(H:\codes\pytorch\Deep_Learning_PyTorch_note\激活函数.assets\image-20220124174412319.png)]

>>> a=torch.linspace(-1,1,10)
>>> a
tensor([-1.0000, -0.7778, -0.5556, -0.3333, -0.1111,  0.1111,  0.3333,  0.5556,
         0.7778,  1.0000])
>>> torch.tanh(a)
tensor([-0.7616, -0.6514, -0.5047, -0.3215, -0.1107,  0.1107,  0.3215,  0.5047,
         0.6514,  0.7616])

Rectified Linear Unit(ReLU)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fdfxdvap-1644334735098)(H:\codes\pytorch\Deep_Learning_PyTorch_note\激活函数.assets\image-20220124174539911.png)]

Derivative

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pLNFX21i-1644334735098)(H:\codes\pytorch\Deep_Learning_PyTorch_note\激活函数.assets\image-20220124174628247.png)]

>>> a=torch.linspace(-1,1,10)
>>> a
tensor([-1.0000, -0.7778, -0.5556, -0.3333, -0.1111,  0.1111,  0.3333,  0.5556,
         0.7778,  1.0000])
>>> torch.relu(a)
tensor([0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.1111, 0.3333, 0.5556, 0.7778,
        1.0000])

### GELU 激活函数及其梯度计算 Gaussian Error Linear Unit (GELU) 是一种基于高斯误差分布的激活函数,其定义如下: \[ G(x) = x \cdot P(X \leq x) = x \cdot \Phi(x) \] 其中 \(P(X \leq x)\) 表示标准正态分布累积分布函数(CDF),即 \(\Phi(x) = \frac{1}{2} \left[ 1 + \text{erf}\left( \frac{x}{\sqrt{2}} \right) \right]\),而 erf 则是误差函数。 #### GELU 的显式表达形式 为了便于实现和优化,通常会采用近似公式来替代原始定义。常见的两种近似方式分别为精确版和近似版: - **精确版本**: \[ G(x) = x \cdot \frac{1}{2} \left[ 1 + \text{erf}\left( \frac{x}{\sqrt{2}} \right) \right] \] - **近似版本**: \[ G(x) \approx 0.5 \cdot x \cdot \left( 1 + \tanh\left[\sqrt{\frac{2}{\pi}} \cdot \left( x + 0.044715 \cdot x^3 \right) \right] \right) \] 这两种形式都可以用于实际应用中,但在梯度推导上略有不同[^1]。 --- #### GELU 梯度公式的推导 假设输入为 \(x\),则 GELU 函数关于 \(x\) 的梯度可以通过链式法则求得。以下是详细的推导过程: ##### 对于精确版本: 已知: \[ G(x) = x \cdot \frac{1}{2} \left[ 1 + \text{erf}\left( \frac{x}{\sqrt{2}} \right) \right] \] 对其求导得到: \[ \frac{\partial G(x)}{\partial x} = \frac{1}{2} \left[ 1 + \text{erf}\left( \frac{x}{\sqrt{2}} \right) \right] + x \cdot \frac{d}{dx} \left[ \frac{1}{2} \text{erf}\left( \frac{x}{\sqrt{2}} \right) \right] \] 由于误差函数的导数性质为: \[ \frac{d}{dx} \text{erf}(z) = \frac{2}{\sqrt{\pi}} e^{-z^2} \] 因此有: \[ \frac{\partial G(x)}{\partial x} = \frac{1}{2} \left[ 1 + \text{erf}\left( \frac{x}{\sqrt{2}} \right) \right] + x \cdot \frac{1}{\sqrt{2\pi}} e^{-\frac{x^2}{2}} \] 最终可写成: \[ \frac{\partial G(x)}{\partial x} = \Phi(x) + x \phi(x) \] 其中 \(\phi(x) = \frac{1}{\sqrt{2\pi}} e^{-\frac{x^2}{2}}\) 是标准正态分布的概率密度函数(PDF)。 --- ##### 对于近似版本: 已知: \[ G(x) \approx 0.5 \cdot x \cdot \left( 1 + \tanh\left[\sqrt{\frac{2}{\pi}} \cdot \left( x + 0.044715 \cdot x^3 \right) \right] \right) \] 设中间变量为: \[ u = \sqrt{\frac{2}{\pi}} \cdot \left( x + 0.044715 \cdot x^3 \right) \] 那么: \[ \tanh(u)' = 1 - \tanh^2(u) \] 对整个函数求导后可以得到: \[ \frac{\partial G(x)}{\partial x} = 0.5 \cdot \left( 1 + \tanh(u) \right) + 0.5 \cdot x \cdot \left( 1 - \tanh^2(u) \right) \cdot u' \] 其中 \(u' = \sqrt{\frac{2}{\pi}} \cdot \left( 1 + 0.134145 \cdot x^2 \right)\)。 将上述各项代入即可完成完整的梯度计算[^2]。 --- ### 实现代码示例 以下是一个 Python 中实现 GELU 及其梯度的代码片段: ```python import numpy as np def gelu_exact(x): """Exact version of the GELU function.""" return 0.5 * x * (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3)))) def gelu_gradient_exact(x): """Gradient of the exact GELU function.""" cdf = 0.5 * (1 + scipy.special.erf(x / np.sqrt(2))) pdf = (1 / np.sqrt(2 * np.pi)) * np.exp(-np.power(x, 2) / 2) return cdf + x * pdf # Example usage x = np.array([1.0, 2.0, 3.0]) print(gelu_exact(x)) print(gelu_gradient_exact(x)) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值