Skip to content
mlmentorship

Weight decay vs. L2 regularization

L2 adds ½λ‖θ‖² to the loss; weight decay shrinks θ multiplicatively at each step. They are equivalent under SGD but not under Adam. Which is why AdamW exists.

Published · 7 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

Trace the lambda-theta term and see why L2 shrinkage passes through Adam's coordinate-wise history while AdamW decay bypasses it.

Preparing the visual…

Summary

L2 regularization adds a penalty to the loss; weight decay multiplies parameters by at each step. Under vanilla SGD they are mathematically equivalent. Under adaptive optimizers like Adam they are not. And the difference is large enough that AdamW (Loshchilov & Hutter, 2019) is now the default for transformer training.

The two formulations

L2 (penalty added to loss)

Gradient: . Update under SGD: .

Weight decay (multiplicative shrink)

Same expression. Under vanilla SGD, the two are identical.

Why they diverge under Adam

Adam scales the gradient per parameter using its moment history. With L2, the penalty is added before those moments are updated:

The penalty therefore enters both moment histories and inherits Adam’s coordinate-wise scaling. In the fixed-preconditioner view, its contribution is proportional to : larger for coordinates with a small denominator and smaller for coordinates with a large denominator. Regularization is now coupled to gradient history.

Learning objective

Adam with L2 sends shrinkage through the adaptive denominator; AdamW routes it around.

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
Read it this way: follow λθ. In Adam with L2, it merges with the gradient before the adaptive machinery, so each coordinate's history changes how much it shrinks. In AdamW, λθ takes the upper bypass and reaches the parameter directly, so decay is one clear fractional shrinkage. Original schematic checked against Loshchilov and Hutter (2019) and the PyTorch AdamW algorithm.

AdamW decouples them: apply Adam to the data loss only, and then shrink the parameters multiplicatively as a separate step:

The shrink term has no scaling. This recovers the SGD-equivalent behavior.

Empirical impact

Loshchilov & Hutter (2019) and many follow-up benchmarks show AdamW generalizes meaningfully better than Adam-with-L2 across vision and NLP. The exact gain depends on the task; on transformer LLM training the gap is large enough that essentially all modern training uses AdamW.

What to skip

Common practice: do not decay biases, LayerNorm parameters, or embeddings. These are 1D parameters with different statistical roles, and decaying them often hurts. Standard implementations construct two parameter groups: {decay: linear weights, conv kernels} and {no decay: biases, norms, embeddings}.

Common pitfalls

  • Using Adam with weight_decay > 0 in PyTorch. This applies L2-as-gradient, not AdamW. Use AdamW explicitly.
  • Decaying bias and LayerNorm parameters. Hurts performance; exclude them via parameter groups.
  • Picking from a CNN recipe. for ResNets; for AdamW transformer pretraining (with the no-decay carve-out). Different scale, different rule of thumb.
  • Forgetting that decay scales with LR. Effective shrink per step is . Halving LR halves effective decay; you may need to compensate.