Skip to content
mlmentorship

L2 and weight decay: follow the shrinkage path

Derive the vanilla-SGD equivalence, isolate adaptive scaling, and work changes to parameter groups and cumulative decay exposure.

Published · 9 min read ·Core ·Foundation
Reading depth

Summary

L2 adds a penalty to the objective. Decoupled weight decay shrinks parameters outside the optimizer’s gradient history. Their updates coincide for vanilla SGD, but that algebra does not automatically extend to momentum or Adam.

Task: start two parameters at , with data gradient , learning rate , and . Freeze adaptive denominators at to isolate one step. Does routing through those denominators give the same result as direct decay?

The two formulations

L2 (penalty added to loss)

Writing , the penalized gradient is . Vanilla SGD gives the exact expansion

Weight decay (multiplicative shrink)

Direct decay uses the expression on the right. Equality requires the same learning rate, penalty convention, parameter groups, and old-state evaluation, with no momentum or adaptive scaling.

Why they diverge under Adam

Coupled L2 feeds into both Adam moment buffers. Decoupled AdamW feeds only into them. The history itself changes, not merely a final division.

For the frozen-denominator fixture, isolate the paths without updating moments:

QuantityCoordinate 1Coordinate 2
Coupled data-plus-penalty step
Coupled next parameter
Direct decay
Decoupled data step
Decoupled next parameter

The equal weights receive equal fractional direct decay despite unequal adaptive denominators. This illustration is not a complete coupled-Adam trajectory; real coupled penalties also change both moments.

Learning objective

Adam rescales coupled L2 through its moments; AdamW applies decay directly

Adaptive rescaling changes coupled L2, not the direct decay fractionFor equal weights, coupled L2 passes through different fixed preconditioners. The illustrative decoupled decay remains 0.001 for either history.Shrinkage: coupled or directSame weights; frozen preconditionersFrozen gradient scale: 1w/ 10.0010.001Frozen gradient scale: 10w/ 100.00010.001Blue: coupled through the gradientscaleGreen: direct decay bypasses thatscale
Derivation

Follow λθ: through Adam's denominator in L2, around it in AdamW.

Coupled L2 and decoupled AdamW route the regularization term through different pathsThe left panel routes lambda theta through gradient joining, moment tracking, and two adaptive denominators, producing different shrinkage for coordinates with small and large denominators. The right panel routes lambda theta directly around Adam, producing the same fractional decay for both coordinates.Adam + L2: coupledlambda theta becomes part of the adapted gradientλθdata gradjoing + λθAdam momentsm and v include λθcoord 1small sqrt(v)coord 2large sqrt(v)same λθ, different effective shrinkageAdamW: decoupleddata gradient adapts; decay bypasses momentsλθdata gradAdam stepm, v see data onlyparameter updateminus ηλθ directlysame fractional decay for both coordinatesMobile view of coupled L2 and decoupled AdamW pathsTwo stacked panels show lambda theta merging with the Adam path in L2 and bypassing it in AdamW.Adam + L2: λθ joins Adamλθdata gradjoing + λθAdamdividessame λθ, but each coordinate history changes shrinkageAdamW: λθ bypasses Adamλθdata gradAdamdata onlyθupdateone direct fractional shrinkage, independent of v
follow λθ. In Adam with L2, it joins the gradient before the adaptive machinery and changes the moment histories. AdamW's upper bypass instead applies direct fractional decay. Numerical denominator comparisons freeze the preconditioner; they are not full coupled-Adam trajectories. Original schematic checked against Loshchilov and Hutter (2019) and the PyTorch AdamW algorithm.
Check: Why does Adam scale equal L2 penalties differently, while AdamW applies equal direct decay fractions?
Read it this way: For equal weights, coupled L2 passes through different fixed preconditioners. The illustrative decoupled decay remains 0.001 for either history.
Which route changes if the preconditioner grows from one to ten?

For AdamW, compute corrected moments from the data gradients and evaluate both contributions against the old parameter:

Epsilon is outside the square root in this convention. Shrinking an already updated parameter instead introduces a cross-term; it is not the displayed update.

Remove adaptive scaling

New task: replace denominators with , without momentum. Picture and change: both paths reduce to the vanilla-SGD expansion. Check: both produce . Use when: the actual optimizer satisfies those assumptions, not merely because its name contains SGD.

Exempt one parameter

New task: exclude coordinate two from direct decay while retaining its data gradient. Picture and change: set only its shrink contribution to zero. Check: the decoupled result becomes , instead of . Use when: the architecture explicitly exempts that parameter role; tied tensors must not receive duplicate updates.

Failure and transfer

With momentum, even zero current data gradient can leave a nonzero history-driven update. The vanilla proof cannot identify that movement with direct shrinkage. With zero adaptive updates, cumulative decay is , so the schedule matters.

Reconstruct: what happens when halves from at , ?

Answer

One direct-decay step changes from to . The decay exposure halves for that step; this says nothing about stored-gradient movement.

Empirical impact

Loshchilov and Hutter distinguish decoupled decay from adaptive L2 and report experiments. Their results are not a guarantee of improved generalization on every architecture or training regime. Compare tuned coefficients and schedules under a matched evaluation contract.

What to skip

Review parameter roles rather than applying a universal no-decay list. Biases and normalization scales are often treated separately; embedding tables are usually matrices, not one-dimensional parameters. An embedding can also be tied to an output projection, so membership must follow tensor identity. Assign every trainable parameter exactly once and retain the architecture’s chosen exclusions.

Common pitfalls

  • Inspect the installed API’s decay option; some Adam implementations expose a decoupled mode.
  • Loss reduction conventions change the relative scale of an objective penalty.
  • A copied coefficient does not preserve exposure when step count or learning-rate schedule changes.
  • The term is ordinary fractional shrinkage when ; larger products can erase or flip a parameter.