Gradient Clipping

⚡ Key Takeaways

Gradient clipping is a technique that bounds the magnitude of gradients during the training of machine learning models to prevent destructively large parameter updates. Key result in words: when a gradient's norm exceeds a chosen threshold, it is rescaled (or its components are truncated) so that updates remain stable. Where it applies: training deep neural networks, especially recurrent networks (RNNs, LSTMs), transformers, and reinforcement learning agents. Who/when: popularized by Razvan Pascanu, Tomas Mikolov, and Yoshua Bengio (2012–2013) as a remedy for the exploding gradient problem.

💡 The Core Insight

Optimization fails not because gradients point in the wrong direction, but because they can occasionally point in the right direction with an enormous magnitude — overshooting the minimum and landing in a worse region. Gradient clipping decouples where to step (direction) from how far to step (magnitude), capping only the latter. The direction is trusted; the size is policed.

Formal Statement

DefinitionGradient and its norm

Let L(θ)L(\boldsymbol\theta) be a scalar loss function of parameters θ∈Rn\boldsymbol\theta \in \mathbb{R}^n. The gradient is

1
g  =  ∇θL  =  (∂L∂θ1,…,∂L∂θn) ⁣⊤.\quad \mathbf{g} \;=\; \nabla_{\boldsymbol\theta} L \;=\; \left(\frac{\partial L}{\partial \theta_1}, \dots, \frac{\partial L}{\partial \theta_n}\right)^{\!\top}.

Derivation hint: each component is the partial derivative of the loss with respect to one parameter; assemble into a column vector. Its Euclidean (ℓ2\ell_2) norm is

2
∥g∥2  =  ∑i=1ngi2.\quad \|\mathbf{g}\|_2 \;=\; \sqrt{\sum_{i=1}^{n} g_i^{2}}.

Derivation hint: apply the definition of the ℓ2\ell_2 norm to the gradient's components. Here gi=∂L/∂θig_i = \partial L / \partial \theta_i, nn is the number of parameters, and ∥⋅∥2\|\cdot\|_2 measures the total step length the raw gradient would induce.

DefinitionNorm clipping (clip-by-norm)

Given a threshold c>0c > 0, the clipped gradient g^\hat{\mathbf{g}} is

