Exploding gradient problem

Also known as: exploding gradients

On this page 7

The exploding gradient problem is a training instability in which the gradients of the loss grow exponentially as they are propagated backward through many layers or time steps, producing excessively large weight updates that destabilize or destroy the model. In the training of neural networks in artificial intelligence, it is the mirror image of the vanishing gradient problem, and practitioners often refer to it simply as exploding gradients.

Training with gradient descent assumes that a small step along the negative gradient reduces the loss. When gradient norms reach thousands or millions, that assumption fails: a single update can overshoot into a region of the loss landscape where the loss is enormous or numerically undefined.

Why gradients explode during backpropagation

Gradients explode because backpropagation multiplies many Jacobian matrices together, one per layer or time step. If the typical magnitude of these factors exceeds one, the product grows roughly exponentially with depth. A factor of only 1.1 repeated across 100 layers already amplifies the signal by more than 13,000 times.

In recurrent neural networks, the same weight matrix is applied at every time step, so the gradient through a sequence of length T contains that matrix raised to roughly the power T. When its largest singular value exceeds one, long sequences produce explosive gradients. This analysis, formalized in the early 1990s and revisited in work on training recurrent networks, explains why plain RNNs were notoriously hard to train.

The table below lists the main factors that push gradient magnitudes upward.

CauseMechanismWhere it appears
Poor weight initializationLayer gains above one compound with depthDeep feedforward and convolutional networks
Recurrent weight reuseSame matrix multiplied across many stepsVanilla RNNs on long sequences
High learning rateLarge steps push weights into steep regionsAny architecture, especially early in training
Missing normalizationActivations drift and scale up layer by layerDeep networks without LayerNorm or BatchNorm
Low-precision arithmeticOverflow in FP16 turns large values into infinityMixed-precision training without loss scaling
Main causes of exploding gradients

Symptoms and how to detect them

The most reliable diagnostic is to log the global gradient norm at every step. A healthy run shows a norm that fluctuates within a stable band; an exploding run shows sudden jumps of one or more orders of magnitude, often followed by a loss spike.

SymptomWhat it indicatesSeverity
Loss becomes NaN or infinityNumerical overflow in weights or activationsRun is usually unrecoverable
Sharp loss spikeOne update overshot the stable regionOften recoverable with rollback
Gradient norm jumps 10x or moreInstability is starting to buildEarly warning signal
Weights grow without boundRepeated oversized updatesModel quality degrades steadily
Oscillating lossUpdates bounce across a steep valleyTraining stalls without converging
Observable symptoms of exploding gradients

Not every loss spike comes from exploding gradients. Corrupted data batches, a learning rate schedule that rises too quickly during learning rate warmup, or optimizer state issues can produce similar curves, so the gradient norm log should be checked before applying a fix.

Techniques that prevent exploding gradients

Modern training stacks combine several complementary defenses, because no single technique removes the risk across all architectures and scales.

Gradient clipping is the most direct safeguard. Clipping by global norm rescales the full gradient vector whenever its norm exceeds a threshold, preserving its direction while capping its size. A threshold of 1.0 is a common default in large language model training, although values between 0.5 and 5 are used depending on the model and optimizer.

TechniqueHow it helpsTrade-off
Gradient clipping by normCaps update size, keeps directionThreshold needs tuning; can hide root causes
Careful initializationXavier or He schemes keep layer gain near oneMust match the activation function
Normalization layersLayerNorm or BatchNorm stabilize activationsExtra computation; placement matters
Residual connectionsProvide identity paths for gradient flowRequires careful scaling in very deep stacks
Gated recurrent unitsLSTM and GRU gates regulate gradient flowMore parameters than vanilla RNNs
Learning rate warmupStarts with small steps while statistics settleSlightly slower early progress
Dynamic loss scalingAvoids FP16 overflow and underflowSkips steps when overflow is detected
Countermeasures and their trade-offs

Adaptive optimizers such as the Adam optimizer and AdamW normalize updates by a running estimate of gradient variance, which dampens the effect of large gradients. They do not eliminate the problem, however, because a sudden outlier gradient can still corrupt the moment estimates and trigger instability several steps later.

Exploding versus vanishing gradients

Both problems arise from the same multiplicative structure of backpropagation, but they fail in opposite directions and call for different responses.

AspectExploding gradientsVanishing gradients
Gradient magnitudeGrows exponentially with depthShrinks exponentially with depth
Visible effectLoss spikes, NaN values, divergenceEarly layers stop learning
DetectionEasy, because failures are abruptHarder, because training merely stalls
Primary fixClipping and lower learning ratesResidual paths, gating, better activations
Exploding and vanishing gradients compared

Because exploding gradients fail loudly, they are easier to detect but more destructive: a single bad step can waste days of compute on a large run if checkpoints are infrequent.

Exploding gradients in large-scale training

In large transformer models, instability rarely looks like the textbook recurrent case. It tends to appear as intermittent loss spikes late in pretraining, linked to attention logit growth, outlier activations or interactions between high learning rates and optimizer state.

  • Frequent checkpointing allows teams to roll back to a point before the spike and skip the offending data batches.
  • QK normalization and logit soft-capping limit the growth of attention scores.
  • BF16 precision offers a wider exponent range than FP16 and reduces overflow risk.
  • Monitoring per-layer norms helps identify which block starts the divergence.

The idea in brief

A neural network learns by making many small corrections. Each correction is based on a signal that says which way to adjust. Sometimes this signal gets multiplied again and again as it travels through the network. It becomes huge.

Imagine a microphone placed too close to a speaker. A small sound turns into a loud screech. Exploding gradients work the same way. The fix is to limit the signal, start learning gently and design the network so that the signal stays at a sensible level.

A practical scenario

A machine learning team trains a 1.3-billion-parameter language model on a cluster of GPUs. Around step 42,000, the loss jumps from 2.4 to 7.9 and then becomes NaN. The gradient norm log shows a rise from about 0.8 to over 300 within three steps.

The team restores the checkpoint saved at step 41,500, enables global gradient clipping at 1.0, switches from FP16 to BF16 and skips the two data batches that coincided with the spike. They also lower the peak learning rate by about 20 percent.

The run continues past step 42,000 without incident, and the gradient norm stays below 1.5 for the rest of training. The incident costs about half a day of compute instead of the full restart that would have been needed without regular checkpoints.