3.2.12 · D3Training Deep Networks

Worked examples — L1 - L2 weight decay in deep nets

3,567 words16 min readBack to topic

This page is a drill-ground for the parent topic. We take the two update rules you already met and run them through every kind of situation they can face: positive and negative weights, zero weights, huge weights, zero data-gradient and nonzero data-gradient, the sparsity snap of L1, the equilibrium of L2, and a limiting case where the step size is dangerously large.

Two rules, kept here so we never look them up mid-example.

A word on each symbol before we use it, so a newcomer is never lost:

  • — one number telling us which way the data wants this weight to move and how hard.
  • — how big a step we take; a small positive number like .
  • — how much we punish large weights; means "no punishment."
  • — just the arrow: if , if , if .
  • — "keep if it is positive, otherwise clamp to ." This is the clamp that lets L1 hit exactly zero.

The scenario matrix

Every cell below is a distinct behaviour these rules can produce. The examples that follow are tagged with the cell they cover.

# Case class What is special about it Covered by
A L2, , pure decay, geometric shrink Ex 1
B L2, equilibrium: decay balances data Ex 2
C L2, decay works symmetrically on negatives Ex 3
D L1, small , snap to exactly (sparsity) Ex 4
D' L1, small , snap to from the negative side Ex 4b
D'' L1, small , still snaps once Ex 4c
E L1, large $ w g\ne 0$
F L1 vs L2 head-to-head same weight, different fate Ex 6
G Degenerate: regularization off, plain GD Ex 7
H Limiting: factor : oscillation / neutral / blow-up Ex 8
I Word problem tuning against overfitting Ex 9
J Exam twist Adam ≠ Adam+L2, needs AdamW Ex 10

We cover every cell with the examples below. Figures accompany the two most geometric ones.

The first figure previews the whole page: it overlays the L2 geometric slide of Example 1 (blue) against the L1 march-to-zero of Example 4 (pink), so you can see before the algebra that L2 curves gently toward but never lands, while L1 walks in equal steps and hits exactly. Refer back to it as you read Examples 1 and 4.

Figure — L1 - L2 weight decay in deep nets

Example 1 — Cell A: pure L2 decay, no data


Example 2 — Cell B: L2 equilibrium


Example 3 — Cell C: L2 on a negative weight


Example 4 — Cell D: L1 snaps a small positive weight to exactly zero

The next figure is the reason this snap happens. It plots the soft-threshold map: input across, new weight up. The pink band around the origin is the dead zone — every is flattened onto the line "output ." Example 4's sits inside that band, so it is sent to ; Example 5's sits far outside it, so it only slides down by the fixed gap; Example 4c's (below) also lands inside the band. Watch how the band is symmetric — that symmetry is exactly what Example 4b relies on.

Figure — L1 - L2 weight decay in deep nets

Example 4b — Cell D': L1 snaps a small negative weight to zero


Example 4c — Cell D'': L1 snaps a small weight to zero even with nonzero data gradient


Example 5 — Cell E: L1 on a large weight (constant shrink, no snap)


Example 6 — Cell F: L1 vs L2 on the same weight


Example 7 — Cell G: degenerate


Example 8 — Cell H: limiting cases, (all three regimes)


Example 9 — Cell I: word problem (tuning )


Example 10 — Cell J: exam twist (Adam ≠ Adam+L2)


Recall One-line recap of every cell

A: geometric shrink. B: parks at . C: negatives shrink toward too. D/D': L1 snaps small weights (either sign) to exact . D'': snap survives a nonzero if . E: L1 trims big weights by a fixed sliver. F: same weight → L1 zeroes, L2 shrinks. G: ⇒ plain GD. H: converges, neutral oscillation, diverges. I: pick at validation minimum. J: Adam+L2 ≠ AdamW.

Which cell shows L1 achieving exact sparsity from the negative side?
Cell D' (Example 4b) — small negative weight, magnitude inside the dead zone, clamped to .
Does L1 still snap a small weight when the data gradient is nonzero?
Yes (Cell D'', Example 4c) — what matters is whether , not whether .
What is the L2 stability window for the shrink factor, and what happens at the boundary?
converges; gives factor and neutral (constant-amplitude) oscillation; diverges.
Equilibrium weight formula (L2, constant )?
, independent of .
How does the sign of enter the update?
Via : pushes down, pushes up (descent goes opposite the loss-increasing direction).