Foundation Lab

Gradient Clipping & Explosion Prevention

Essential for training LLMs—without clipping, gradients explode on certain batches

Concept 59 of 100OptimizationPhase 3
#59Grad ClipOptimization
key equation
g~=min⁡(1,c/∥g∥)⋅g\tilde{g} = \min(1, c / \|g\|) \cdot g
Reading map and next steps

Selected Foundation Object

Keep the equation fixed; move through the evidence.

Concept 59 of 100Grad ClipOptimization / Phase 3: Optimization & generalization
Current question

Clipping preserves gradient direction while bounding step size—you still go the right way

g~=min⁡(1,c/∥g∥)⋅g\tilde{g} = \min(1, c / \|g\|) \cdot g
PredictionCommit before tracing the equation.

Ask what should change under a concrete input, then trace that expectation through the equation.

EvidenceCompare the equation and source.

Use the key equation and canonical papers as the available witnesses, without implying that a runnable panel exists.

InvariantName what survives notation changes.

The useful learning product is the reusable mechanism you can carry into another model, paper, or engineering tradeoff.

Next moveContinue through the atlas.

Use prerequisites, dependents, and semantic links to repair the next gap without leaving the object behind.

Why It Matters for Modern Models

  • Essential for training LLMs—without clipping, gradients explode on certain batches
  • GPT-3 used gradient clipping of 1.0; it's a standard hyperparameter in all LLM training
  • Explains why very deep networks (100+ layers) require careful architectural choices

What Tutorials Skip

What is still poorly explained in textbooks and papers:

  • Clipping preserves gradient direction while bounding step size—you still go the right way
  • Bad batches (outliers) cause gradient spikes; clipping prevents single batches from destabilizing training
  • Gradient norm is a useful diagnostic: spikes often precede training instabilities

Visualization Status

Core Math (Optional Deep Dive)

If you want intuition first, start with the key equation and cited sources. Come back here for the full walkthrough.

Key Equation
g~=min⁡(1,c/∥g∥)⋅g\tilde{g} = \min(1, c / \|g\|) \cdot g

Gradient norm clipping: Scale gradient if norm exceeds threshold:

g~={gif ∥g∥≤cc⋅g∥g∥if ∥g∥>c\tilde{g} = \begin{cases} g & \text{if } \|g\| \leq c \\ c \cdot \frac{g}{\|g\|} & \text{if } \|g\| > c \end{cases}

Value clipping (per-coordinate): g~i=clip(gi,−c,c)\tilde{g}_i = \text{clip}(g_i, -c, c)

Why gradients explode: In deep networks, gradients are products of Jacobians:

∂L∂W1=∂L∂hL∏l=2L∂hl∂hl−1∂h1∂W1\frac{\partial L}{\partial W_1} = \frac{\partial L}{\partial h_L} \prod_{l=2}^{L} \frac{\partial h_l}{\partial h_{l-1}} \frac{\partial h_1}{\partial W_1}

If ∥∂hl∂hl−1∥>1\|\frac{\partial h_l}{\partial h_{l-1}}\| > 1, gradients grow exponentially with depth.

Canonical Papers

On the difficulty of training Recurrent Neural Networks

Pascanu, Mikolov, Bengio2013ICML
Read paper →

Language Models are Few-Shot Learners

Brown et al.2020NeurIPS
Read paper →

Connections

Next Moves

Choose the next question to carry this object forward.