M3-015M3: Deep Learning FoundationsActivation FunctionsMedium
⚡Flashcard Drill➔Mastery:
Why Modern Transformers Use GELU and SiLU Instead of ReLU
📐 Mathematical Definition
⚡ One-Sentence Core Answer
GELU and SiLU offer smooth non-monotonic curvature, continuous higher-order derivatives, and probabilistic gating interpretations that eliminate gradient shattering and dying neurons.
📌 Key Takeaways
- •Smoothness: GELU/SiLU are smooth functions with non-zero curvature everywhere, avoiding hard ReLU kinks at zero
- •Non-monotonicity: small negative dip around acts as a soft threshold that filters low-magnitude noise
- •Gradient flow: eliminates gradient shattering, ensuring stable second-order optimization and smooth loss landscapes
📐 Mathematical Derivations
Mathematical Comparison:
- GELU (Gaussian Error Linear Unit): . Can be viewed as a stochastic regularizer: the neuron's input is multiplied by 1 with probability and by 0 with probability . For large positive , ; for large negative , . At , and .
- SiLU / Swish: . When , .
- Loss Surface Smoothness: Li et al. (2018) demonstrated that ReLU's non-differentiable corner causes 'gradient shattering', where gradients of deep networks behave like high-frequency white noise. Smooth activations (GELU/SiLU) preserve gradient correlations across layers, stabilizing Transformer attention-MLP interactions.
- GELU (Gaussian Error Linear Unit): . Can be viewed as a stochastic regularizer: the neuron's input is multiplied by 1 with probability and by 0 with probability . For large positive , ; for large negative , . At , and .
- SiLU / Swish: . When , .
- Loss Surface Smoothness: Li et al. (2018) demonstrated that ReLU's non-differentiable corner causes 'gradient shattering', where gradients of deep networks behave like high-frequency white noise. Smooth activations (GELU/SiLU) preserve gradient correlations across layers, stabilizing Transformer attention-MLP interactions.
🏭 Production Trade-offs
Computational cost: Exact GELU involves the error function , which is computationally expensive on hardware. In practice, modern frameworks use the fast tanh approximation: or native hardware-fused CUDA kernels.
⚠️ Common Interview Pitfalls
- ✕Swapping GELU for ReLU during downstream fine-tuning of a pretrained Transformer, which severely degrades pretrained representation alignment
- ✕Using naive un-fused Python implementations of GELU that materialize multiple intermediate tensors in GPU memory
🎯 Interviewer Follow-up Inquiries
- ?What is 'gradient shattering' in deep networks, and why do smooth activations like GELU prevent it?
- ?How does the stochastic regularization interpretation of GELU relate to variational dropout?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
为什么 Transformer 用 GELU/SiLU 而不是 ReLU?
平滑激活在 0 附近可导、梯度更稳定,且实验上语言建模效果更好。
核心要点
- 四个原因:① 平滑性 ——ReLU 在 0 处不可导(次梯度),GELU/SiLU 处处可导(且 GELU 的导数连续);平滑的激活使损失景观更平滑,优化更稳定(尤其配合大学习率与深层网络)。② 负区间的小梯度 ——ReLU 在负区间梯度为 0(死亡风险 + 信息丢失);GELU/SiLU 在负区间有非零梯度(SiLU 的导数在负区间最小约 −0.1),保留了一定的负响应能力,无死亡问题。③ 非单调性 ——GELU 与 SiLU 在负区间是 非单调 的(在 x≈−0.75/−1.28 处有最小值),这种'先降后升'的形状提供更丰富的非线性(而 ReLU 是单调的)。④ 实验验证 ——在语言建模任务上,GELU/SiLU 一致优于 ReLU(BERT 用 GELU、GPT 系列用 GELU、LLaMA 用 SiLU);这一差异在小模型上不明显但在大模型上稳定存在。此外, 门控 FFN(SwiGLU)需要 SiLU 作为门控函数 ——这是 LLaMA FFN 的结构要求(见下题)。
- 实践要点:① 计算成本对比 ——ReLU 最便宜(一次比较);SiLU 需一次 sigmoid(约 4 次浮点运算);GELU 的精确式需 erf(较贵),故实践用 tanh 近似 :GELU(x)≈0.5x(1+tanh[√(2/π)(x+0.044715x³)]),只多几次乘法与一次 tanh。② 与归一化的配合 ——GELU/SiLU 的输出范围无界(与 ReLU 相同),仍需归一化层;但它们对初始化的敏感度略低于 ReLU(因为负区间不截断)。③ FLOPs 与内存 ——GELU/SiLU 的额外计算在总 FLOPs 中占比很小(FFN 的矩阵乘占主导),故性能损失可忽略。④ 其他现代激活 —— Squared ReLU (视觉模型)、 Mish 、 GeGLU (GELU 门控版)、 ReGLU (ReLU 门控版)——实验上门控版本(SwiGLU/GeGLU)普遍优于非门控,而门控函数的差异(SiLU vs GELU vs ReLU)影响较小。⑤ 实践建议 ——默认用 GELU(BERT/GPT 生态)或 SiLU(LLaMA 生态);两者差异很小,主要看预训练权重的兼容性(微调时应用与预训练相同的激活)。⑥ 注意 ——某些推理框架对特定激活有融合优化(fused GELU/SiLU),改用非标准激活可能失去这些优化。
- 平滑 → 二阶可导,利于优化
- 门控 FFN(SwiGLU)需要 SiLU
常见陷阱
- 微调时用与预训练不同的激活(性能下降)
- 认为 GELU 的精确式与 tanh 近似有显著差异