activation function:sigmoid、softmax、ReLU
各有什么优缺点?

英文问法Compare sigmoid, tanh, softmax and ReLU. · Why is ReLU preferred over sigmoid in hidden layers? · What is the dying ReLU problem? · Why do we subtract the max in softmax?

考过面经:微软 Office Copilot AS PhD 实习("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-centeredbinary output、LSTM gate
tanh(−1, 1)1zero-centered两端仍 saturateRNN hidden state
softmax(0, 1),和为 1–logits → 概率分布;配 cross-entropy,gradient = \(p - y\)要减 max 防 overflow;只适合类别互斥multi-class output
ReLU[0, ∞)1正区间不 saturate;便宜;sparsedead ReLU;非 zero-centeredCNN / 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)\):

必答点

追问

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)