π Vanishing & Exploding Gradients Problems
Descriptionβ
< The chain rule >β
The chain rule differentiates a function nested inside another. If depends on , and depends on :
For a longer chain, consider:
The derivative travels back through every step:
- Differentiate the outside while keeping the inside expression
- A longer nested function creates one derivative factor per layer
Neural-network layers form the same kind of nested chain; backpropagation repeatedly applies this rule to determine how every parameter affects the loss.
The vanishing-gradient problem is a consequence of repeatedly applying the chain rule across many layers or time steps: every application introduces another derivative factor, and those factors are multiplied together. If they are usually smaller than 1, the gradient shrinks toward zero; if they are usually larger than 1, it can explode.
< What are they? >β
Backpropagation sends a loss signal from the output back through every layer. For an earlier representation , that signal contains a chain of Jacobians:
In a deep networkβor a recurrent network unrolled across many time stepsβrepeated multiplication is the problem:
| Problem | What happens | Result |
|---|---|---|
| Vanishing gradients | Typical factors are smaller than 1, so the signal shrinks toward zero. | Early layers barely update; long-range dependencies are hard to learn. |
| Exploding gradients | Typical factors are larger than 1, so the signal grows without bound. | Updates become unstable; loss or gradients may become inf / NaN. |
This is about the backward signal, not merely a high or low loss. A model can have a finite loss while some layers receive almost no useful gradient.
Key pointsβ
< When it happens >β
Deep networks and recurrent networks repeatedly multiply local derivatives. The same basic mechanism appears in several practical settings.
< Vanishing gradients >β
| Scenario | Why gradients vanish | Typical fix |
|---|---|---|
Deep sigmoid / tanh networks | Sigmoid derivatives are at most ; repeated factors such as erase early-layer signal. Tanh also saturates away from zero. | ReLU-family activations and initialization matched to the activation |
| Vanilla RNNs on long text or time series | The recurrent Jacobian is multiplied once per time step. Typical factors below erase distant context. | LSTM or GRU gates |
| Very deep networks without residual paths | Even non-saturating activations can gradually shrink a signal across hundreds of layers. | Residual connections, normalization, and suitable initialization |
| Dead or saturated units | A ReLU held below zero has a local derivative of ; saturated sigmoid / tanh units have near-zero derivatives. | Leaky ReLU or ELU, plus He initialization |
| Historical deep autoencoders / belief networks | Bottom layers often received too little end-to-end signal from random initialization. | Modern residual designs, normalization, and initialization replaced much layer-wise pretraining |
< Exploding gradients >β
| Scenario | Why gradients explode | Typical fix |
|---|---|---|
| Very deep networks with poor initialization or no normalization | Repeated Jacobians with large singular values amplify the backward signal layer by layer. | He / Kaiming initialization for ReLU, normalization, and residual paths |
| Vanilla RNNs on long sequences | The same recurrent matrix is reused at every time step; factors above grow exponentially across time. | LSTM or GRU, global-norm clipping, and sometimes truncated BPTT |
| Early GAN training | An imbalanced generator and discriminator can create sharp, unstable generator updates. | Balanced learning rates, gradient penalties or Wasserstein-style objectives, and clipping when needed |
| Regression with extreme outliers | MSE gradients grow with the prediction error, so one extreme target can dominate an update. | Target transforms, outlier handling, or Huber loss |
| Policy-gradient reinforcement learning | High-variance or unusually large returns can create large policy-gradient updates. | Global-norm clipping, advantage normalization, or PPO's clipped objective |
Long, deep Transformer training can also produce early gradient spikes, so global-norm clipping is common in training recipes.
< How to diagnose it >β
Exploding gradients are usually easier to spot: norms spike, the loss becomes unstable, or values become inf / NaN.
Vanishing gradients are subtler: they remain finite but can be too small to train early layers. Slow learning, a flat loss,
or poor long-range learning can also have other causes.
- Log raw gradient norms layer by layer over time (for example, ), not only parameter updates:
momentum or Adam can make updates look nonzero even when the current gradient is tiny.
- Early-layer norms consistently far below later-layer norms suggest vanishing.
- Sudden huge norms or non-finite values suggest exploding.
- Check activation statistics. Saturated
sigmoidortanhunits have derivatives close to zero and can starve earlier layers of gradient. - Watch the loss curve, but do not rely on it alone: a flat loss can also be caused by a bad learning rate, a disconnected computation graph, or insufficient model capacity.
< How to reduce the problem >β
< Vanishing gradients >β
- Use an appropriate activation: ReLU, Leaky ReLU, ELU, and GELU avoid the saturated
sigmoid/tanhderivative in their responsive regions - Match the initialization: Xavier / Glorot for
tanh-like activations and He / Kaiming for ReLU-like activations - Add stable paths: residual connections give gradients an identity shortcut; BatchNorm and LayerNorm keep activation scales better conditioned
- Use gated sequence models: LSTM and GRU cells preserve long-range signal more reliably than a vanilla RNN
< Exploding gradients >β
- Clip the global norm: cap an unusually large gradient after
backward()and beforeoptimizer.step(); this is the first-line defense against spikes - Control scale from the start: suitable initialization, normalization, and residual paths reduce harmful amplification
- Use a smaller learning rate or warmup: this limits destructive early parameter updates while optimizer statistics stabilize
- Stabilize the task-specific signal: use robust losses or target transforms for outliers; use gates, truncated BPTT, advantage normalization, PPO, or GAN-stabilizing objectives where appropriate
< Why Batch Normalization helps >β
Batch normalization helps indirectly: it normalizes a layer's activations, not the gradients themselves. For a mini-batch, it computes:
- It keeps activation scales more consistent between layers, making one layer less likely to greatly amplify or shrink the signal.
- For
sigmoidandtanh, it keeps inputs nearer their responsive range rather than saturated regions, where derivatives are near zero and gradients vanish. - More stable activation scales make optimization better conditioned, so gradients and parameter updates are less erratic and larger learning rates can often be used safely.
- The learnable and let the model choose an appropriate scale and offset instead of permanently forcing zero mean and unit variance.
The Batch Normalization page covers the rest β the training/inference split, the PyTorch layers, and batch-size sensitivity.
BatchNorm is not a complete cure: it cannot guarantee that gradients will never vanish or explode. Residual connections, suitable initialization, gated RNNs, and gradient clipping can still be important.
< A practical response >β
- Record layer-wise gradient norms and check for
NaN/infvalues. - For explosions, clip the global norm after
backward()and before the optimizer step; then lower the learning rate if needed. - For vanishing gradients, revisit initialization, activation functions, and architectureβespecially residual paths or gates.
- With
fp16, use loss scaling (orbf16) to avoid numerical underflow. This does not solve mathematical vanishing gradients.
< Related ideas >β
- Gradient Descent explains how gradients become parameter updates.
- Data Normalization helps make optimization better conditioned, but it is not a substitute for stable gradient paths through a deep model.
Crash courseβ
- Vanishing Gradients
- Vanishing AND Exploding Gradient Problem Explained
- Vanishing/Exploding Gradients
Examplesβ
π‘ The most famous real-world example:
- In the early 2000s, researchers tried to train deep RNNs for speech recognition but hit a wall. The gradients would vanish after ~10-15 time steps, so the model could only use the last few milliseconds of audio, ignoring the broader context. The invention of LSTM (by Hochreiter & Schmidhuber in 1997, but popularized later) was a direct response to thisβit used additive cell states to keep the gradient magnitude stable indefinitely, finally allowing RNNs to process entire sentences and audio clips.