Foundations to Frontier
Tests
E. Deep Learning › E0 Core Concepts (concept pages)

Gradient Clipping

Gradient clipping limits how large a gradient step can be. Clipping by global norm rescales all parameter gradients together whenever their combined norm exceeds a threshold, preserving the direction; clipping by value clamps each component separately, which changes the direction. Clipping is the standard defence against exploding gradients in recurrent networks and a routine safety measure in transformer training, where occasional huge gradients would otherwise wreck the run. This page covers both methods, why loss surfaces of recurrent models contain cliffs, how clipping turns the learning rate into a cap on step length, how to choose the threshold from gradient-norm statistics, and adaptive variants. In the exercises you implement both kinds of clipping and watch them rescue gradient descent on a recurrence cliff, then train recurrent networks at several learning rates with and without clipping.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A speed limiter for learning

Gradient descent takes a step proportional to the gradient. Usually that is sensible, but occasionally the gradient is enormous, perhaps because the network stepped near a cliff in the loss landscape, and one step throws the weights somewhere useless. Gradient clipping is a speed limiter: if the gradient is too large, it is scaled down before the step is taken.

Gradient clipping on a cliff
w from 0.60 to 1.08 · loss on a log scale · dashed blue: the target w* = 0.5^(1/30)
first gradient 272.5 · after 40 steps: w = 0.9772 · loss 4.97e-23
Without clipping, the steep wall above w* gives a huge gradient, and a single step throws w over the target onto the flat plateau at the left. There the gradient is tiny, so progress almost stops, or, with a bigger learning rate, w flies off entirely. Clipping caps each step at learning rate × threshold, so w walks down the wall and settles at w*. Recurrent networks have exactly such walls, where w^T changes violently (Pascanu et al., 2013).
Why engineers care

Training runs for sequence models of telemetry, and for large transformers, are long and expensive. A single exploding gradient can turn the loss into NaN hours into a run. Clipping by global norm is cheap insurance, and the logged gradient norms it produces are one of the best diagnostics of training health.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Gradient clipping…
  2. 2. Clipping mainly protects against…
  3. 3. Clipping by global norm keeps the gradient's…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Two kinds of clipping by hand

Global norm. Treat all parameter gradients g₁, …, g_k as one long vector, with norm ‖g‖ = √(Σᵢ ‖gᵢ‖²). If ‖g‖ > c, multiply every gᵢ by c/‖g‖.

  • For gradients (3, −4) and (12), the norm is √(9 + 16 + 144) = 13.
  • With c = 6.5 every component is halved, giving (1.5, −2) and (6). The direction is unchanged.

By value. Clamp each component to [−c, c]. With c = 1, (3, −4, 0.5) becomes (1, −1, 0.5), whose direction has changed: the cosine with the original is about 0.96.

Effect on the step. With global-norm clipping, an SGD step can never move the parameters further than lr × c. For lr = 0.5 and c = 1 that is 0.5, however wild the gradient.

Per-layer norm clipping clips each tensor separately. It is less common than global norm, which keeps the relative sizes across layers.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Global norm of (6, 8)?
  2. 2. Scale factor for norm 20 and threshold 5?
  3. 3. Which clipping changes the gradient's direction?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Cliffs in recurrent models

Why cliffs appear. A recurrent network applies the same weight at every time step. A one-unit linear RNN gives h_T = w^T x. Near |w| = 1, w^T changes violently: from w = 1.00 to 1.03, w³⁰ grows from 1 to 2.4. The loss surface then has a near-vertical wall beside a flat plateau (Pascanu, Mikolov and Bengio, 2013).

Exercise 1.

  • Global-norm clipping took (3, −4, 12) from norm 13 to 6.5, with the direction unchanged. Value clipping changed it.
  • On the 30-step recurrence starting at w = 1.03, the first gradient was 273. An unclipped step with lr = 0.001 leapt over the target to w = 0.757, onto the plateau where the gradient is tiny, and was still at w = 0.758 after 100 steps, with loss 0.250.
  • Clipping the gradient at 10 walked down the wall to w = 0.9772, where w³⁰ = 0.5 exactly.
loss.backward()
total_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)   # returns the pre-clip norm
optimizer.step()
🐍 Exercise 1: clipping and the recurrence cliff
Implement global-norm and value clipping, and compare plain and clipped gradient descent on the loss (w³⁰ − 0.5)², which has a cliff.
📝 Quick check (Level 2)
0/3 answered
  1. 1. In exercise 1, unclipped descent ended…
  2. 2. Why does w^T create cliffs?
  3. 3. clip_grad_norm_ returns…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory and practice

Clipping as a trust region. Global-norm clipping makes each step min(lr, lr·c/‖g‖) · g, an adaptive learning rate that shrinks only when the gradient is large. Zhang et al. (2020) showed that this provably accelerates training under a relaxed smoothness condition (local smoothness growing with the gradient norm), which fits language-model training better than the usual Lipschitz-gradient assumption.

