๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #118

Exploding Gradient Problem

The exploding gradient problem is vanishing gradients' mirror image: instead of shrinking toward zero across layers, gradients grow uncontrollably large โ€” often ending training abruptly with NaN (not-a-number) losses.

The Mechanism

The same multiplicative chain-rule product responsible for vanishing gradients can just as easily explode if the factors involved are consistently greater than 1 instead of less than 1:

\[ \frac{\partial L}{\partial \mathbf{z}^{(1)}} \propto \prod_{l=1}^{L} \mathbf{W}^{(l)} \cdot \phi'(\mathbf{z}^{(l)}) \]

If weight matrices have large-magnitude entries (or, equivalently, large eigenvalues โ€” see Eigenvalues), this product can grow exponentially with depth, in exactly the same way the vanishing-gradient product shrinks exponentially.

Numerical Demonstration

Suppose every layer's combined weight-and-derivative factor is 1.5 instead of a fraction less than 1:

Depth (\(L\))\(1.5^L\)
5โ‰ˆ 7.6
10โ‰ˆ 57.7
20โ‰ˆ 3,325

By 20 layers, the gradient has grown over three thousand-fold โ€” resulting weight updates can be enormous, immediately destabilizing training, often producing NaN values as numbers overflow standard floating-point precision.

Where This Is Especially Common

Recurrent neural networks (see the RNN category, particularly RNN Exploding Gradient) are especially prone to this, since the same weight matrix is applied repeatedly across every time step of a long sequence โ€” effectively multiplying by the same matrix many times, which can compound very quickly if that matrix's eigenvalues exceed 1.

Recognizing It in Practice

SymptomLikely Cause
Loss suddenly becomes NaN or infGradients (or the resulting weight updates) have overflowed floating-point range
Loss oscillates wildly, occasionally spiking to huge values, without fully divergingGradients are large but not yet fully overflowing โ€” a milder form of the same problem
Weight values grow to unreasonably large magnitudes over trainingRepeated large updates, consistent with exploding gradients

Code โ€” Observing Gradient Explosion

import torch
import torch.nn as nn

# Deliberately large initial weights to provoke exploding gradients
layers = nn.Sequential(*[nn.Linear(10, 10) for _ in range(15)])
for layer in layers:
    nn.init.normal_(layer.weight, mean=0, std=3.0)   # unusually large initialization

x = torch.randn(1, 10, requires_grad=True)
output = layers(x)
loss = output.sum()
loss.backward()

for i, layer in enumerate(layers):
    print(f"Layer {i} weight grad norm:", layer.weight.grad.norm().item())
# Grad norms typically grow sharply for earlier layers with this setup

Common Mistakes

  • Assuming a sudden NaN loss always indicates a data problem (e.g. a bad label or corrupted input) โ€” exploding gradients are an equally common, purely optimization-side cause, and checking gradient norms before a NaN occurs can help distinguish the two.
  • Responding to instability only by lowering the learning rate โ€” while this can help, gradient clipping (next note) directly and more reliably addresses exploding gradients at their source.

Interview Relevance

Q: "Your training loss suddenly becomes NaN partway through training. What's a likely optimization-related cause, and how would you fix it?" Exploding gradients โ€” likely from weight matrices (especially in a recurrent architecture applying the same matrix repeatedly) whose combined effect across layers or time steps causes gradients to grow exponentially, eventually overflowing floating-point precision. Gradient clipping (capping the gradient's norm before the weight update) is the standard, direct fix.

Practice Question

Why are recurrent neural networks especially prone to exploding gradients compared to typical feedforward networks of similar depth?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Exploding Gradient Problem โ€“ FAQs

Quick answers about learning Exploding Gradient Problem in Deep Learning.

This free note from CodingNow 2.0 explains Exploding Gradient Problem in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Exploding Gradient Problem, is 100% free with no signup required.
With focused practice, most students grasp Exploding Gradient Problem in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now