← All notes

ML algorithms · Core Concepts

Vanishing and Exploding Gradients

The gradient at layer $\ell$ is a product of terms across all later layers:

In deep networks, backprop multiplies many Jacobians together. If their magnitudes are consistently $<1$ the gradient vanishes; if $>1$ it explodes — either way, early layers fail to train.

The Mechanism

The gradient at layer $\ell$ is a product of terms across all later layers:

\[\frac{\partial L}{\partial \theta^{(\ell)}} \propto \prod_{k=\ell}^{L} W^{(k)} \cdot \text{diag}\big(\phi'(z^{(k)})\big)\]

Multiplying many factors:

  • each $< 1$ → product shrinks exponentially → vanishing (early layers get ~zero gradient, stop learning).
  • each $> 1$ → product grows exponentially → exploding (NaNs, divergence).

This is most acute in deep nets and RNNs over long sequences (see Recurrent Neural Networks).

Symptoms

  Vanishing Exploding
Loss Plateaus early, early layers frozen Spikes, NaN/Inf
Gradient norms → 0 in early layers → very large
Common with Sigmoid/tanh, deep stacks, long RNNs Poor init, high LR, RNNs

Fixes

For vanishing:

For exploding:

  • Gradient clipping (clip by norm/value) — the standard fix for RNNs.
  • Lower learning rate; proper init; normalization layers.

Why it shaped modern architectures

The vanishing-gradient problem is the reason plain deep MLPs/RNNs were historically hard to train — and why ResNets, LSTMs, normalization, and attention/Transformers (which shorten gradient paths) exist.