3
g^  =  {g,if ∥g∥2≤c,c∥g∥2 g,if ∥g∥2>c.\quad \hat{\mathbf{g}} \;=\; \begin{cases} \mathbf{g}, & \text{if } \|\mathbf{g}\|_2 \le c,\\[4pt] \dfrac{c}{\|\mathbf{g}\|_2}\,\mathbf{g}, & \text{if } \|\mathbf{g}\|_2 > c. \end{cases}

Derivation hint: scale the vector by the ratio of allowed norm cc to actual norm, but only when the actual norm exceeds cc. A compact single-line equivalent is

4
g^  =  g⋅min⁡ ⁣(1, c∥g∥2).\quad \hat{\mathbf{g}} \;=\; \mathbf{g}\cdot \min\!\left(1,\ \frac{c}{\|\mathbf{g}\|_2}\right).

Derivation hint: the min⁡\min selects the scaling factor 11 when ∥g∥≤c\|\mathbf g\|\le c and c/∥g∥c/\|\mathbf g\| otherwise, reproducing the piecewise definition. Symbols: cc is the clip threshold (a hyperparameter), g^\hat{\mathbf g} is the gradient actually fed to the optimizer, and the scaling factor lies in (0,1](0,1].

DefinitionValue clipping (clip-by-value)

An alternative clamps each component independently to [−v,v][-v, v]:

5
g^i  =  clip⁡(gi,−v,v)  =  max⁡ ⁣(−v, min⁡(v, gi)),i=1,…,n.\quad \hat{g}_i \;=\; \operatorname{clip}(g_i, -v, v) \;=\; \max\!\big(-v,\ \min(v,\ g_i)\big), \qquad i = 1,\dots,n.

Derivation hint: nest a lower bound (max⁡\max with −v-v) inside an upper bound (min⁡\min with vv) for each coordinate. Here v>0v > 0 is the per-component bound. Unlike norm clipping, this changes the direction of the gradient vector.

TheoremDirection preservation of norm clipping

Norm clipping (Eq. [3]) preserves the direction of the gradient: g^\hat{\mathbf g} is a non-negative scalar multiple of g\mathbf g, i.e. g^=α g\hat{\mathbf g} = \alpha\,\mathbf g with α∈(0,1]\alpha \in (0,1]. Consequently the cosine similarity between g^\hat{\mathbf g} and g\mathbf g equals 11.

Proof

Step 1. If ∥g∥2≤c\|\mathbf g\|_2 \le c, then g^=g=1⋅g\hat{\mathbf g} = \mathbf g = 1\cdot \mathbf g, so α=1\alpha = 1.

Step 2. If ∥g∥2>c\|\mathbf g\|_2 > c, then by Eq. [3], g^=c∥g∥2g\hat{\mathbf g} = \frac{c}{\|\mathbf g\|_2}\mathbf g, so α=c/∥g∥2\alpha = c/\|\mathbf g\|_2.

Step 3. Since c>0c > 0 and ∥g∥2>0\|\mathbf g\|_2 > 0, we have α>0\alpha > 0; since ∥g∥2>c\|\mathbf g\|_2 > c, we have α<1\alpha < 1. Thus α∈(0,1)\alpha \in (0,1) in this case.

Step 4. Combining, α∈(0,1]\alpha \in (0,1] always. The cosine similarity is

6
cos⁡θ=g^⋅g∥g^∥ ∥g∥=α ∥g∥2α∥g∥⋅∥g∥=1.■\quad \cos\theta = \frac{\hat{\mathbf g}\cdot \mathbf g}{\|\hat{\mathbf g}\|\,\|\mathbf g\|} = \frac{\alpha\,\|\mathbf g\|^2}{\alpha\|\mathbf g\|\cdot\|\mathbf g\|} = 1. \qquad \blacksquare

Derivation hint: substitute g^=αg\hat{\mathbf g}=\alpha\mathbf g, use g^⋅g=α∥g∥2\hat{\mathbf g}\cdot\mathbf g = \alpha\|\mathbf g\|^2 and ∥g^∥=α∥g∥\|\hat{\mathbf g}\| = \alpha\|\mathbf g\|, then cancel.

∎
Figure
Norm clipping rescales an over-long gradient back to the threshold ball while preserving direction
θ₁ θ₂ ‖g‖ = c (clip ball) raw g (‖g‖ > c) ĝ = (c/‖g‖) g same direction, capped length

Why It Holds / Derivation

Proof

Goal: show that norm clipping is equivalent to a per-step adaptive learning rate. Consider standard gradient descent with learning rate η\eta:

7
θt+1=θt−η g^t.\quad \boldsymbol\theta_{t+1} = \boldsymbol\theta_t - \eta\,\hat{\mathbf g}_t.

Derivation hint: substitute the clipped gradient into the ordinary update rule.

Step 1. When ∥g∥2>c\|\mathbf g\|_2 > c, substitute Eq. [3]:

8
θt+1=θt−η c∥gt∥2 gt=θt−ηeff gt,ηeff=η c∥gt∥2.\quad \boldsymbol\theta_{t+1} = \boldsymbol\theta_t - \eta\,\frac{c}{\|\mathbf g_t\|_2}\,\mathbf g_t = \boldsymbol\theta_t - \eta_{\text{eff}}\,\mathbf g_t, \qquad \eta_{\text{eff}} = \frac{\eta\,c}{\|\mathbf g_t\|_2}.

Derivation hint: group the constants multiplying the raw gradient into a single effective learning rate.

Step 2. Interpretation: the effective learning rate ηeff\eta_{\text{eff}} is inversely proportional to the gradient norm. When the gradient explodes (∥gt∥→∞\|\mathbf g_t\| \to \infty), ηeff→0\eta_{\text{eff}}\to 0, so the step length is bounded:

9
∥θt+1−θt∥=η ∥g^t∥≤η c.\quad \|\boldsymbol\theta_{t+1} - \boldsymbol\theta_t\| = \eta\,\|\hat{\mathbf g}_t\| \le \eta\,c.

Derivation hint: take norms of Eq. [7]; since ∥g^∥≤c\|\hat{\mathbf g}\|\le c always, the step is bounded by ηc\eta c.

Step 3. When ∥g∥2≤c\|\mathbf g\|_2 \le c, ηeff=η\eta_{\text{eff}} = \eta and clipping is inert. Hence clipping is a conditional learning-rate damper that activates only on dangerous steps. ■\blacksquare

∎
RP
Razvan Pascanu, Tomas Mikolov & Yoshua Bengio
2012–2013

Working at the University of Montreal under Bengio, Pascanu and Mikolov diagnosed why recurrent neural networks failed to learn long-range dependencies. In their paper "On the difficulty of training Recurrent Neural Networks" (ICML 2013), they framed the problem geometrically: the loss surface of an RNN contains steep "cliffs" where the gradient norm spikes by orders of magnitude. They proposed gradient norm clipping as a cheap, principled defense, showing it allowed RNNs to traverse cliff edges without being flung into bad regions. Bengio, a Turing Award laureate (2018), had long championed deep architectures; this work made them trainable in practice.

Building Intuition

🧠 Intuition

Imagine hiking downhill in thick fog, taking steps proportional to how steep the ground feels beneath your feet. On gentle slopes you take normal strides. But if you reach the edge of a cliff, the ground feels infinitely steep, and a proportional step would hurl you off the cliff into a ravine far worse than where you started. Gradient clipping is a rule: "no matter how steep it feels, never take a stride longer than cc." You still step in the downhill direction — you just refuse to leap. This is why clipping rarely changes the path on smooth terrain but is a lifesaver near cliffs, which in neural networks correspond to regions where the loss curvature is extreme.

AN
A note on the "cliff" geometry
n/a

The cliffs are real consequences of the chain rule. In a recurrent network unrolled over TT steps, the gradient involves a product of TT Jacobian matrices. If the largest singular value of the recurrent weight matrix exceeds 11, this product grows like λT\lambda^T — exponentially. A tiny region where parameters cross such a threshold produces a near-vertical wall in the loss landscape.

Worked Examples

ExampleNorm clipping a 2D gradient

Given: gradient g=(6,8)⊤\mathbf g = (6, 8)^\top, clip threshold c=5c = 5, learning rate η=0.1\eta = 0.1.

Step 1 — compute the norm:

10
∥g∥2=62+82=36+64=100=10.\quad \|\mathbf g\|_2 = \sqrt{6^2 + 8^2} = \sqrt{36+64} = \sqrt{100} = 10.

Derivation hint: apply Eq. [2] to the two components.

Step 2 — check the trigger: ∥g∥2=10>5=c\|\mathbf g\|_2 = 10 > 5 = c, so clipping activates.

Step 3 — scale factor: α=c/∥g∥2=5/10=0.5\alpha = c/\|\mathbf g\|_2 = 5/10 = 0.5.

Step 4 — clipped gradient: g^=0.5 (6,8)⊤=(3,4)⊤\hat{\mathbf g} = 0.5\,(6,8)^\top = (3,4)^\top. Check: ∥g^∥=9+16=5=c\|\hat{\mathbf g}\| = \sqrt{9+16} = 5 = c. ✓

Step 5 — parameter step length: η ∥g^∥=0.1×5=0.5\eta\,\|\hat{\mathbf g}\| = 0.1 \times 5 = 0.5.

g^=(3, 4)⊤,step length=0.5 (parameter units)\boxed{\hat{\mathbf g} = (3,\,4)^\top,\quad \text{step length} = 0.5\ \text{(parameter units)}}
ExampleValue clipping vs. norm clipping comparison

Given: gradient g=(10,−1)⊤\mathbf g = (10, -1)^\top. Compare norm clipping (c=5c = 5) with value clipping (v=5v = 5).

Norm clipping: ∥g∥=100+1=101≈10.05>5\|\mathbf g\| = \sqrt{100+1} = \sqrt{101} \approx 10.05 > 5, so α=5/10.05≈0.4975\alpha = 5/10.05 \approx 0.4975.

11
g^norm≈(4.975, −0.4975)⊤.\quad \hat{\mathbf g}_{\text{norm}} \approx (4.975,\ -0.4975)^\top.

Derivation hint: multiply each component by α=c/∥g∥\alpha = c/\|\mathbf g\|. Direction unchanged: ratio g1/g2=−10g_1/g_2 = -10 preserved.

Value clipping (v=5v=5): clamp each component to [−5,5][-5,5]:

12
g^value=(min⁡(5,10), max⁡(−5,−1))⊤=(5, −1)⊤.\quad \hat{\mathbf g}_{\text{value}} = (\min(5,10),\ \max(-5,-1))^\top = (5,\ -1)^\top.

Derivation hint: apply Eq. [5] componentwise; only g1g_1 exceeds the bound. Direction changed: new ratio 5/(−1)=−5≠−105/(-1) = -5 \ne -10.

norm: (4.98,−0.50), direction kept;value: (5,−1), direction altered\boxed{\text{norm: }(4.98,-0.50),\ \text{direction kept};\quad \text{value: }(5,-1),\ \text{direction altered}}

Common Misconceptions

⚠️ Common mistake

Clipping is not regularization. Regularization (e.g. ℓ2\ell_2 weight decay) penalizes large parameters and changes the objective being minimized. Clipping leaves the objective LL untouched; it only modifies the optimizer's step. The minimum you converge toward is the same — you simply reach it more safely.

⚠️ Common mistake

The threshold cc must be tuned. Too small and learning crawls (every step is throttled, effectively a tiny learning rate); too large and clipping never activates, offering no protection. A common heuristic is to track the running statistics of ∥g∥\|\mathbf g\| during a warm-up phase and set cc near a high percentile (e.g. the 90th) of observed norms.

CounterexampleClipping does not fix vanishing gradients

Suppose an RNN suffers the opposite pathology: gradients shrink to ∥g∥=10−9\|\mathbf g\| = 10^{-9} as they propagate back in time. Clipping with any c>10−9c > 10^{-9} is completely inert — the gradient is already below threshold, so g^=g\hat{\mathbf g} = \mathbf g. Clipping addresses exploding gradients only; the vanishing gradient problem requires different remedies such as gated architectures (Long Short-Term Memory) or skip connections, not clipping.

CounterexampleValue clipping can stall descent

Let g=(100,0.001)⊤\mathbf g = (100, 0.001)^\top at a point where progress requires moving mainly along θ2\theta_2. Value clipping with v=1v=1 gives g^=(1,0.001)⊤\hat{\mathbf g} = (1, 0.001)^\top — it crushes the dominant component but leaves the tiny one almost untouched, badly distorting the direction so the step barely advances along the axis that matters. Norm clipping would have preserved the 100:0.001100{:}0.001 ratio.

Connections

connection

Gradient clipping sits at the intersection of optimization and numerical stability. It is most often paired with Gradient Descent and its variants such as Stochastic Gradient Descent and Adam Optimizer, where it is applied to the raw gradient before adaptive moment estimation. It is the standard partner of Backpropagation Through Time, the algorithm that produces the exploding gradients in the first place. Conceptually it relates to trust-region methods, which also bound step size, and to Learning Rate Scheduling, since Eq. [8] shows clipping is a data-dependent learning-rate adjustment. It complements but does not replace Batch Normalization and Long Short-Term Memory, which attack instability at the architecture level rather than the optimizer level.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1991 — Sepp Hochreiter characterizes the vanishing/exploding gradient problem in his diploma thesis, identifying the exponential scaling through time.
- 1994 — Bengio, Simard & Frasconi publish a formal analysis showing learning long-term dependencies with gradient descent is fundamentally difficult.
- 1997 — Hochreiter & Schmidhuber introduce LSTM, an architectural fix for vanishing gradients (not exploding).
- 2010 — Tomas Mikolov popularizes RNN language models, where exploding gradients become a practical nuisance and ad hoc clipping is used informally.
- 2012 — Mikolov's PhD thesis documents gradient norm clipping as a working trick for RNN-LM training.
- 2013 — Pascanu, Mikolov & Bengio publish "On the difficulty of training Recurrent Neural Networks" (ICML), giving the cliff-geometry analysis and formalizing norm clipping (Eq. [3]).
- 2015 — Sutskever et al. and the seq2seq era adopt clipping as a default in neural machine translation.
- 2017 — WGAN (Arjovsky et al.) uses weight clipping for Lipschitz constraints, broadening the family of clipping ideas.
- 2019–2020 — GPT-2/GPT-3 (OpenAI) entrench global-norm clipping (c=1.0c=1.0) as standard for large-scale transformer training.

Subtleties & Limits

open-problem

Several open questions remain. (1) Convergence theory: clipped SGD breaks the standard assumption that the optimizer follows the true gradient, so classical convergence proofs do not directly apply; recent work (Zhang et al., 2020) gives convergence guarantees under a relaxed smoothness assumption ∥∇2L∥≤L0+L1∥∇L∥\|\nabla^2 L\| \le L_0 + L_1\|\nabla L\|, but a complete picture is still developing. (2) Threshold selection: there is no universally principled way to choose cc; it remains largely empirical and architecture-dependent. (3) Per-layer vs. global clipping: whether to clip each layer's gradient separately or the concatenated global vector affects training dynamics in ways not fully understood. (4) Interaction with adaptive optimizers: how clipping composes with Adam's per-parameter scaling, and whether they sometimes work at cross purposes, is an active area. (5) Bias introduction: clipping makes the expected update a biased estimate of the true gradient direction in stochastic settings, and quantifying this bias precisely is subtle.

See Also

Gradient Descent · Stochastic Gradient Descent · Backpropagation Through Time · Long Short-Term Memory · Adam Optimizer · Vanishing Gradient Problem · Batch Normalization · Learning Rate Scheduling