Choosing the threshold.

  • Log the gradient norms for a while without clipping and set c near a high percentile, or simply use 1.0, the common default for transformers.
  • If clipping is active on most steps, it effectively lowers the learning rate, so lower the rate instead.

Variants.

  • Adaptive gradient clipping (AGC; Brock et al., 2021) clips each unit’s gradient relative to its weight norm, which enabled batch-norm-free ResNets.
  • Value clipping is cruder but cheap.
  • Clipping is applied after any gradient accumulation and before the optimiser step. With mixed precision, gradients must be unscaled first.

Limits. Clipping does not fix vanishing gradients, and it does not remove the cause of explosions (a bad initialisation, too high a learning rate, a faulty loss). It also interacts with adaptive optimisers. Adam already normalises per coordinate, but clipping still guards against rare spikes.

Interview questions.

  • Global norm or per-tensor clipping? Global norm keeps the relative contribution of each tensor and the overall direction. It is the default.
  • Gradient norms suddenly spike in a transformer run. What do you check? Data issues (corrupt batches), learning-rate warmup, loss scaling in mixed precision, and layer-norm placement. Clipping is a safety net, not a diagnosis.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Clipping acts like…
  2. 2. With mixed precision, before clipping you must…
  3. 3. AGC clips each unit's gradient relative to…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Does clipping make recurrent training safe?

History.

  • T. Mikolov (2012) used gradient clipping for recurrent language models.
  • R. Pascanu, T. Mikolov and Y. Bengio (2013) explained exploding gradients and cliffs and popularised norm clipping.
  • J. Zhang, T. He, S. Sra and A. Jadbabaie (2020) proved its benefits under relaxed smoothness.
  • A. Brock et al. (2021) introduced adaptive gradient clipping.

Exercise 2. A tanh RNN (16 units) summing 20-step sequences, trained with SGD and BPTT for 200 steps. Three seeds per setting.

clipping learning rate final loss per seed largest gradient norm seen
none 0.05 0.273, 0.345, 0.265 5.45, 8.13, 6.05
none 0.2 0.409, 0.919, 0.503 9.45, 22.3, 13.8
none 0.5 diverged ×3 2.5 × 10⁴, 4.0 × 10⁴, 2.3 × 10³
global norm ≤ 1 0.05 0.231, 0.234, 0.207 5.69, 5.85, 7.64
global norm ≤ 1 0.2 0.264, 0.344, 0.317 6.99, 8.85, 7.88
global norm ≤ 1 0.5 0.494, 0.577, 0.512 11.7, 18.0, 21.6
value ≤ 0.1 0.05 0.184, 0.205, 0.137 6.17, 7.91, 8.68
value ≤ 0.1 0.2 0.299, 0.345, 0.300 7.33, 8.60, 6.97
value ≤ 0.1 0.5 0.501, 0.671, 0.427 6.21, 237, 5.20
  • Without clipping, learning rate 0.5 diverged in every run. Gradient norms reached thousands before the loss became NaN.
  • With either clipping method no run diverged at any learning rate, even when an occasional gradient of 237 appeared.
  • Clipped runs also ended lower at learning rates 0.05 and 0.2. Clipping was active on many steps (the norms were well above 1 and 0.1), so it acted as a smaller, adaptive step size.
  • Clipping prevented failure but did not make high learning rates good. Learning rate 0.05 was still best overall, so clipping complements a sensible learning rate rather than replacing it.
🐍 Exercise 2 (research): clipping recurrent training
Train a tanh RNN with BPTT at learning rates 0.05, 0.2 and 0.5 with no clipping, global-norm clipping and value clipping; record final losses, divergences and gradient norms.

Gradient clipping in AI, robotics and aerospace

  • Sequence models for telemetry and trajectories: clipping is standard when training RNNs, LSTMs and transformers on long sensor sequences.
  • Reinforcement learning: policy-gradient and actor–critic methods (PPO, SAC) clip gradient norms, because reward-driven gradients are noisy and heavy-tailed.
  • Monitoring: logged pre-clip norms flag bad batches and instabilities in long training runs on shared compute.

References

  1. R. Pascanu, T. Mikolov, Y. Bengio (2013). “On the difficulty of training recurrent neural networks.” Proc. ICML.
  2. J. Zhang, T. He, S. Sra, A. Jadbabaie (2020). “Why gradient clipping accelerates training: a theoretical justification for adaptivity.” Proc. ICLR.
  3. A. Brock, S. De, S. L. Smith, K. Simonyan (2021). “High-performance large-scale image recognition without normalization.” Proc. ICML.
  4. I. Goodfellow, Y. Bengio, A. Courville (2016). Deep Learning, section 10.11. MIT Press.