TL;DR
AdamW is the Adam update with one change: the weight penalty is subtracted from the weights directly, not added to the gradient. This makes the penalty behave the way people meant it to, shrinking each weight by a fixed fraction every step. Adam with a standard L2 penalty does not do this, because the adaptive scaling silently changes how strong the penalty is for each weight.
Builds on: Adam Optimizer, L2 Regularization · Leads to: Fixing Weight Decay Regularization in Adam
Where it's used
Training large language models. Transformer language models with hundreds of millions to billions of parameters are commonly trained with AdamW. Its weight decay keeps the weights from growing without bound over a run of hundreds of thousands of steps.
Fine-tuning pretrained models. When a model pretrained on a large dataset is adapted to a smaller task, practitioners use AdamW with a small learning rate, often around , and a modest decay. The decay gently pulls the weights back towards zero, which limits how far the model can drift from its pretrained values and so reduces Overfitting to the small dataset.
Default settings in libraries. Deep learning frameworks such as PyTorch provide AdamW as a standard optimizer, and training recipes for vision and language models frequently name it. A practitioner who sees "weight decay 0.1" in a recipe is almost always looking at AdamW's decoupled form, so knowing what the number does matters when you copy a recipe.
How it works
Start with one weight and a learning rate . Suppose the loss gradient for this weight is at the current step, so only the penalty acts. The penalty strength is , and the weight is . Here is what AdamW does in one step:
- Compute the loss gradient for the weight. It is .
- Update the running average of the gradient, , and of its square, . Both stay , so after bias correction the adaptive part is . Check: .
- Compute the decay term separately: .
- Subtract both from the weight, with the learning rate multiplying the whole step: .
- Check the result. The weight shrank by , which is exactly . Shrinkage is a fixed fraction of the weight, here percent.
Now run the same case with Adam and a standard L2 penalty. The penalty is added to the gradient first, so the gradient is . At the first step the bias-corrected average equals this gradient, and the square-root average is its absolute value, so the adaptive ratio is . The update is . The weight shrank by , which is five times the AdamW shrinkage, and the difference came from the adaptive scaling.
flowchart LR
G[Loss gradient g] --> A[Adaptive step m hat over sqrt v hat]
T[Weight theta] --> D[Decay alpha times lambda times theta]
A --> S[Subtract from theta]
D --> S
S --> N[New weight]
The diagram is the AdamW rule. The loss gradient goes through the adaptive part, and the decay term bypasses it and goes straight to the subtraction.
The math
Write for the weights after step , for the learning rate, for the weight-decay strength and for the small constant that prevents division by zero. The loss gradient at step is , computed from the loss alone.
Step 1 in symbols: the loss gradient
The gradient contains no penalty. It is the slope of the data loss at the current weights, exactly as in Gradient Descent.
Step 2 in symbols: Adam's averages
Adam keeps two running averages (see Adam Optimizer):
and corrects their start-up bias to and . The averages are element by element, so each weight has its own and .
Step 3 in symbols: the adaptive step
The adaptive part of the update is
It is the direction given by the averaged gradient, divided by the typical size of that gradient. Weights with large past gradients take small steps, and weights with small past gradients take larger ones.
Step 4 in symbols: the decoupled decay
AdamW applies the penalty as a separate term that does not pass through the averages:
When the loss gradient is zero, the two averages decay to zero too, and the update reduces to . Each step multiplies the weight by the same factor . This is the "fixed fraction" from the walkthrough.
Two conventions for the decay term. The formula above is the one most libraries implement (PyTorch's AdamW multiplies the decay by the learning rate). The paper's Algorithm 2 writes it with a schedule multiplier instead: , so there the decay is scaled by the schedule, not by . Both keep the decay outside the adaptive step, which is the point of AdamW, but a given means a different amount of decay in each convention, so check which one a codebase uses before copying a value.
Comparison with L2 inside Adam
The L2 version adds the penalty to the gradient before the averages:
The penalty's contribution to the step is now divided by . If the penalty is small next to the loss history, the decay on one weight is roughly . Weights with a large gradient history (large ) are decayed weakly, and weights with a small history are decayed strongly. The penalty's strength therefore depends on the gradient, which is the opposite of what L2 regularization is meant to do.
Worked examples
Use , , , and a loss gradient of . This is the first step, so the bias corrections make and .
- AdamW. The loss gradient is , so and , and the adaptive part is .
- The decay term is .
- The update is .
- L2 inside Adam. The penalised gradient is . Then , , and .
- The adaptive ratio is , so .
- Interpret. Both methods used the same penalty, but AdamW moved the weight to and L2 inside Adam moved it to . The L2 version normalised the penalty away and made it a full-size step.
Take two weights, and , both currently , with and . Suppose the loss gradient is zero at this step, so only the penalty acts. Weight has a long history of large gradients, so its running average has . Weight has a history of small gradients, with .
- The penalty term is for both weights.
- AdamW: the decay on each weight is . Both weights shrink by , so both become .
- L2 inside Adam: the penalty's contribution to the step is . For this is .
- For it is . (This treats each weight's gradient history as fixed for this step, which is the point of the comparison; the history itself also changes with the penalty.)
- So under L2 inside Adam, moves to and moves to . Weight is decayed times as strongly as .
- Interpret. AdamW gives both weights the same shrink, percent, as the penalty promises. The L2 version decays weights according to their gradient history, so the penalty is not the same for every weight.
A pretrained weight has value . It is fine-tuned on a small dataset with AdamW, learning rate and decay . For this weight the loss gradient is close to zero over the 1,000 steps, so the decay dominates. Find the weight after 1,000 steps.
- Under decay alone, each step multiplies the weight by .
- After 1,000 steps the factor is . Use for small : .
- The weight is , which is about .
- Interpret. Over the run the weight lost about percent of its value. The decay is gentle enough that the fine-tuning loss, not the penalty, decides where most weights end up.
Try it: Use , and steps on a weight of , again with zero loss gradient. What is the weight at the end, to two decimal places?
Answer: The step factor is . The total is . The weight is , which rounds to (since ). Doubling roughly doubled the exponent, so the shrink from to is about the square of the earlier factor.
Common mistakes and tradeoffs
Mistake: "AdamW is Adam with a bigger penalty." It is tempting to think AdamW only changes the strength of regularization. In fact it changes where the penalty enters the update. The same gives different behaviour in the two methods, as Example 2 shows, so a value tuned for one should not be copied to the other without checking.
Mistake: "Weight decay and L2 regularization are the same thing." It is tempting to think they are interchangeable. In fact they are equivalent for plain gradient descent with a suitable scaling of , but not for adaptive methods. For Adam the L2 gradient is divided by the adaptive scale, so the two differ. The term "weight decay" in AdamW means the decoupled form.
Mistake: "The learning rate does not affect the decay." It is tempting to think alone sets the shrinkage. In fact the shrink per step is , so a schedule that lowers the learning rate also weakens the decay. This is usually what you want, but it surprises people who tune and then change .
Mistake: "Penalise every parameter the same way." Many practitioners exclude bias terms and normalisation scales from the decay, because shrinking them does little for generalisation. Check how your library groups parameters before assuming all of them decay.
When to use it. Use AdamW when you train or fine-tune deep networks with an adaptive optimizer and want regularization that you can reason about. It is the usual choice for transformers.
When not to use it. For some simple models, or for training pipelines built around plain SGD with momentum, a standard L2 penalty and SGD are simpler and well understood. Adaptive methods are not always better in the final accuracy either, so the choice should be tested.
Alternatives. Adam with L2 is the older version and still appears in old code. Plain Stochastic Gradient Descent with an L2 penalty is simpler and works well for convex problems. Techniques such as Dropout regularize without changing the optimizer.
History
The original Adam paper in 2014 and 2015 showed a practical adaptive method, and its implementations included an L2 penalty added to the gradient, as was common at the time. Practitioners used it widely, and it became the default for many deep networks. Over time some people noticed that Adam with L2 did not regularize as well as SGD with the same penalty, and the reason was not clear.
In 2017 Ilya Loshchilov and Frank Hutter posted a paper, later published at ICLR 2019, that identified the coupling between the penalty and the adaptive scale. They proposed decoupling the weight decay from the gradient, which gives AdamW. The paper showed that the decoupled version generalised better on image benchmarks, and that the separation made the decay easier to tune. Frameworks added AdamW as a standard option soon after, and it is now common in language-model training.
Recap
- Adam divides a smoothed gradient by the root of a smoothed squared gradient, giving each weight its own step.
- Adding an L2 penalty to the gradient inside Adam makes the penalty depend on the gradient history of each weight.
- AdamW applies the penalty directly to the weights, so each step shrinks every weight by the fixed fraction .
- The decoupled form is what people mean by "weight decay" in modern training.
- The learning rate and the decay strength interact, because the shrink per step is their product.
What to read next: Fixing Weight Decay Regularization in Adam gives the original argument and experiments for the decoupled form.
Check yourself
QuestionRecall: what is the difference between AdamW and Adam with an L2 penalty, in one sentence?Show answer
Adam with L2 adds the penalty to the gradient before the adaptive averages, so the penalty is rescaled by the weight's gradient history. AdamW subtracts the penalty from the weights outside the adaptive step, so every weight shrinks by the same fraction per step.
QuestionComputation: a weight equals , the learning rate is and the decay is . The loss gradient is zero. What is the weight after one AdamW step?Show answer
The decay term is . The new weight is .
QuestionWhy: explain why the L2 penalty inside Adam is weaker for weights with large gradient histories.Show answer
Inside Adam the penalty is part of the gradient, and the step divides it by , the typical size of that weight's gradient. A large history gives a large , so the penalty's contribution to the step is small. AdamW does not divide the decay by anything, so the shrink is the same for all weights.
QuestionApplied: you fine-tune a model with AdamW and reduce the learning rate by a factor of 10 halfway through training. Predict what happens to the decay per step, and whether you should retune the decay.Show answer
The decay per step is , so it also falls by a factor of 10 after the change. Weights are shrunk less in the second half, which is often the intended effect of a schedule. You should not retune just because changed, unless you want the total decay over the run to match a specific target. If you do, compute the total shrink over the schedule.