Part IX — Deep Learning Regularization, Normalization & Training Dynamics · Chapter 2

Vanishing & exploding gradients

Hook

Chapter 8 asked whether the forward pass survives depth. Backprop has to travel the exact same distance in reverse — does the gradient survive the trip?

Intuition
depth = 1 — gradient reaching the input ≈ 8.16e-2

This is a chain of sigmoid layers, same weight repeated at every layer. Drag depth up and watch the gradient reaching the input drop — not by a little each layer, but by roughly one more order of magnitude every single time.

Formalize

Going backward one layer multiplies the gradient by σ(z)w\sigma'(z)\cdot w — Chapter 5's chain rule, once per layer. Sigmoid's derivative peaks at exactly 0.250.25 and is smaller everywhere else:

Lhi1=Lhiσ(zi)w\frac{\partial L}{\partial h_{i-1}} = \frac{\partial L}{\partial h_i}\cdot \sigma'(z_i)\cdot w
  • LL — the loss.
  • hih_i and hi1h_{i-1} — the hidden layer outputs at layer ii and the layer just before it.
  • ziz_i — layer ii's raw weighted sum, before its activation.
  • σ(zi)\sigma'(z_i) — sigmoid's derivative at that point, capped at 0.250.25.
  • ww — the weight, repeated at every layer in this example.
  1. Repeated shrinking compounds to nothing

    Multiply a number smaller than 11 by itself ten times and it's gone.

  2. No weight scale rescues sigmoid

    Unlike Chapter 8's forward pass, there's no weight scale that fixes this — a bigger ww pushes zz further from 00, which makes σ(z)\sigma'(z) even smaller, not larger. Sigmoid's derivative is capped at 0.250.25, so this particular chain can only ever shrink going backward.

  3. Exploding gradients are the mirror image

    The mirror-image failure — exploding gradients — shows up instead in linear or ReLU layers with large weights, where nothing caps that per-layer factor above 11; it's the exact same backward multiplication as Chapter 8's forward one, just run in reverse.

Play
depth = 5 — gradient ≈ 2.29e-5 (dashed line = practically-zero threshold)

Slide from a shallow to a deep chain. Five layers in, the gradient is already down around 10510^{-5}; by ten layers it's down to about 2×1092\times10^{-9} — utterly unusable for updating a weight that far back. This is the vanishing gradient problem, and it's exactly why sigmoid fell out of favor as networks got deep.

Worked example

With weight w=3w=3:

  1. Check a single layer

    σ(z0)w\sigma'(z_0)\cdot w is at most 0.25×3=0.750.25\times3=0.75, so even the very first backward step shrinks the gradient.

  2. Follow it to depth 6

    Each backward step multiplies by that layer's own σ(zi)w\sigma'(z_i)\cdot w — and w=3w=3 pushes the forward pass toward saturation fast (the signal climbs from 0.50.5 toward 0.94\approx0.94 within a couple of layers, where σ\sigma' is tiny), so the six per-layer factors, computed layer by layer from the output backward, are all well under the depth-1 bound of 0.750.75:

    • 0.158, 0.158, 0.159, 0.167, 0.219, 0.447\approx0.158,\ \approx0.158,\ \approx0.159,\ \approx0.167,\ \approx0.219,\ \approx0.447

    Multiplying the output gradient (0.056\approx-0.056) by all six: 0.056×0.158×0.158×0.159×0.167×0.219×0.4473.6×106-0.056\times0.158\times0.158\times0.159\times0.167\times0.219\times0.447\approx-3.6\times10^{-6} — already roughly four orders of magnitude smaller than the loss gradient at the output.

  3. One layer further

    It drops under 10610^{-6} entirely: for practical purposes, that weight has stopped learning.

Checkpoint

Drag depth until the gradient reaching the input drops below 0.000001.

depth = 1 — gradient ≈ 8.16e-2
Move the depth slider to try it
Summary
Lhi1=Lhiσ(zi)w\frac{\partial L}{\partial h_{i-1}} = \frac{\partial L}{\partial h_i}\cdot \sigma'(z_i)\cdot w

A gradient that vanishes on the way back means the earliest layers of a deep network barely update at all — they're effectively frozen at their initial (random) values while only the last few layers learn anything. ReLU (short for Rectified Linear Unit)'s derivative doesn't have this ceiling (it's exactly 11 wherever the neuron is active), which is a large part of why it replaced sigmoid as the default choice once networks got deep.