Before you use the demo in the newer notebook, say what you expect to change.
Foundation reference
Weight Decay & AdamW: Decoupled Regularization
Weight decay shrinks the weights at each update; AdamW applies this shrinkage separately from Adam’s per-parameter scaling. The published GPT-3 and LLaMA recipes both used Adam with decoupled weight decay of 0.1 (Brown et al., 2020; Touvron et al., 2023).
Study guide
Where to start
Why does Adam's per-coordinate scaling also rescale an L2 penalty that is added to the gradient, so that weights with a history of large gradients are pulled toward zero less?
How to study this pagePredict · Check · Generalize · Continue
Compare what the notebook demo shows with the key equation here and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- Weight decay shrinks the weights at each update; AdamW applies this shrinkage separately from Adam’s per-parameter scaling. The published GPT-3 and LLaMA recipes both used Adam with decoupled weight decay of 0.1 (Brown et al., 2020; Touvron et al., 2023).
- Common implementations of Adam applied "weight decay" as an L2 term added to the gradient. Loshchilov & Hutter (2019) showed that this is not equivalent, and that decoupling the decay improved Adam's generalization enough to compete with SGD with momentum on image classification.
- Decoupling also makes the best weight-decay value largely independent of the learning rate, which simplifies tuning (Loshchilov & Hutter, 2019).
Questions to Think About
- What changes when the decay term λθ is applied to the weights directly instead of passing through Adam's division by √v̂?
- When a layer is followed by normalization, scaling its weights does not change the output; why does weight decay still matter there, through its effect on the effective learning rate (Zhang et al., 2019)?
Interactive Demo
The interactive demo for this concept is in the newer notebook.
Core Math
If you prefer intuition first, try the demo in the newer notebook, then come back for the math.
L2 regularization adds a penalty on the size of the weights to the loss:
Weight decay shrinks the weights directly at every step:
For plain SGD the two give the same update. For Adam they do not: an L2 term added to the gradient is divided by along with the rest of the gradient, so weights with large past gradients are regularized less (Loshchilov & Hutter, 2019).
AdamW decouples the decay from the adaptive step. With and the moving averages of and , and , their bias-corrected versions,
so every weight decays at the same rate per step, whatever its gradient history.