AI / ML Theory
Chapter 2 · 18 min

2.9 — Vanishing and exploding gradients

Goal: Explain the multiplicative chain of g'(z) along the backward pass and why sigmoid kills depth while ReLU survives.

1. Why this session exists

The backprop recursion from 2.6,

δ(ℓ)=((W(ℓ+1))⊤δ(ℓ+1))⊙g′(ℓ)(z(ℓ)),\delta^{(\ell)} = \bigl((W^{(\ell+1)})^\top \delta^{(\ell+1)}\bigr) \odot g'^{(\ell)}(z^{(\ell)}),

is a product over layers. Whatever happens at the top gets multiplied by a chain of Jacobian factors as it propagates to the bottom. If those factors are systematically <1< 1, the gradient signal vanishes before reaching the input layers. If they are systematically >1> 1, it explodes. Either way, training stalls — vanishing because layer-1 weights stop receiving meaningful updates; exploding because the optimiser overshoots into noise.

This session quantifies the chain and pins down why ReLU saved deep learning circa 2012.[1, 2]


2. The multiplicative chain

Iterating the recursion from layer LL down to layer 11 and ignoring the matrix factors for a moment (treating them as "approximately norm-preserving" — we'll fix this in 2.10),

∥δ(1)∥  ≈  ∥δ(L)∥⋅∏ℓ=1L−1∣g′(ℓ)(z(ℓ))∣typical,\lVert \delta^{(1)} \rVert \;\approx\; \lVert \delta^{(L)} \rVert \cdot \prod_{\ell = 1}^{L - 1} \bigl|g'^{(\ell)}\bigl(z^{(\ell)}\bigr)\bigr|_{\text{typical}},

with the product running from layer 11 to layer L−1L - 1 and "typical" meaning the geometric mean of the per-unit derivatives along the path. The size of the gradient at layer 1 is set by this product.

If ∣g′∣≈c<1|g'| \approx c < 1 at every layer, then ∥δ(1)∥≈cL−1∥δ(L)∥\lVert \delta^{(1)} \rVert \approx c^{L-1} \lVert \delta^{(L)} \rVert. For sigmoid, c≤1/4c \le 1/4, so depth L=10L = 10 kills the gradient by a factor of (1/4)9≈4×10−6(1/4)^9 \approx 4 \times 10^{-6}. Vanishing gradient.

If ∣g′∣≈c>1|g'| \approx c > 1, then cL−1c^{L-1} blows up. Exploding gradient.

The matrix factors (W(ℓ+1))⊤(W^{(\ell+1)})^\top can rescue or destroy this in either direction — which is why we'll spend all of 2.10 on initialisation.


3. Sigmoid: vanishing by construction

Recall from 2.3 that σ′(z)=σ(z)(1−σ(z))≤1/4\sigma'(z) = \sigma(z)(1 - \sigma(z)) \le 1/4, with equality only at z=0z = 0. At typical operating points z=±2z = \pm 2, σ′(z)≈0.10\sigma'(z) \approx 0.10. So the multiplicative factor at each layer is at most 0.250.25, and usually around 0.100.10.

For depth L=10L = 10, ∥δ(1)∥/∥δ(L)∥∈(10−10, 10−6)\lVert \delta^{(1)} \rVert / \lVert \delta^{(L)} \rVert \in (10^{-10},\, 10^{-6}), depending on operating points. The gradient at layer 1 is not getting useful training signal. This is the entire reason sigmoid hidden layers were abandoned around 2010–2012.

Tanh is slightly better: tanh⁡′(z)∈(0,1]\tanh'(z) \in (0, 1], with maximum 11 at z=0z = 0. But for ∣z∣>2|z| > 2 the derivative is again close to 00, and the chain still shrinks. Tanh deep networks were trainable but slow; the AlexNet jump in 2012 was largely about replacing them with ReLU.


4. ReLU: vanishing is gone (on the active side)

For ReLU, ReLU⁡′(z)∈{0,1}\operatorname{ReLU}'(z) \in \{0, 1\}. On the active side (z>0z > 0), the multiplicative factor is exactly 11 — gradient passes through undamped. On the dead side (z<0z < 0), it is 00 — gradient is killed completely for that unit.

The key statistical fact: a path from layer 1 to the loss is alive iff every ReLU along the way is active. If we initialise weights so each ReLU is active with probability 1/21/2 (which is what symmetry around zero gives), then a single fixed path of length LL is alive with probability 2−L2^{-L}. But there are exponentially many paths, and they share weights — so the effective gradient at layer 1 stays bounded as LL grows, provided the weight matrices have the right scale.

That "right scale" is the He initialisation of 2.10: Var⁡(Wi,j(ℓ))=2/dℓ−1\operatorname{Var}(W^{(\ell)}_{i, j}) = 2 / d_{\ell - 1}. It compensates for the factor of 1/21/2 from dead ReLUs.


5. The exploding side

For unbounded activations or aggressively scaled weights, the per-step product can be >1> 1, and the gradient explodes exponentially. Classical example: linear RNNs (no nonlinearity, weights unbounded) — the backprop product becomes the matrix power (W⊤)T(W^\top)^T for TT time steps, and the largest eigenvalue of WW controls everything.

The clean clinical fix is gradient clipping: after computing the gradient, scale it down to a fixed norm threshold cc,

g←g⋅min⁡ ⁣(1,c∥g∥).g \leftarrow g \cdot \min\!\left(1, \frac{c}{\lVert g \rVert}\right).

This caps the maximum step size in parameter space and prevents the optimiser from veering off into infinity. Standard practice in RNNs and Transformers; clip threshold is typically c=1.0c = 1.0.


6. What this implies for activation choice

  • Avoid pure sigmoid / tanh in hidden layers. Their derivatives are bounded above by 1/41/4 and 11 respectively, and saturate fast. The gradient chain shrinks geometrically with depth.
  • Prefer ReLU / GELU / SiLU. Their derivatives are bounded by ∼1\sim 1 everywhere on the positive side and don't go far above 11 on the negative side. The chain is roughly preserved.
  • Use the right initialisation (2.10) so the matrix factors don't unilaterally rescale the chain.
  • Use residual connections (Chapters 4, 5) — they bypass the multiplicative chain entirely by adding the input to the output, a(ℓ)=a(ℓ−1)+f(ℓ)(a(ℓ−1))a^{(\ell)} = a^{(\ell-1)} + f^{(\ell)}(a^{(\ell-1)}).

The combination of "good activation + good init + residuals + layernorm" is essentially the entire story of how networks with 50–100+ layers became routine in the 2015–2020 stretch.


7. Takeaways

  • The backprop signal at layer 11 is a product of per-layer Jacobian norms.
  • Sigmoid: typical factor ≤1/4\le 1/4 → vanishing for L>10L > 10.
  • ReLU: typical factor =1= 1 on the active side → vanishing solved (modulo dying units).
  • Exploding: rare with bounded activations, common in unbounded ones; fix with gradient clipping.
  • Modern recipe: ReLU/GELU + He init + residual connections + layernorm.

Self-test

(a) Compute σ′(z)\sigma'(z) at z=0,1,2,3z = 0, 1, 2, 3. For a sigmoid hidden net of depth L=8L = 8 with typical operating point z=2z = 2, estimate the ratio ∥δ(1)∥/∥δ(L)∥\lVert \delta^{(1)} \rVert / \lVert \delta^{(L)} \rVert.

(b) For ReLU at depth L=10L = 10, assume each unit is active with probability 1/21/2. What is the probability that a single fixed path from input to output is alive end-to-end? With dℓ=256d_\ell = 256 units per layer, what is the expected number of live paths?

(c) Show that scaling g←g⋅min⁡(1,c/∥g∥)g \leftarrow g \cdot \min(1, c / \lVert g \rVert) ensures ∥gnew∥≤c\lVert g_{\text{new}} \rVert \le c and preserves direction. Is the resulting update step still a descent direction for the original loss?

(d) Residual connection: a(ℓ)=a(ℓ−1)+f(ℓ)(a(ℓ−1))a^{(\ell)} = a^{(\ell-1)} + f^{(\ell)}(a^{(\ell-1)}). Compute ∂a(ℓ)/∂a(ℓ−1)\partial a^{(\ell)} / \partial a^{(\ell-1)} and explain why this Jacobian "always contains II as a term", letting gradient flow through even when ff saturates.


References

  1. [1]

    Hochreiter, S. (1991). Untersuchungen zu dynamischen neuronalen Netzen. Diploma thesis, TU Munich. — First vanishing-gradient analysis in RNNs (2.9).

    ↩
  2. [2]

    Bengio, Y., Simard, P., & Frasconi, P. (1994). "Learning long-term dependencies with gradient descent is difficult". IEEE Transactions on Neural Networks, 5(2), 157–166. — Formal vanishing-gradient analysis (2.9).

    ↩