Gradient clipping
Euclidean norm clipping shrinks an oversized gradient by one shared factor and leaves smaller vectors unchanged.
On this page 8 sections
- Overview
- An unusually large gradient can propose a disruptive parameter update
- When clipping precedes an optimizer step, first obtain the clipped vector
- Clipping each coordinate separately changes a different object and can change direction
- A closer look
- Key takeaway
- Sources & further reading
- 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 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).
For , squared length is , so length is 10. A threshold 5 gives scale factor . Apply the same factor to both entries to get , whose length is 5. If the learning rate is 0.1, the eventual plain-gradient step has length 0.5.
Whole-vector Euclidean clipping: g=, threshold 5. Find the clipped gradient.
Show answer and explanation
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.
Clip g= to Euclidean norm ≤2.5. Then update θ= with η=0.5. Find θ−ηg_clipped.
Show answer and explanation
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.
Whole-vector Euclidean clipping: g=, threshold 5. Draft: . Correct the vector.
Show answer and explanation
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.
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]D2L §9.5.3, equation9.5.3 ↗D2L §9.5.3, equation9.5.3 · Article