Interactive Explainer
Vanishing & Exploding Gradients
Stack a hundred layers; multiply a hundred Jacobians; watch the gradient die — or blow up. A derivative panel demystifies the magic factor (0.25 for sigmoid, 1.0 for ReLU); a depth slider shows the multiplicative chain compound; a weight-scale slider flips the failure mode from vanishing to exploding. Then a misconceptions card cleans up what "vanishing" really means.
Why gradients matter at all
A neural network learns by subtracting a tiny multiple of $\partial \mathcal{L} / \partial W$ from each weight. If that gradient is essentially zero, the weight doesn't move and the layer doesn't learn. If it's astronomical, the weight overshoots and the loss explodes. Both failure modes have the same root cause: the gradient at layer 1 is a product of $L$ factors — one per layer downstream of it.
A product of numbers all less than 1 shrinks to zero. A product of numbers all greater than 1 blows up. The two failures — vanishing and exploding — are the same algebraic accident, with opposite signs.
The interactives below take you through the story in three layers: (1) what a single activation's derivative actually looks like; (2) what stacking the chain does; (3) what changing weight scale or activation function buys you.
Where the magic factor 0.25 comes from
Every backprop chain factor is the derivative of the layer's activation function evaluated at the pre-activation $z$. So we should start by looking at that derivative. Drag the input $z$ below; the dot on the left panel walks the activation function, and the dot on the right panel walks its derivative. The right panel is the one that ends up in the chain.
activation σ(z)
derivative σ'(z) — this is what backprop multiplies
Why sigmoid is doomed at depth: even at its most generous point ($z = 0$), $\sigma'(z) = 0.25$. Anywhere else, it's smaller. The chain rule is going to multiply $L$ of these numbers together — and they are all at most $0.25$.
- Sigmoid. $\sigma'(z) = \sigma(z)(1 - \sigma(z))$. Max value $0.25$ at $z = 0$; falls off to zero on either side.
- Tanh. $\tanh'(z) = 1 - \tanh(z)^2$. Max value $1.0$ at $z = 0$ — generous in the middle, saturates fast outside $|z| > 2$.
- ReLU. $\sigma'(z) = \mathbb{1}\{z > 0\}$ — either exactly 1 (active) or exactly 0 (dead). No saturation; a discrete switch.
The chain compounds — let's watch it
Now stack $L$ identical layers, each contributing the activation's best-case derivative to the chain. The input-layer gradient is the cumulative product of those $L$ factors. Below: the bar at the top is the output layer (where backprop starts); each bar below it is one more multiplication into the chain. The bottom bar is the input layer — the one whose gradient we care about when we say "vanishing".
Things worth trying:
- Set activation to sigmoid, depth $L = 10$: the chain is $0.25^{10} \approx 9.5 \times 10^{-7}$ — already in the "shrinking" band.
- Sigmoid at $L = 20$: $0.25^{20} \approx 9 \times 10^{-13}$ — below typical float-32 precision. The layer cannot learn.
- Switch to tanh: max derivative 1.0; at $L = 50$ the gradient stays at $1.0^{50} = 1$. But that's the best case — at any $|z| > 2$, $\tanh' \ll 1$ and the real chain still vanishes, just more slowly.
- Switch to ReLU with active fraction 100%: gradient stays at 1 forever. This is why ReLU broke the depth ceiling.
The other failure mode: exploding
We have been ignoring the weight matrices in the chain. The real per-layer factor is $W_\ell^\top \sigma'(z_\ell)$ — the activation derivative times the weight. If we initialise the weights too large, the product is greater than 1, and the chain blows up instead of dying.
advanced: weight initialisation scale (try this)
Move the slider below. Each layer now contributes $w \cdot \sigma'(z)$ to the chain. Values $w > 1$ flip the failure mode from vanishing to exploding.
- ReLU and friends (Glorot & Bengio 2010; Nair & Hinton 2010): $\sigma' \in \{0, 1\}$ instead of $\le 0.25$.
- Xavier / He initialisation: scale weights so $\mathrm{Var}(W_\ell) \cdot \mathbb{E}[\sigma'^2] = 1$ — the chain factor's variance is 1.0 by construction.
- Batch / Layer norm: rescale activations layer-by-layer so the per-layer Jacobian's spectral radius stays near 1.
- Residual connections (He et al. 2015): $y = f(x) + x$. The identity path adds 1 to the Jacobian; even if $f$ vanishes, $1$ does not.
- LSTM cell state / gated cells: the forget gate $\in [0, 1]$ multiplies the cell-state Jacobian directly; setting it near 1 gives a gradient highway across thousands of timesteps.
Three misconceptions, three corrections
Numbers to remember
Six facts that make the next time you look at a deep network's training curve immediately legible:
- $\sigma'_{\max} = 0.25$ (sigmoid), $1.0$ (tanh), $1$ (ReLU active).
- Sigmoid hits the gradient floor at depth $\approx \log_4 (1 / \text{floor}) \approx 20$ for floor $= 10^{-12}$.
- Xavier init scales weights as $\mathcal{N}(0, 1/\text{fan\_in})$; He init scales as $\mathcal{N}(0, 2/\text{fan\_in})$. The factor 2 is because ReLU kills half the activations on average.
- BatchNorm typically rescales layer outputs to $\mathrm{Var} = 1$; this is equivalent to "renormalise the Jacobian's spectrum to 1" at every layer.
- Residual connections add an identity to the Jacobian, so the lower bound on the gradient is $1$, not $0$.
- Gradient clipping caps the exploding direction; it doesn't fix vanishing. Use clipping for RNNs / transformers training stability; don't expect it to make a sigmoid-MLP train at depth 50.
Reading list
- Hochreiter, 1991 (Diploma thesis) — the original "long-term dependencies are hard" analysis.
- Bengio, Simard, Frasconi, 1994 — Learning long-term dependencies with gradient descent is difficult. The formal vanishing-gradient analysis.
- Glorot & Bengio, 2010 — Understanding the difficulty of training deep feedforward neural networks. Xavier init; the modern story starts here.
- Nair & Hinton, 2010 — Rectified linear units improve restricted Boltzmann machines.
- He et al., 2015 — Delving deep into rectifiers. He init; ReLU-aware variance.
- He et al., 2015 — Deep residual learning for image recognition. Residual connections.
- Pascanu, Mikolov, Bengio, 2013 — On the difficulty of training recurrent neural networks. Exploding gradients and clipping.
- Karpathy, "Yes you should understand backprop" — readable companion piece arguing students should know all of this without autograd.