Quant Memo
Core

Weight Decay vs L2 Regularization

Why weight decay and L2 regularization give the same update rule under plain SGD but diverge once you use an adaptive optimizer like Adam — the distinction AdamW was built to fix.

L2 regularization adds a penalty λ2w2\frac{\lambda}{2}\|w\|^2 to the loss function, so the optimizer is minimizing loss plus a term that punishes large weights. Weight decay is a direct update rule: at every step, shrink each weight by a fixed fraction, wwηλww \leftarrow w - \eta \lambda w, before applying the gradient step. Under plain stochastic gradient descent these turn out to be exactly equivalent, because the gradient of the L2 penalty term λw\lambda w added to the loss gradient produces precisely that same shrinkage.

The equivalence breaks under Adam and similar adaptive optimizers, which rescale each parameter's update by a running estimate of that parameter's gradient magnitude:

wwηm^v^+ϵ.w \leftarrow w - \eta \, \frac{\hat{m}}{\sqrt{\hat{v}} + \epsilon}.

If you implement "regularization" as an L2 penalty added to the loss (as most deep learning frameworks did by default for years), the penalty's gradient λw\lambda w gets divided by v^\sqrt{\hat v} along with everything else — so parameters with large historical gradients get less shrinkage than intended, and parameters with small gradients get more, an unintended and parameter-dependent side effect nobody asked for. Applying weight decay as a direct multiplicative shrink instead (wwηλww \leftarrow w - \eta\lambda w, applied separately from the adaptive gradient step) avoids this entirely — that's the whole idea behind AdamW.

Worked example. Suppose λ=0.01\lambda = 0.01, η=0.001\eta = 0.001, and a parameter's adaptive denominator v^+ϵ=0.1\sqrt{\hat v}+\epsilon = 0.1. L2-as-penalty shrinks the weight by ηλw/0.1=0.0001w\eta \cdot \lambda w / 0.1 = 0.0001w (10x amplified by the small denominator). Decoupled weight decay shrinks it by ηλw=0.00001w\eta\lambda w = 0.00001w regardless of v^\hat v — the intended, denominator-independent amount.

L2 regularization and weight decay are the same thing under plain SGD, but under Adam the L2-as-penalty version gets warped by the adaptive per-parameter learning rate, while true (decoupled) weight decay shrinks every weight by the same intended fraction — the fix AdamW implements.

Related concepts

Practice in interviews

Further reading

  • Loshchilov and Hutter (2019), Decoupled Weight Decay Regularization
ShareTwitterLinkedIn