2.3 — Activation functions: sigmoid, tanh, ReLU, leaky ReLU, GELU
Goal: Know the formula, derivative, saturation regime, and typical use case of each common activation function.
1. Why this session exists
The activation function is the only nonlinearity in an MLP — everything else is affine. Picking is the difference between a net that trains easily (ReLU, GELU) and one that vanishes its own gradient before reaching layer 5 (deep sigmoid). This session collects the standard choices, their derivatives, and their training-time pathologies.
2. Sigmoid
Range . Symmetric around in the sense . Self-derivative identity from 1.2.
Saturation. with maximum at . For large, exponentially. This is the vanishing-gradient problem. Multiply saturated s and the gradient signal at the input shrinks by in the worst case — a depth-10 sigmoid net loses 6 orders of magnitude.
Where it appears. Output layer of binary classifiers (paired with BCE in 1.2). Almost never used as a hidden activation in modern networks.[5]
3. Hyperbolic tangent
Range . Centred at , which makes downstream weights see a zero-mean input — empirically slightly better trainability than sigmoid for the same architecture.
Saturation. with the maximum at . Same exponential decay for large, but the bound is instead of .
Where it appears. Old-school RNNs (LSTM cells use in the cell update). Largely supplanted by ReLU and friends in feed-forward networks.
4. ReLU
(Subgradient at ; see 0.9. Frameworks pick by convention.)
No saturation on the positive side. whenever , so gradient signal flows through without attenuation. This is the single biggest reason ReLU enabled training of very deep networks (Krizhevsky et al., 2012 onwards)[1].
Dying-ReLU pathology. A unit with on every training example has on every example and never receives a gradient — it is dead and stays dead. Bad initialisation or excessive learning rate can kill a substantial fraction of a layer's units.
Where it appears. The default hidden-layer activation for everything from CNNs to MLPs through the 2010s. Still common in Transformers, though increasingly displaced by GELU and SiLU.
5. Leaky ReLU and parametric ReLU
with small , typically .
The negative-side slope ensures no unit is ever truly dead: a leaky-ReLU unit always receives some gradient signal. Parametric ReLU (PReLU) makes a learned parameter, one per channel.
Where it appears. Vision backbones occasionally; less common in modern Transformer LMs.
6. GELU
where is the standard-normal CDF. Smooth everywhere (). A common closed-form approximation,
Derivative: , where is the standard-normal density.
Properties. Like ReLU for (slope ). Like zero for (with a small negative dip near ). Smooth instead of piecewise-affine. No dying units — gradients flow everywhere, just very small for .
Where it appears. The default activation in BERT, GPT-2, GPT-3, and most subsequent Transformer LMs. The smoothness probably helps optimisation marginally; the bigger reason is empirical — GELU consistently outperforms ReLU by a small margin in attention-heavy architectures.
7. Swish / SiLU
Smooth, non-monotonic (small dip near , like GELU). Discovered by Ramachandran et al. (2017)[3] through neural-architecture search; equivalent to the Swish activation of the same paper.
Where it appears. Many recent LLMs (Llama, Mistral, …) use SiLU in their feed-forward sub-blocks, often in the gated variant called SwiGLU.
8. Gated variants: GLU, GeGLU, SwiGLU
Modern LLM feed-forward blocks often gate the activation: compute two parallel linear projections and elementwise-multiply one through an activation,
This doubles the parameter count of the FFN block but consistently outperforms ungated versions (Shazeer 2020)[4]. We'll meet SwiGLU again in Chapter 8 when surveying modern LLM architectures.
9. Choosing one
A short heuristic.
- Output layer of a binary classifier. Sigmoid.
- Output layer of a multi-class classifier. Softmax.
- Output layer of a regression. Identity.
- Hidden layers, small / classical net. ReLU (or Leaky ReLU if dying-ReLU bites).
- Hidden layers, modern Transformer LM. GELU (canonical) or SwiGLU (state-of-the-art).
- Hidden layers, recurrent net. Tanh and sigmoid (legacy LSTM/GRU choices).
10. Takeaways
- Sigmoid and tanh saturate and cause vanishing gradients in deep networks.
- ReLU resolves vanishing gradients on the positive side but introduces dying units.
- Leaky ReLU, GELU, SwiGLU are "ReLU-like but more forgiving on the negative side".
- Modern default: GELU for Transformer FFN; SwiGLU for the latest LLMs.
(a) Compute , , , , at . Which has the largest derivative at zero?
(b) Show that . Use this to derive from .
(c) A ReLU MLP of depth , initialised so each unit has . Compute the expected fraction of fully-alive gradient paths from input to output. (Motivation for the He initialisation in 2.10.)
(d) For GELU, verify as and exponentially fast as . Roughly where is the minimum of on ?
References
- [1]
Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2012). "ImageNet classification with deep convolutional neural networks". NeurIPS. — AlexNet; popularised ReLU at scale (2.3).
↩ - [2]
Hendrycks, D., & Gimpel, K. (2016). "Gaussian Error Linear Units (GELUs)". arXiv:1606.08415 — GELU activation (2.3).
↩ - [3]
Ramachandran, P., Zoph, B., & Le, Q. V. (2017). "Searching for activation functions". arXiv:1710.05941 — Swish / SiLU (2.3).
↩ - [4]
Shazeer, N. (2020). "GLU variants improve Transformer". arXiv:2002.05202 — SwiGLU, GeGLU (2.3, will reappear in Cap 8).
↩ - [5]
Cobbe, K., Kosaraju, V., Bavarian, M., et al. (2021). "Training verifiers to solve math word problems" (GSM8K + verifiers). arXiv:2110.14168 — Verifier-based Best-of-N (9.15 §2.3).
↩