Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Shrink the weights, not the gradient
Weight decay pulls every weight a little toward zero each step, a simple way to prevent over-large weights. With plain SGD you can get it two ways: add λ‖w‖²/2 to the loss, or multiply the weights by (1 − ηλ) each step. They are mathematically identical. With Adam they are not: the extra gradient λw from the loss gets divided by Adam’s per-parameter scale like any other gradient, so its strength depends on that parameter’s gradient history. AdamW applies the decay directly to the weights, outside Adam.
Nearly every transformer, and many vision and control networks, are trained with AdamW, and its weight-decay value is one of the most influential hyperparameters. Knowing that “L2 in the loss” and “weight decay” are different under Adam explains why porting settings between frameworks or papers can change results, and when either is appropriate.
- 1. For plain SGD, L2 regularisation and weight decay are…
- 2. AdamW applies weight decay…
- 3. Under Adam, an L2 term in the loss…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- SGD with L2: w ← w − η(g + λw) = (1 − ηλ)w − ηg, exactly SGD with decay λ.
- AdamW: w ← w − η·m̂/(√v̂ + ε) − ηλw, where m̂ and v̂ are computed from the loss gradient only. Example: w = 2, η = 0.01, λ = 0.1: the decay alone gives 2 × 0.999 = 1.998.
- Adam + L2: the gradient g + λw goes into m and v. When the loss gradient vanishes, the step is still about η·sign(w), whatever λ is, because Adam normalises it.
- Half-life of decoupled decay: ln 2/(−ln(1 − ηλ)) ≈ 693 steps for ηλ = 0.001.
- Fixed points:
- Adam + L2 converges where ∇L + λw = 0, the ordinary L2-regularised (ridge) optimum.
- AdamW converges where the Adam direction m̂/√v̂ equals −λw. With a constant unit gradient, w → −1/λ.
- Parameter groups: biases and normalisation parameters are usually excluded from decay.
- 1. Decoupled decay factor per step for η = 0.001, λ = 0.01?
- 2. Adam + L2 converges where…
- 3. Usually excluded from weight decay are…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Decoupled decay in code
Exercise 1.
- With plain SGD, L2 and weight decay give identical trajectories.
- With no loss gradient, AdamW shrank a weight by exactly 0.999ᵗ, to 0.135 after 2000 steps. Adam + L2 drove it to zero within about 100 steps, because Adam normalises the λw gradient to a step of about η regardless of λ.
- Under a constant unit gradient, AdamW settled at w = −1/λ = −10, its fixed point.
Practice.
- Typical AdamW weight decay is 0.01–0.1. PyTorch’s AdamW applies η·λ per step, so decay strength changes with the learning-rate schedule. The original paper scaled decay by the schedule multiplier only.
- Keep ηλ well below 1: at ηλ = 1 every weight is zeroed each step.
- Use parameter groups to exclude biases, LayerNorm and BatchNorm parameters, and often embeddings, from decay.
- When reproducing a paper, check whether “weight decay” meant L2 in the loss or decoupled decay.
torch.optim.AdamW([{"params": decay, "weight_decay": 0.05}, {"params": no_decay, "weight_decay": 0.0}], lr=3e-4)- 1. PyTorch's AdamW applies decay per step of…
- 2. With ηλ = 1, decoupled decay…
- 3. Without a loss gradient, Adam + L2 moves a weight by about…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why they differ. Adam’s step for coordinate i is η(gᵢ + λwᵢ)/√v̂ᵢ when L2 is added. Coordinates with large historical gradients (large v̂ᵢ) receive proportionally weaker regularisation, and those with small ones stronger. Loshchilov and Hutter (2019) argued this is undesirable: the amount of regularisation should not depend on how noisy or large a weight’s gradients happen to be. Decoupled decay shrinks every weight by the same factor.
But fixed points matter too. On a convex problem, whatever optimiser is used, minimising L + λ‖w‖²/2 converges to the same regularised optimum, the ridge solution for least squares. Adam + L2 is just another way to reach it. AdamW’s fixed point is the solution of m̂/√v̂ = −λw, which depends on the gradient statistics and is not the minimiser of any fixed regularised loss. Which generalises better is an empirical question. Exercise 2 finds the classical ridge solution better on convex least squares.
Where AdamW’s advantage comes from. In deep networks with learning-rate schedules, three effects favour decoupled decay:
- Weight decay’s interaction with normalisation layers acts on the effective learning rate.
- The decay and learning-rate hyperparameters become more separable to tune.
- Large-gradient parameters still get regularised.
The original paper and later practice (transformers, ConvNeXt, ViT) report consistent gains there.
- 1. Under Adam + L2, parameters with large gradient histories are regularised…
- 2. On convex least squares, Adam + L2 converges to…
- 3. AdamW was introduced by…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Is AdamW always better?
History.
- A. Krogh and J. Hertz (1992) analysed weight decay in neural networks.
- I. Loshchilov and F. Hutter (2017 preprint, 2019 ICLR) showed L2 and weight decay differ for adaptive methods and introduced AdamW.
- G. Zhang et al. (2019) analysed weight decay’s interaction with normalisation (effective learning rate).
- AdamW became the default for BERT, GPT and ViT training.
Exercise 2. Over-parameterised linear regression: 80 examples, 160 features, half of them at scale 10 and half at scale 1, with only 10 relevant features. Adam with L2 in the loss and AdamW are each trained for 3000 full-batch steps at η = 0.01, with λ chosen on a grid for the lowest test MSE.
| seed | method | best λ | test MSE | predicting zero |
|---|---|---|---|---|
| 0 | Adam + L2 | 1000 | 5.63 | 5.73 |
| 0 | AdamW | 100 | 5.73 (all weights zeroed) | 5.73 |
| 1 | Adam + L2 | 100 | 2.84 | 4.52 |
| 1 | AdamW | 1 | 3.19 | 4.52 |
| 2 | Adam + L2 | 100 | 7.05 | 11.40 |
| 2 | AdamW | 1 | 10.80 | 11.40 |
At the constant rate η = 0.01, Adam + L2 keeps taking steps of about η and hovers around the optimum, so how far it ends from the closed-form ridge solution depends on rounding (2–5% natively, up to 24% in the browser). Repeating the run with the rate annealed to zero over the last 1000 steps landed on the ridge solution to within 10⁻¹⁵ relative error in every seed.
- Adam + L2 was at least as good in every seed. On convex least squares it converges toward the ridge solution, the classical regularised estimator.
- AdamW converged to a different solution. It left irrelevant weights larger in both feature groups (seed 2: 0.226 against 0.096 for the scale-10 group). Its fixed point balances Adam’s normalised direction against decay, not the loss against a penalty.
- Seed 0 had little signal. AdamW’s best setting there (ηλ = 1) simply zeroed every weight each step.
- Lesson: AdamW’s reported advantages come from deep networks with normalisation and schedules. In a convex problem, L2 in the loss gives the principled solution. Test both on the problem at hand.
AdamW in AI, robotics and aerospace
- Transformers for perception, planning and language are trained almost universally with AdamW and weight decay around 0.05–0.1.
- Fine-tuning pretrained models on small aerospace datasets relies on weight decay to avoid drifting far from the pretrained weights.
- Classical models: for convex models (linear, logistic), use an L2 penalty in the loss, or a dedicated solver, rather than AdamW.
References
- I. Loshchilov, F. Hutter (2019). “Decoupled weight decay regularization.” Proc. ICLR.
- A. Krogh, J. A. Hertz (1992). “A simple weight decay can improve generalization.” Advances in NeurIPS 4.
- G. Zhang, C. Wang, B. Xu, R. Grosse (2019). “Three mechanisms of weight decay regularization.” Proc. ICLR.
- A. Dosovitskiy et al. (2021). “An image is worth 16×16 words: transformers for image recognition at scale.” Proc. ICLR.