What are vanishing and exploding gradients?
Vanishing and exploding gradients are two failures of training deep networks, where the gradient shrinks toward zero or grows without bound as backpropagation carries it from the output back toward the input. With vanishing gradients, the early layers barely change and the network learns slowly or not at all. With exploding gradients, updates become huge, the loss jumps around, and the weights can overflow to NaN.
Both problems come from the same cause: the gradient at an early layer is a product of many per-layer factors, and a long product of numbers below 1 shrinks while one above 1 grows. They held back deep feed-forward networks and recurrent networks for years. The modern fixes are ReLU-family activations, careful weight initialization, normalization layers, residual connections, gated RNN cells such as LSTMs, and gradient clipping.
Key ideas
- An early layer's gradient is a product of one factor per later layer.
- Factors consistently below 1 make gradients vanish; factors above 1 make them explode.
- Vanishing gradients leave early layers almost untrained while later layers keep learning.
- Exploding gradients cause erratic loss, huge updates, and NaN values.
- Residual connections add an identity path that carries the gradient past each block.
- Gradient clipping rescales an oversized gradient while keeping its direction.
Short takes on this topic
Common questions
How do I tell if my network has vanishing or exploding gradients?
Log the gradient norm of each layer during training. Vanishing gradients show up as norms in early layers that are orders of magnitude smaller than in later layers, with a loss that plateaus early. Exploding gradients show up as sudden spikes in the loss or gradient norm, or as NaN values.
Do vanishing gradients still happen in modern networks?
Far less often. ReLU-family activations, He or Xavier initialization, normalization layers, and residual connections together keep gradients in a workable range even in networks with hundreds of layers. Plain recurrent networks on long sequences remain vulnerable, which is one reason transformers replaced them.
Why do RNNs suffer more than feed-forward networks?
Backpropagation through time multiplies by the same recurrent weight matrix once per time step. Over 100 steps, a matrix whose largest singular value is slightly below 1 shrinks the gradient toward zero, and one slightly above 1 can make it explode. LSTMs and GRUs add gated, additive paths to avoid this.
What value should I use for gradient clipping?
A max norm of 1.0 is a common default for transformers and RNNs. A good value sits slightly above the gradient norms seen in normal training, so clipping only triggers on outliers. Logging gradient norms for a few hundred steps shows where that is.
Related explainers
- Backpropagation — How networks learn from their mistakes
- Activation Functions — Why networks need non-linearity to learn
- Weight Initialization — Xavier, He, and why random weights matter
- Skip Connections & ResNets — Residual learning and why deeper networks work
- LSTMs — Gates and memory cells for long sequences
- Batch Normalization — Why normalizing activations speeds up deep network training
References
- Bengio, Simard & Frasconi (1994), "Learning long-term dependencies with gradient descent is difficult", IEEE Transactions on Neural Networks
- Glorot & Bengio (2010), "Understanding the difficulty of training deep feedforward neural networks", AISTATS
- Pascanu, Mikolov & Bengio (2012), "On the difficulty of training Recurrent Neural Networks"
- He et al. (2015), "Deep Residual Learning for Image Recognition"
- Zhang et al., "Dive into Deep Learning", section 5.4: Numerical Stability and Initialization
Last updated