AdamW › L2 Regularization vs. Weight Decay
Language AI HandbookAll chapters AdamW › L2 Regularization vs. Weight DecayOne section · Sections in this chapter3 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Understanding the distinction between L2 regularization and weight decay is the core insight behind AdamW. These two approaches address the same goal from different angles, and for a long time, practitioners used the terms interchangeably. They are identical in the context of standard gradient descent, but they diverge for adaptive optimizers, and that divergence has significant practical consequences.
L2 Regularization
L2 regularization adds a penalty term to the training loss. The modified objective function becomes:
where:
- : the original training loss (cross-entropy, MSE, etc.)
- : the regularization coefficient controlling penalty strength
- : the squared L2 norm of all parameters
- : the regularized loss that the optimizer minimizes
When you compute the gradient of with respect to a parameter , the regularization term contributes an extra additive term:
The gradient now has two components: the original loss gradient, which points toward lower training loss, plus , which always pushes toward zero. The optimizer sees a single combined gradient, with no distinction between the signal from data and the signal from regularization. The optimizer does not know that part of the gradient it receives is a regularization term rather than a data-derived signal. It simply processes the combined gradient as if it were all signal from the loss.
Weight Decay
Weight decay is a different approach to the same goal. Instead of modifying the loss function, weight decay modifies the parameter update directly. After computing the gradient update from the loss, you shrink the weights by a fixed fraction:
where:
- : the parameter value at step
- : the weight decay coefficient
- : the learning rate
- : the gradient of the training loss
The weight decay term multiplies the current parameter value by a number slightly less than 1, which shrinks it toward zero. This shrinkage is completely independent of the gradient. It happens as a direct multiplicative operation on the parameter value, regardless of what the gradient is doing or how large recent gradients have been. The gradient step and the weight decay step are conceptually separate operations, even though they are combined into a single update equation.
Why They Are Equivalent for SGD
For standard stochastic gradient descent with learning rate , the two approaches produce mathematically identical updates. The SGD update with L2 regularization is:
Compare this to the weight decay update: .
These are identical when . You can translate between the two by rescaling the regularization coefficient by the learning rate. For SGD, L2 regularization and weight decay are equivalent formulations of the same thing, just parameterized differently.
This equivalence is why many practitioners and many codebases used the terms interchangeably. Weight decay in PyTorch's SGD optimizer actually implements L2 regularization via the gradient, and for SGD it makes no difference. The naming inconsistency in the deep learning ecosystem is a direct consequence of this historical equivalence being assumed to hold universally, when in fact it breaks down as soon as you move beyond SGD.
Why the Distinction Matters Conceptually
Even though L2 regularization and weight decay are equivalent for SGD, they reflect different computational philosophies. L2 regularization thinks about regularization as part of the objective: you are literally optimizing a different function, one that includes a preference for small weights. Weight decay thinks about regularization as part of the update rule: you are adding a direct shrinkage operation that is applied after the gradient step.
For simple first-order optimizers, this distinction does not matter because the gradient step scales uniformly across all parameters. But once you introduce any form of adaptive scaling, the two approaches diverge because the adaptive scaling modifies the gradient-based update but not the direct parameter update.