Search

Search papers and lessons

Gradient Norm Clipping

Level 3 · 8 min read

Gradient norm clipping caps the total size of the gradient at a threshold and keeps its direction, so one huge step cannot wreck training.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Probability
  • Optimizers
  • Losses & regularization
  • Discrete math
  • Sign in to see your progress

TL;DR

Training takes a step in the direction of the gradient, and occasionally that gradient is enormous. One huge step can throw the weights far away from anything useful, and the loss jumps. Gradient norm clipping fixes this by measuring the length of the whole gradient vector and, if it is longer than a threshold, shrinking it to exactly that threshold. The direction stays the same. Only the size changes.

Where you'll see it: in the training loop of almost every recurrent network and large language model, where the default setting is a maximum gradient norm of 1.0 or 5.0.

Intuition

A steering wheel that turns too far

Gradient descent is driving downhill and steering by the slope under the wheel. Usually the slope is gentle and the car moves sensibly. Occasionally the slope becomes a cliff, and the car lurches off the road. The obvious fix is to limit how far the car can move in one go, and keep it pointed the same way.

Clipping does exactly this. It does not change where the car is heading, only how far it is allowed to go in that direction this step.

Why the gradient explodes

Recall the backward pass. The gradient for an early weight is a product of many local factors, one per layer or per time step. In a recurrent network the same weight matrix is applied at every step, so the product is repeated again and again. If the typical factor is a little above 1, the product grows geometrically. Ten steps at a factor of 2 give 210=10242^{10} = 1024 times the original signal.

The opposite problem is the vanishing gradient, where factors below 1 shrink the signal to nothing. Clipping is a tool for the first problem only. It does not help when the gradient is already too small.

flowchart LR
  G[Compute gradient g] --> N{Length of g above threshold c?}
  N -->|no| U[Use g unchanged]
  N -->|yes| S[Rescale g so its length is c]
  U --> W[Update weights]
  S --> W

The math

Setting up the gradient as a vector

Let θ\boldsymbol{\theta} be all the trainable weights of a model, stacked into one long vector. The gradient of the loss LL with respect to them, g=∇θL\mathbf{g} = \nabla_{\boldsymbol{\theta}} L, is a vector of the same length. Each entry says how the loss would change if one weight moved a little. Gradient descent updates the weights by

θ←θ−η g,\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \eta\,\mathbf{g},

where η\eta is the learning rate.

The size of g\mathbf{g} is measured with the Euclidean norm over all weights together, the global norm:

∥g∥=∑jgj2.\|\mathbf{g}\| = \sqrt{\sum_{j} g_j^2}.

The clipping rule

Choose a threshold c>0c \gt 0. Then clip the gradient:

