AlgoInsight

Q&A

What is the exploding gradient problem?

By AlgoInsight Team2026-06-07

Overview

Neural networks learn via backpropagation, which multiplies gradients layer by layer using the chain rule. If those multiplicative factors are consistently greater than 1, the gradient grows exponentially with depth - it "explodes." The resulting massive updates throw the weights wildly off course, and training diverges. (If the factors are consistently less than 1, you get the opposite: vanishing gradients.)

graph LR
    A[Loss] -->|backprop| B[Layer N gradient]
    B -->|× weight| C[Layer N-1]
    C -->|× weight| D[Layer ...]
    D -->|repeated × > 1| E[Gradient grows exponentially]
    E --> F[Huge weight updates → NaN, divergence]
    style E fill:#1e1b4b,color:#fff

Why It Happens

Backpropagation applies the chain rule, so a gradient flowing back through L layers is a product of L terms (weight matrices and activation derivatives). If the relevant values (e.g. weight magnitudes, the spectral norm of recurrent matrices) are > 1, repeated multiplication blows up:

gradient ∝ w^L      → if w > 1, this explodes as L grows

Recurrent Neural Networks (RNNs) are especially prone because the same weight matrix is applied at every time step, so a long sequence is like a very deep network sharing weights.

Symptoms

Solution 1: Gradient Clipping (the standard fix)

Cap the gradient's magnitude before applying the update. Clip by norm rescales the whole gradient vector if its norm exceeds a threshold:

# PyTorch
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
# TensorFlow / Keras
optimizer = tf.keras.optimizers.Adam(clipnorm=1.0)   # or clipvalue=0.5

Clipping preserves the gradient's direction while limiting its size, keeping updates stable.

Other Mitigations

Exploding vs Vanishing Gradients

Exploding Vanishing
Cause Multiplicative factors > 1 Factors < 1
Effect Gradients/weights blow up; NaN Gradients shrink to ~0; no learning
Symptom Unstable loss, NaN Loss stalls, early layers don't update
Common fix Gradient clipping ReLU, residual connections, LSTM gates, good init

Both stem from the same chain-rule multiplication; the direction of the imbalance determines which one you get.

Why This Matters in Interviews

ML/deep-learning interviews use this to test understanding of backpropagation and training stability. Be able to explain why it happens (chain-rule product of many terms > 1, worsened by weight sharing in RNNs), the symptoms (NaN/divergent loss), and the primary fix (gradient clipping, by norm or value). Contrasting it with vanishing gradients and naming architectural remedies (LSTM, residual connections, normalization, good initialization) signals depth.

Common Pitfalls

References

More answers