Skip to content
Glacius
OptimizationConcept reference

Gradient clipping

Euclidean norm clipping shrinks an oversized gradient by one shared factor and leaves smaller vectors unchanged.

On this page 8 sections
  1. Overview
  2. An unusually large gradient can propose a disruptive parameter update
  3. When clipping precedes an optimizer step, first obtain the clipped vector
  4. Clipping each coordinate separately changes a different object and can change direction
  5. A closer look
  6. Key takeaway
  7. Sources & further reading
  8. Concept connections

01An unusually large gradient can propose a disruptive parameter update#

An unusually large gradient can propose a disruptive parameter update. Whole-vector clipping limits the gradient’s length while preserving its direction. It is a guard on the proposed gradient, separate from the optimizer’s learning rate.

Whole-vector clipping caps Euclidean length at T>0. For a nonzero gradient, use one common factor:

a=min(1,T/g2)gclip=ag\begin{gathered}a=\min(1,T/\|g\|_2)\\g_{\rm clip}=a g\end{gathered}

A zero vector stays zero; never divide by its norm.

Gradient (6,8) has norm 10. Threshold 5 gives common factor 0.5, so the clipped vector is (3,4).

Gradient g=(6,8) has norm 10. The threshold-5 circle meets the same ray at (3,4). Multiplying all coordinates by .5 halves the length and preserves direction.Gradient g=(6,8) has norm 10. The threshold-5 circle meets the same ray at (3,4). Multiplying all coordinates by .5 halves the length and preserves direction.
Figure 1Gradient g=(6,8) has norm 10. The threshold-5 circle meets the same ray at (3,4). Multiplying all coordinates by .5 halves the length and preserves direction.
Link to this figure ↗Download SVGDownload PNG

For g=(6,8)g=(6,8), squared length is 36+64=10036+64=100, so length is 10. A threshold 5 gives scale factor 5/10=0.55/10=0.5. Apply the same factor to both entries to get (3,4)(3,4), whose length is 5. If the learning rate is 0.1, the eventual plain-gradient step has length 0.5.

Check your reasoning

Whole-vector Euclidean clipping: g=(8,6)(-8, 6), threshold 5. Find the clipped gradient.

  1. A(4,4)(-4, 4)
  2. B(3,3)(-3, 3)
  3. C(4,3)(-4, 3)
Show answer and explanation
(4,3)(-4, 3)

Norm 10; scale by 1/2.

02When clipping precedes an optimizer step, first obtain the clipped vector#

When clipping precedes an optimizer step, first obtain the clipped vector. Then apply the learning rate to that result.

The threshold limits gradient length. It does not itself specify the parameter-step length, which also depends on the learning rate.

Check your reasoning

Clip g=(3,4)(3, 4) to Euclidean norm ≤2.5. Then update θ=(5,3)(5, 3) with η=0.5. Find θ−ηg_clipped.

  1. A(4.25,3)(4.25, 3)
  2. B(4.25,2)(4.25, 2)
  3. C(5.25,2)(5.25, 2)
Show answer and explanation
(4.25,2)(4.25, 2)

Clipped g=(1.5,2).

03Clipping each coordinate separately changes a different object and can change direction#

Clipping each coordinate separately changes a different object and can change direction. Whole-vector clipping uses one common nonnegative factor.

Vectors already at or below the threshold stay unchanged. Clipping should never enlarge them to reach the threshold.

Check your reasoning

Whole-vector Euclidean clipping: g=(6,8)(6, -8), threshold 5. Draft: (5,5)(5, -5). Correct the vector.

  1. A(3,4)(3, -4)
  2. B(3,3)(3, -3)
  3. C(4,4)(4, -4)
Show answer and explanation
(3,4)(3, -4)

One factor: 1/2.

04A closer look#

Clipping is used to control very large gradients during training, including in recurrent networks. It can limit an update’s size but cannot repair an incorrectly computed gradient. Componentwise clipping is a different operation and can change the direction.

Key takeaway

Compare the whole-vector norm with the threshold, shrink only if needed, then apply any requested update.

  • Rescale a gradient to satisfy a supplied norm threshold.

Sources & further reading

  1. [1]
    D2L §9.5.3, equation9.5.3D2L §9.5.3, equation9.5.3 · Article

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Gradient clipping.” Math behind ML. /learn/o-gradient-clipping