Visual walkthrough — Gradient clipping
Before we start, one word we will lean on the whole way:
See Exploding and vanishing gradients for why the arrow sometimes grows monstrously long; this page assumes it can, and fixes it.
Step 1 — Draw the gradient as an arrow
WHAT. We take a tiny toy gradient with just two coordinates, , so it fits on a flat page. Here is the first weight's push, the second's. We plant its tail at the origin and draw an arrow to the point .
WHY. Real gradients have millions of coordinates — impossible to draw. But every idea in clipping (direction, length, shrinking) already appears in 2‑D. If you understand two numbers, you understand a million: the math is identical, just longer.
PICTURE. The blue arrow below is the gradient. Its horizontal reach is , its vertical reach is .
Step 2 — Measure the arrow's length (the norm)
WHAT. We need a single number for "how long is this arrow?" The arrow, its horizontal shadow , and its vertical shadow form a right triangle — a triangle with one square corner. The two shadows are the short sides; the arrow is the slanted long side (the hypotenuse).
WHY. Clipping's whole job is "is the arrow too long?" We literally cannot ask that until we can measure length. And "too long" must mean the straight-line length of the arrow, not or alone — because the arrow can be short in each coordinate yet long overall (think ).
Which tool, and why this one? To get the slanted side of a right triangle from its two square sides, the tool is the Pythagorean theorem: for a right triangle, . We use it and not, say, simple addition, because would over-count — walking east then north is shorter as a diagonal than as two separate legs. Squaring, adding, then square-rooting is exactly the straight-line distance.
PICTURE. The triangle: red horizontal leg , green vertical leg , blue hypotenuse = the arrow, with its length labelled on the slant.
Step 3 — Draw the safe zone (the threshold circle)
WHAT. We pick one number , the threshold, and declare: any arrow of length is safe. All safe arrow-tips live inside (or on) a circle of radius centred at the origin. That circle is the "leash length."
WHY. We need a fixed line between "fine" and "dangerous," decided before we see the arrow, so the rule is objective. The circle is the natural boundary because "length " is exactly the definition of a disk of radius — every safe arrow fits inside, every unsafe one pokes out.
PICTURE. A yellow circle of radius . A short blue arrow sits inside (safe); a long blue arrow pierces the circle (unsafe). The tip is all that matters — inside = keep, outside = must fix.
Step 4 — The safe case: do nothing
WHAT. Suppose . The arrow already lives inside the circle. We leave it completely untouched: . (The hat just means "the arrow after clipping.")
WHY. The arrow's direction is our best guess at downhill and its length is reasonable — distorting a perfectly good gradient would only slow learning for no reason. Rule one of any safety device: don't fire when there's no danger. See Loss landscapes and cliffs for why most ordinary steps are exactly this harmless case.
PICTURE. Arrow inside the circle, untouched; a green check marks "keep as-is."
Step 5 — The dangerous case: same direction, shrink the length
WHAT. Now : the arrow pokes out of the circle. We build a replacement obeying two demands at once:
- same direction as — so for some positive number (multiplying an arrow by a positive number keeps its heading, only rescales its length);
- length exactly — sitting right on the leash.
WHY. Near a cliff, the arrow's length is untrustworthy (it exploded) but its direction is still roughly the true downhill. So we surgically keep the direction and cut the length back to the safe boundary — no shorter (wasteful), no longer (unsafe).
Solve for the shrink factor. Demand 2 says the new length is , and we want that to equal : Since , this is a number between 0 and 1 — a genuine shrink.
PICTURE. The long arrow, and along the same ray, a shorter arrow whose tip lands exactly on the yellow circle. The scale factor is annotated on the ray.
Step 6 — Fuse both cases into one formula
WHAT. We now merge "do nothing" (Case 1) and "scale by " (Case 2) into a single expression, using the (minimum: "pick the smaller of two numbers").
WHY. Two if-branches are error-prone; one clean formula runs the same code path every step. The trick: notice the branches only differ in the multiplier — Case 1 multiplies by , Case 2 by . We want whichever is smaller.
- If : then , so → do nothing. ✓ (matches Step 4)
- If : then , so → shrink. ✓ (matches Step 5)
The automatically selects the right branch. That is the whole reason it appears.
PICTURE. A number line of the multiplier value : to the left of (unsafe, we shrink), to the right of (safe, locks us to ). The kink at is where behaviour switches.
Step 7 — Edge & degenerate cases (never leave the reader stranded)
WHAT & WHY. A formula is only trustworthy if it survives the weird inputs. We test the corners.
- Exactly on the boundary, . Then , → unchanged. Good: the boundary is "safe," no jolt.
- The zero gradient, . Length , so would divide by zero — undefined! But a zero arrow has no direction and no danger, so the correct answer is simply . In real code this is handled by adding a tiny or checking first. Never divide by a zero norm.
- Already tiny, . Factor is huge , clamps it to → untouched. Clipping never grows a small gradient — which is exactly why it cannot cure vanishing gradients (that needs Weight initialization, Batch Normalization, or gating from Recurrent Neural Networks).
- One coordinate zero, e.g. . Fine — the norm is just , and scaling keeps that lone axis aligned. No special handling.
PICTURE. Four mini-panels: on-boundary (green tick), zero arrow (a dot, "undefined → set 0"), tiny arrow (untouched), axis-aligned arrow (untouched-or-scaled cleanly).
Recall Quick self-check:
, Length . Unsafe since . Factor . So , whose length is . Exactly on the leash. ✓
The one-picture summary
WHAT. One diagram compresses the entire derivation: safe arrows stay put; unsafe arrows slide down their own ray until their tip rests on the circle of radius .
Every clipped arrow lands on the yellow circle along its original direction — "same arrow, shorter arrow." Every safe arrow is left exactly where it was. That single behaviour is the boxed formula, and the boxed formula is that single behaviour. For where this sits in the training loop (after Backpropagation through time, before the Optimizers - SGD Adam RMSProp step), return to the parent Gradient clipping.
Recall Feynman retelling — the whole walkthrough in plain words
A gradient is just an arrow: which way to nudge the weights, and how hard (Step 1). To ask "is it too big?" we first need its length, which is the slanted side of the right triangle its coordinates make — Pythagoras, square-add-root (Step 2). We draw a circle of radius around the origin: arrows inside are safe, arrows poking out are dangerous (Step 3). If the arrow is inside, we touch nothing — a good gradient shouldn't be spoiled (Step 4). If it pokes out, we keep its heading and slide its tip back onto the circle by multiplying the whole arrow by , a number less than one (Step 5). Both rules fuse into one line using , which quietly picks "" when safe and "" when not (Step 6). We double-check the corners: on the boundary nothing happens, a zero arrow we just leave at zero (never divide by zero), and a tiny arrow is never inflated — so clipping fights explosions, never rescues vanishing (Step 7). One sentence to keep: same arrow, shorter arrow.
Connections
- Gradient clipping (parent)
- Exploding and vanishing gradients
- Loss landscapes and cliffs
- Recurrent Neural Networks
- Backpropagation through time
- Optimizers - SGD Adam RMSProp
- Weight initialization
- Batch Normalization