激活函数及其梯度

一、激活函数及其梯度

一、简介

  1. 在神经网络中引入激活函数的目的是:我们的数据绝大部分都是非线性的,而在一般的神经网络中,计算都是线性的。而引入激活函数就是在神经网络中引入非线性,强化网络的学习能力。

二、常见的激活函数

1、Sigmoid函数

  1. 公式: f ( x ) = σ ( x ) = 1 1 + e − x f(x)=\sigma(x)=\frac{1}{1+e^{-x}} f(x)=σ(x)=1+ex1
    在这里插入图片描述
  2. 导数: f ( x ) ′ = e − x ( 1 + e − x ) 2 = f ( x ) ∗ ( 1 − f ( x ) ) f(x)' = \frac{e^{-x}}{(1+e^{-x})^2}=f(x)*(1-f(x)) f(x)=(1+ex)2ex=f(x)(1f(x))
    在这里插入图片描述
  3. 代码
a = torch.linspace(-100,100,10)
print(a)
print(torch.sigmoid(a))
# 输出:
# tensor([-100.0000,  -77.7778,  -55.5556,  -33.3333,  -11.1111,   11.1111,
#          33.3333,   55.5555,   77.7778,  100.0000])
# 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])

2、Tanh函数

  1. 公式: f ( x ) = t a n h ( x ) = e x − e − x e x + e − x = 2 ∗ s i g m o i d ( 2 x ) − 1 f(x)=tanh(x)=\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}=2*sigmoid(2x)-1 f(x)=tanh(x)=ex+exexex=2sigmoid(2x)1
    在这里插入图片描述
  2. 导数: t a n h ( x ) ′ = 1 − ( e x − e − x ) 2 ( e x + e − x ) 2 = 1 − t a n h ( x ) 2 tanh(x)'=1-\frac{(e^{x}-e^{-x})^2}{(e^{x}+e^{-x})^2}=1-tanh(x)^2 tanh(x)=1(ex+ex)2(exex)2=1tanh(x)2
    在这里插入图片描述
  3. 代码
a = torch.linspace(-1,1,10)
print(a)
print(torch.tanh(a))
# 输出
# tensor([-1.0000, -0.7778, -0.5556, -0.3333, -0.1111,  0.1111,  0.3333,  0.5556,
#          0.7778,  1.0000])
# tensor([-0.7616, -0.6514, -0.5047, -0.3215, -0.1107,  0.1107,  0.3215,  0.5047,
#          0.6514,  0.7616])

3、ReLU函数

  1. 公式: f ( x ) = { 0     f o r    x    <    0 x    f o r    x    ⩾    0 f\left( x \right) =\begin{cases} 0\,\,\, for\,\,x\,\,<\,\,0\\ x\,\, for\,\,x\,\,\geqslant \,\,0\\ \end{cases} f(x)={0forx<0xforx0
    在这里插入图片描述

  2. 导数: f ( x ) = { 0     f o r    x    <    0 1    f o r    x    ⩾    0 f\left( x \right) =\begin{cases} 0\,\,\, for\,\,x\,\,<\,\,0\\ 1\,\, for\,\,x\,\,\geqslant \,\,0\\ \end{cases} f(x)={0forx<01forx0

  3. 代码

import torch
from torch.nn import functional as F


a = torch.linspace(-1,1,10)
print(a)
print(torch.relu(a))
print(F.relu(a))   #两种调用函数形式

# tensor([-1.0000, -0.7778, -0.5556, -0.3333, -0.1111,  0.1111,  0.3333,  0.5556,
#         0.7778,  1.0000])
# tensor([0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.1111, 0.3333, 0.5556, 0.7778,
#         1.0000])
# 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、付费专栏及课程。

余额充值