Weight Decay › Decoupled Weight Decay and AdamW
Language AI HandbookAll chapters Weight Decay › Decoupled Weight Decay and AdamWOne section · Sections in this chapter4 / 11 ↓
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
Standard weight decay works beautifully with stochastic gradient descent. The math lines up: adding the L2 penalty to the loss is exactly equivalent to decaying the weights by a fixed fraction at each step. However, when you move to adaptive gradient methods like Adam, this equivalence breaks down in a way that significantly matters in practice.
How Adam Works
Before examining the problem, it helps to recall how Adam updates parameters. Adam maintains per-parameter adaptive learning rates. For each parameter , it tracks the first moment (exponential moving average of gradients) and the second moment (exponential moving average of squared gradients) . The full update procedure at step is:
where:
- : the gradient of the loss with respect to parameter
- : decay rate for the first moment (typically 0.9)
- : decay rate for the second moment (typically 0.999)
- : bias-corrected first moment estimate, correcting for initialization at zero
- : bias-corrected second moment estimate
- : small constant for numerical stability (typically )
The key insight about Adam is that the denominator normalizes the update by the root mean square of past gradients. Parameters that have historically large gradients receive smaller updates (the large denominator divides them down), while parameters with historically small gradients receive larger updates. This per-parameter rescaling is what makes Adam so effective for neural networks, where different parameters may have very different natural gradient scales.
The Problem with L2 Regularization in Adam
When you add L2 regularization to the loss, the gradient passed to Adam becomes , where is the task loss gradient. This regularization gradient gets folded into both the first and second moment estimates:
As a result, the adaptive learning rate scale, which is computed from gradient history, is influenced by the regularization term. The second moment now reflects the history of both the task gradient and the weight decay gradient.
The consequence is that the effective weight decay depends on the history of gradients for that parameter. Parameters with small gradients from the task loss will have small values. Adam would normally amplify their updates (small denominator). But because is also folded into , the second moment estimate is inflated by the regularization term, partially suppressing this amplification. The actual regularization strength varies in complicated ways across parameters, defeating the purpose of having a clean hyperparameter.
The deeper problem is this: the adaptive scaling that Adam applies to regularize the step size based on gradient history should apply to the task signal, not to the weight decay signal. Weight decay is supposed to be a global shrinkage applied uniformly to all parameters. When it gets folded into Adam's adaptive mechanism, the global uniformity is lost. Parameters with small task gradients receive weaker regularization, which is the opposite of what you want: these are often the parameters most likely to drift to unusual values, since Adam amplifies their updates.
Ilya Loshchilov and Frank Hutter identified this problem in their 2017 paper "Decoupled Weight Decay Regularization" and proposed a fix: separate the weight decay step from the gradient update step.
AdamW: Decoupled Weight Decay
AdamW modifies the Adam update by applying weight decay directly to the parameters, independently of the adaptive gradient scaling. The update rule becomes:
where:
- : the gradient of the task loss (not including the regularization term)
- : the weight decay term applied directly to the parameter, outside the adaptive scaling
- All other terms are as in standard Adam
The critical difference is in the last line. The weight decay term is added to the update outside the adaptive denominator . This means the weight decay applies uniformly to all parameters at the same rate , regardless of their gradient history. The adaptive learning rate only affects the gradient term, not the regularization term.
Expanding the AdamW update:
This is analogous to the SGD case: multiply the parameter by to shrink it, then subtract the adaptively scaled gradient. The weight decay effect is clean and uniform.
Why AdamW Trains Better
In practice, AdamW consistently outperforms Adam with L2 regularization for training large language models and other deep networks. The reason is that uniform weight decay across all parameters is a more coherent regularization strategy. When Adam applies L2 regularization internally, parameters with small gradients (often those that represent rarely-used features or less-frequently updated neurons) receive weaker regularization. These are exactly the parameters most at risk of drifting to unusual values, since the optimizer's adaptive scaling amplifies their updates. AdamW avoids this asymmetry.
The practical difference is especially visible when you tune the weight decay hyperparameter. With standard Adam + L2, the optimal tends to be entangled with the learning rate in a complex way, making hyperparameter search difficult. AdamW decouples these, making and more independently tunable and simplifying the optimization landscape.
A concrete way to see the difference: suppose you have a parameter with consistently small task gradients, so is small (Adam will amplify its updates). With L2 Adam, the regularization term gets divided by the small , making the effective regularization on this parameter much larger than would suggest. With AdamW, the regularization is exactly regardless of . This predictability is what makes AdamW a reliable optimizer for large-scale training.
To put numbers on this: if (a rarely-updated parameter), , , and , then:
- With L2 Adam: effective weight decay per step is approximately (100 times stronger than intended)
- With AdamW: effective weight decay per step is (exactly as intended)
The L2 Adam approach accidentally applies extremely strong regularization to rarely-updated parameters, which is exactly backwards from what a well-calibrated regularizer should do. AdamW keeps regularization proportional to throughout, regardless of the parameter's gradient history.
Most deep learning frameworks now provide AdamW as a first-class optimizer. In PyTorch, torch.optim.AdamW implements decoupled weight decay. In Hugging Face Transformers, AdamW is the default optimizer and is used to train nearly all published pretrained models including BERT, GPT-2, and their successors. When you see weight decay reported in a model's training configuration, it almost always refers to AdamW-style decoupled weight decay.
Historical Context: Why This Took So Long to Notice
It might seem surprising that a flaw in how L2 regularization interacts with Adam went unnoticed for several years after Adam was introduced in 2014. The reason is that for many tasks, the difference between L2 Adam and AdamW is modest: both apply some form of regularization, and both work better than no regularization at all. The difference becomes most visible when you are carefully tuning hyperparameters for large-scale pretraining, where the precise meaning of the weight decay coefficient matters and the scale of training amplifies small differences. The natural language processing community discovered the practical importance of AdamW through large-scale BERT and GPT training, where the cleaner regularization semantics of AdamW translated into measurable improvements.
The Loshchilov and Hutter paper also documented a related issue: the optimal learning rate for Adam with L2 regularization depends on the weight decay coefficient, because both affect the effective step size. This coupling makes grid search over hyperparameters much harder. AdamW breaks this coupling, making hyperparameter search over and more independent.