g~={gif ∥g∥≤c,c∥g∥ gif ∥g∥>c.\tilde{\mathbf{g}} = \begin{cases} \mathbf{g} & \text{if } \|\mathbf{g}\| \le c, \\[4pt] \dfrac{c}{\|\mathbf{g}\|}\,\mathbf{g} & \text{if } \|\mathbf{g}\| \gt c. \end{cases}

When the norm is above the threshold, the gradient is multiplied by c/∥g∥c / \|\mathbf{g}\|. This scalar is less than 1, so every entry shrinks by the same factor. The new vector has length exactly cc:

∥g~∥=c∥g∥ ∥g∥=c.\|\tilde{\mathbf{g}}\| = \frac{c}{\|\mathbf{g}\|}\,\|\mathbf{g}\| = c.

Because every entry is scaled by the same number, g~\tilde{\mathbf{g}} points in exactly the direction of g\mathbf{g}. That is the key difference from clipping each entry separately, which changes the direction.

510152000.20.40.60.81gradient norm ||g||scale applied to g
  • scale factor, c = 5
With threshold c = 5 the scale factor is 1 up to a norm of 5, then falls as 5 divided by the norm. Larger gradients are shrunk more, but never reversed.

Global norm versus per-parameter clipping

There is a design choice. Clipping by the global norm over all weights, as above, keeps the relative sizes between layers. Clipping each parameter tensor separately makes every layer's gradient have its own cap, which changes the direction of the full gradient. Most libraries default to the global norm, because it is the version that matches the geometric description.

Worked examples

Example
A gradient above the threshold

Take a gradient with two weights, g=(3,4)\mathbf{g} = (3, 4) before clipping, and threshold c=1c = 1.

  1. Compute the norm: ∥g∥=9+16=5\|\mathbf{g}\| = \sqrt{9 + 16} = 5.
  2. Compare with the threshold: 5>15 \gt 1, so clipping applies.
  3. Compute the scale: c/∥g∥=1/5=0.2c / \|\mathbf{g}\| = 1/5 = 0.2.
  4. Multiply: g~=0.2⋅(3,4)=(0.6, 0.8)\tilde{\mathbf{g}} = 0.2 \cdot (3, 4) = (0.6,\ 0.8).
  5. Check the new length: 0.36+0.64=10.36 + 0.64 = 1, so ∥g~∥=1=c\|\tilde{\mathbf{g}}\| = 1 = c.

The direction is unchanged: (0.6,0.8)(0.6, 0.8) and (3,4)(3, 4) both point along the line with slope 4/34/3. Only the size has been cut from 5 to 1.

Example
An exploding recurrent gradient

Suppose a recurrent network produces a gradient of g=(600, 800)\mathbf{g} = (600,\ 800) on one batch, because a factor of about 2 has been multiplied in over ten time steps. Use threshold c=5c = 5.

  1. Norm: ∥g∥=6002+8002=360000+640000=1000000=1000\|\mathbf{g}\| = \sqrt{600^2 + 800^2} = \sqrt{360000 + 640000} = \sqrt{1000000} = 1000.
  2. Scale: c/∥g∥=5/1000=0.005c / \|\mathbf{g}\| = 5 / 1000 = 0.005.
  3. Clipped gradient: g~=(600⋅0.005, 800⋅0.005)=(3,4)\tilde{\mathbf{g}} = (600 \cdot 0.005,\ 800 \cdot 0.005) = (3, 4).
  4. Check: ∥g~∥=5=c\|\tilde{\mathbf{g}}\| = 5 = c.

Without clipping, the update ηg\eta\mathbf{g} with learning rate η=0.01\eta = 0.01 would move the weights by (6,8)(6, 8), a jump of length 10. With clipping it moves by (0.03,0.04)(0.03, 0.04), a step of length 0.05 that still heads in the same direction as the true gradient.

Example
A normal gradient passes through untouched

Take g=(0.3,−0.4)\mathbf{g} = (0.3, -0.4) with threshold c=1c = 1.

  1. Norm: ∥g∥=0.09+0.16=0.25=0.5\|\mathbf{g}\| = \sqrt{0.09 + 0.16} = \sqrt{0.25} = 0.5.
  2. Compare: 0.5≤10.5 \le 1, so no clipping happens.
  3. Result: g~=g=(0.3,−0.4)\tilde{\mathbf{g}} = \mathbf{g} = (0.3, -0.4).

Clipping only acts above the threshold. On ordinary batches, training behaves exactly as it would without clipping, which is why the method is almost free to leave on.

Tradeoffs and failure modes

SituationProblemBetter choice
Occasional huge gradients in a recurrent networkSudden jumps wreck trainingClip the global norm
Gradients that are always smallClipping never activates and does nothingLook at the vanishing-gradient fix instead: better initialisation, gated units, or residual connections
Threshold set too lowClipping fires on most steps, so training is slowed by a permanently shrunk learning rateLog the norm and pick a threshold above its typical value
Threshold set too highClipping rarely fires, so it does not helpChoose a threshold near the 90th to 99th percentile of observed norms
Mixed-precision trainingA norm can overflow to infinity before clipping sees itScale the loss first, and check for non-finite gradients

The failure that is easiest to miss is a threshold that is too low. The training run looks stable, but it learns slowly because every large but correct gradient is silently shortened. Always log the gradient norm before and after clipping, so the choice is visible.

History

Exploding and vanishing gradients in recurrent networks were analysed in the early 1990s, including by Yoshua Bengio and colleagues, who studied why such networks struggle to learn long-range dependencies. Clipping gradients was used as a practical fix in recurrent language models in the early 2010s. Razvan Pascanu, Tomas Mikolov and Yoshua Bengio analysed the problem geometrically in their 2013 paper and argued for rescaling the whole gradient to a fixed norm, which preserves the direction of the update. Norm clipping has since become a standard option in deep learning libraries and is still common when training large language models.

Check yourself

QuestionA gradient has norm 8 and the threshold is c=2c = 2. What is the scale factor applied to it?Show answer

Clipping applies because 8>28 \gt 2. The scale factor is c/∥g∥=2/8=0.25c / \|\mathbf{g}\| = 2/8 = 0.25. Every entry of the gradient is multiplied by 0.25, and the new norm is 2.

QuestionWhy does clipping by the global norm keep the direction of the gradient, but clipping each entry to the range [−c,c][-c, c] does not?Show answer

Global-norm clipping multiplies every entry by the same scalar, so the vector is only stretched or shrunk and points the same way. Per-entry clipping caps each entry separately. A large entry is cut while a small entry is left alone, which changes the ratio between entries and so changes the direction.

QuestionA gradient is (1,2,2)(1, 2, 2) and the threshold is c=3c = 3. Is clipping applied, and what is the result?Show answer

The norm is 1+4+4=3\sqrt{1 + 4 + 4} = 3. The condition is ∥g∥≤c\|\mathbf{g}\| \le c, since 3≤33 \le 3, so no clipping is applied and the gradient stays (1,2,2)(1, 2, 2). This is the boundary case: clipping only starts above the threshold.

QuestionWhy is exploding gradient clipping a poor fix for vanishing gradients?Show answer

Clipping only reduces large gradients. When the gradient is already small, its norm is below the threshold, so clipping does nothing. Vanishing gradients need a different fix, such as careful initialisation, gating, or residual connections that give the signal a shorter path.

QuestionA model trains stably but is slower than expected, and logging shows the clip is active on 80 percent of steps. What is the likely cause and what would you change?Show answer

The threshold is probably too low, so most normal gradients are shortened and the effective learning rate is much smaller than intended. Raise the threshold to a value near the high percentiles of the observed gradient norms, so clipping only fires on genuine spikes.

Sources