activation function:sigmoid、softmax、ReLU
各有什么优缺点?
先弄清楚:每个概念是什么
- Activation function
- 每层 linear 变换之后的那一下弯折。没有它,叠多少层都等于一层 linear。例:\(y = 2\cdot(3x) = 6x\)。
- Saturation
- 输入很大或很小时输出几乎不变,这时导数接近 0,gradient 传不回去。例:\(\sigma'(0) = 0.25\),\(\sigma'(5) \approx 0.0066\)。
- Sigmoid 与 tanh
- sigmoid 把任意实数压到 (0, 1),tanh 压到 (−1, 1),以 0 为中心。例:\(\sigma(0) = 0.5\),\(\sigma(2) \approx 0.88\);\(\tanh(0) = 0\)。
- Softmax
- 把一组 logits 按 \(e^{z}\) 的比例变成加起来等于 1 的概率,分数高一点分到的多很多。
- ReLU 与 dead ReLU
- 负数清零,正数原样通过。neuron 对所有输入都落在负区间时,输出和 gradient 永远是 0,叫 dead ReLU。
30 秒回答
Sigmoid squashes to (0, 1), so it suits binary outputs and gates, but it saturates: its derivative is at most 0.25 and near zero for large inputs, which causes vanishing gradients, and it isn't zero-centered. Tanh is zero-centered but still saturates. Softmax turns logits into a probability distribution for multi-class outputs; with cross-entropy the gradient is p minus y, and we subtract the max logit for stability. ReLU is the default for hidden layers: it doesn't saturate for positive inputs, and it's cheap and sparse. Its downside is dead ReLUs, units stuck at zero, which Leaky ReLU or GELU fix.
对比
| 函数 | 范围 | 导数最大 | 优点 | 缺点 | 用在哪 |
|---|---|---|---|---|---|
| sigmoid | (0, 1) | 0.25 | 输出像概率 | saturate → vanishing;非 zero-centered | binary output、LSTM gate |
| tanh | (−1, 1) | 1 | zero-centered | 两端仍 saturate | RNN hidden state |
| softmax | (0, 1),和为 1 | – | logits → 概率分布;配 cross-entropy,gradient = \(p - y\) | 要减 max 防 overflow;只适合类别互斥 | multi-class output |
| ReLU | [0, ∞) | 1 | 正区间不 saturate;便宜;sparse | dead ReLU;非 zero-centered | CNN / MLP hidden 默认 |
| Leaky ReLU | (−∞, ∞) | 1 | 负区间斜率 0.01,仍有 gradient | 多一个超参数 | 怕 dead ReLU 时 |
关键公式
sigmoid 与 tanh 的导数
\[ \sigma(z) = \frac{1}{1 + e^{-z}} \] \[ \sigma'(z) = \sigma(z)\big(1 - \sigma(z)\big) \le \tfrac14 \] \[ \tanh(z) = 2\sigma(2z) - 1 \] \[ \tanh'(z) = 1 - \tanh^2(z) \le 1 \]最大值都在 \(z = 0\),两端都趋于 0。
非 zero-centered 为什么不好
\[ \frac{\partial L}{\partial w_i} = \delta \cdot x_i \]上一层是 sigmoid 时所有 \(x_i > 0\),同一个 neuron 的所有 weight gradient 都和 \(\delta\) 同号,只能一起变大或变小,路线走成之字形。例:\(x = (0.5, 0.9)\),\(\delta = -2\) → gradient \((-1, -1.8)\)。
ReLU
\[ \mathrm{ReLU}(z) = \max(0, z),\qquad \mathrm{ReLU}'(z) = \mathbb{I}[z > 0] \]正区间导数恒为 1,深层连乘不衰减(数字见 vanishing gradient)。sigmoid 只留在输出层,配 cross-entropy 时 log 能抵消它的 saturation。
softmax + cross-entropy
\[ p_i = \frac{e^{z_i}}{\sum_j e^{z_j}} \] \[ \frac{\partial}{\partial z_i}\Big(-\sum_k y_k \log p_k\Big) = p_i - y_i \]所有 logits 加同一个常数,结果不变,所以先减最大值:
\[ \operatorname{softmax}(z) = \operatorname{softmax}\big(z - \max_j z_j\big) \]手算例子
logits \(z = (1, 2, 3)\),真实类别是第 3 类,\(y = (0, 0, 1)\):
- \(e^{z} = (2.72,\ 7.39,\ 20.09)\),和 = 30.19 → \(p = (0.090,\ 0.245,\ 0.665)\)
- loss \(= -\log 0.665 = 0.408\);gradient \(p - y = (0.090,\ 0.245,\ -0.335)\):错的类往下压,对的类往上推
- \(z = (1000, 1001, 1002)\):直接算 \(e^{1000}\) 得 inf,inf / inf = NaN;先减 max 变成 \((-2, -1, 0)\),结果不变
必答点
- 没有 nonlinear activation,多层网络等于一层 linear
- sigmoid:\(\sigma' \le 1/4\),两端 saturate → vanishing;非 zero-centered;现在只用在 binary output 和 gate
- tanh:zero-centered,但仍会 saturate
- softmax:multi-class output;配 cross-entropy 后 gradient \(= p - y\);先减 max
- ReLU:正区间导数 = 1、便宜、sparse,hidden layer 默认;缺点 dead ReLU → Leaky ReLU / GELU
追问
dead ReLU 是怎么产生的?怎么办?
learning rate 太大或初始化不好,把 weight / bias 推到对所有输入都有 \(z < 0\),gradient 恒为 0,再也回不来。办法是 Leaky ReLU / PReLU / ELU 让负区间也有 gradient,调小 learning rate,用 He init。
multi-label 用 softmax 还是 sigmoid?两者什么关系?
类别互斥用 softmax。multi-label 每类一个 sigmoid + binary cross-entropy。两类时 softmax 就是 sigmoid,\(\operatorname{softmax}(z)_1 = \sigma(z_1 - z_2)\)。
ReLU 在 0 点不可导,怎么 backprop?
实现里直接取一侧的导数(0 或 1)。输入恰好等于 0 几乎不会发生,所以不影响。
Transformer / LLM 用什么 activation?
BERT 和 GPT 用 GELU \(x\,\Phi(x)\),\(\Phi\) 是标准正态的 CDF,是平滑版 ReLU,负区间也有一点 gradient。LLaMA 用 SwiGLU。
来源Goodfellow et al., Deep Learning §6.2.2.3 eq. 6.32–6.33(p.182)、§6.3(p.188)、§6.3.1(pp.189–190)、§6.3.2(p.191)· Murphy, PML1 §13.2.3(pp.428–429)、§13.4.3(pp.448–451,Table 13.4)· Dive into Deep Learning §4.1.2.2 eq. 4.1.10 · Hendrycks & Gimpel 2016(GELU)· Devlin et al. 2018 Appendix A.2(p.13)· Touvron et al. 2023 §2.2(p.3)