Search

Search papers and lessons

AdamW Optimizer

Level 4 · 13 min read

AdamW is Adam with weight decay applied directly to the weights, outside the adaptive step, so the penalty shrinks every weight by the same fraction regardless of its gradient history.

On this page

  • Colour = topic mix
  • Discrete math
  • Optimizers
  • Calculus
  • Linear algebra
  • Probability
  • Learning basics
  • ReadReady to readNot yet

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 10−510^{-5}, 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 θ\theta and a learning rate α=0.5\alpha = 0.5. Suppose the loss gradient for this weight is 00 at the current step, so only the penalty acts. The penalty strength is λ=0.1\lambda = 0.1, and the weight is θ=2\theta = 2. Here is what AdamW does in one step:

  1. Compute the loss gradient gg for the weight. It is 00.
  2. Update the running average of the gradient, mm, and of its square, vv. Both stay 00, so after bias correction the adaptive part m^/(v^+ϵ)\hat m / (\sqrt{\hat v} + \epsilon) is 00. Check: 0/(0+ϵ)=00 / (0 + \epsilon) = 0.
  3. Compute the decay term separately: λθ=0.1×2=0.2\lambda \theta = 0.1 \times 2 = 0.2.
  4. Subtract both from the weight, with the learning rate multiplying the whole step: θnew=2−0.5×(0+0.2)=2−0.1=1.9\theta_{\text{new}} = 2 - 0.5 \times (0 + 0.2) = 2 - 0.1 = 1.9.
  5. Check the result. The weight shrank by 0.10.1, which is exactly αλθ=0.5×0.1×2\alpha \lambda \theta = 0.5 \times 0.1 \times 2. Shrinkage is a fixed fraction of the weight, here 55 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 g+λθ=0+0.2=0.2g + \lambda\theta = 0 + 0.2 = 0.2. 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 0.2/0.2=10.2 / 0.2 = 1. The update is θnew=2−0.5×1=1.5\theta_{\text{new}} = 2 - 0.5 \times 1 = 1.5. The weight shrank by 0.50.5, 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 θt\theta_t for the weights after step tt, α\alpha for the learning rate, λ\lambda for the weight-decay strength and ϵ\epsilon for the small constant that prevents division by zero. The loss gradient at step tt is gt=∇L(θt−1)g_t = \nabla L(\theta_{t-1}), computed from the loss alone.

Step 1 in symbols: the loss gradient

The gradient gtg_t 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):

mt=β1mt−1+(1−β1) gt,vt=β2vt−1+(1−β2) gt2,m_t = \beta_1 m_{t-1} + (1-\beta_1)\, g_t, \qquad v_t = \beta_2 v_{t-1} + (1-\beta_2)\, g_t^2,

and corrects their start-up bias to m^t=mt/(1−β1t)\hat m_t = m_t / (1-\beta_1^t) and v^t=vt/(1−β2t)\hat v_t = v_t / (1-\beta_2^t). The averages are element by element, so each weight has its own m^t\hat m_t and v^t\hat v_t.

Step 3 in symbols: the adaptive step

The adaptive part of the update is

st=m^tv^t+ϵ.\mathbf{s}_t = \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon}.

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:

θt=θt−1−α(m^tv^t+ϵ+λ θt−1).\theta_t = \theta_{t-1} - \alpha\left( \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon} + \lambda\, \theta_{t-1} \right).

When the loss gradient is zero, the two averages decay to zero too, and the update reduces to θt=θt−1(1−αλ)\theta_t = \theta_{t-1}(1 - \alpha\lambda). Each step multiplies the weight by the same factor 1−αλ1 - \alpha\lambda. 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 ηt\eta_t instead: θt=θt−1−ηt(αm^tv^t+ϵ+λ θt−1)\theta_t = \theta_{t-1} - \eta_t\left(\alpha \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon} + \lambda\, \theta_{t-1}\right), so there the decay is scaled by the schedule, not by α\alpha. Both keep the decay outside the adaptive step, which is the point of AdamW, but a given λ\lambda 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:

g~t=gt+λ θt−1,θt=θt−1−αm^(g~)tv^(g~)t+ϵ.\tilde g_t = g_t + \lambda\, \theta_{t-1}, \qquad \theta_t = \theta_{t-1} - \alpha \frac{\hat m(\tilde g)_t}{\sqrt{\hat v(\tilde g)_t} + \epsilon}.

The penalty's contribution to the step is now divided by v^t\sqrt{\hat v_t}. If the penalty is small next to the loss history, the decay on one weight is roughly αλθ/v^t\alpha \lambda \theta / \sqrt{\hat v_t}. Weights with a large gradient history (large v^t\sqrt{\hat v_t}) 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

Example
Example 1: one weight, one step (tiny)

Use θ0=2\theta_0 = 2, α=0.5\alpha = 0.5, λ=0.1\lambda = 0.1, and a loss gradient of 00. This is the first step, so the bias corrections make m^1=m1\hat m_1 = m_1 and v^1=v1\hat v_1 = v_1.

  1. AdamW. The loss gradient is g1=0g_1 = 0, so m^1=0\hat m_1 = 0 and v^1=0\hat v_1 = 0, and the adaptive part is 0/(0+ϵ)=00 / (0 + \epsilon) = 0.
  2. The decay term is λθ0=0.1×2=0.2\lambda \theta_0 = 0.1 \times 2 = 0.2.
  3. The update is θ1=2−0.5×(0+0.2)=1.9\theta_1 = 2 - 0.5 \times (0 + 0.2) = 1.9.
  4. L2 inside Adam. The penalised gradient is g~1=0+0.1×2=0.2\tilde g_1 = 0 + 0.1 \times 2 = 0.2. Then m^1=0.2\hat m_1 = 0.2, v^1=0.04\hat v_1 = 0.04, and v^1=0.2\sqrt{\hat v_1} = 0.2.
  5. The adaptive ratio is 0.2/(0.2+ϵ)≈10.2 / (0.2 + \epsilon) \approx 1, so θ1=2−0.5×1=1.5\theta_1 = 2 - 0.5 \times 1 = 1.5.
  6. Interpret. Both methods used the same penalty, but AdamW moved the weight to 1.91.9 and L2 inside Adam moved it to 1.51.5. The L2 version normalised the penalty away and made it a full-size step.
Example
Example 2: two weights with different histories (standard)

Take two weights, AA and BB, both currently θ=1\theta = 1, with α=0.1\alpha = 0.1 and λ=0.1\lambda = 0.1. Suppose the loss gradient is zero at this step, so only the penalty acts. Weight AA has a long history of large gradients, so its running average has v^=10\sqrt{\hat v} = 10. Weight BB has a history of small gradients, with v^=0.1\sqrt{\hat v} = 0.1.

  1. The penalty term is λθ=0.1×1=0.1\lambda \theta = 0.1 \times 1 = 0.1 for both weights.
  2. AdamW: the decay on each weight is αλθ=0.1×0.1×1=0.01\alpha \lambda \theta = 0.1 \times 0.1 \times 1 = 0.01. Both weights shrink by 0.010.01, so both become 0.990.99.
  3. L2 inside Adam: the penalty's contribution to the step is αλθ/v^\alpha \lambda \theta / \sqrt{\hat v}. For AA this is 0.01/10=0.0010.01 / 10 = 0.001.
  4. For BB it is 0.01/0.1=0.10.01 / 0.1 = 0.1. (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.)
  5. So under L2 inside Adam, AA moves to 0.9990.999 and BB moves to 0.900.90. Weight BB is decayed 100100 times as strongly as AA.
  6. Interpret. AdamW gives both weights the same shrink, 11 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.
Example
Example 3: fine-tuning a pretrained weight (realistic)

A pretrained weight has value 0.80.8. It is fine-tuned on a small dataset with AdamW, learning rate α=10−3\alpha = 10^{-3} and decay λ=0.1\lambda = 0.1. 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.

  1. Under decay alone, each step multiplies the weight by 1−αλ=1−10−3×0.1=1−10−4=0.99991 - \alpha\lambda = 1 - 10^{-3} \times 0.1 = 1 - 10^{-4} = 0.9999.
  2. After 1,000 steps the factor is (0.9999)1000(0.9999)^{1000}. Use (1−x)n≈e−nx(1 - x)^n \approx e^{-nx} for small xx: e−1000×10−4=e−0.1≈0.905e^{-1000 \times 10^{-4}} = e^{-0.1} \approx 0.905.
  3. The weight is 0.8×0.905=0.7240.8 \times 0.905 = 0.724, which is about 0.720.72.
  4. Interpret. Over the run the weight lost about 9.59.5 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 α=10−3\alpha = 10^{-3}, λ=0.2\lambda = 0.2 and 1,0001{,}000 steps on a weight of 0.80.8, again with zero loss gradient. What is the weight at the end, to two decimal places?

Answer: The step factor is 1−10−3×0.2=0.99981 - 10^{-3} \times 0.2 = 0.9998. The total is e−1000×2×10−4=e−0.2≈0.8187e^{-1000 \times 2 \times 10^{-4}} = e^{-0.2} \approx 0.8187. The weight is 0.8×0.8187≈0.6550.8 \times 0.8187 \approx 0.655, which rounds to 0.650.65 (since 0.80×0.81873=0.654990.80 \times 0.81873 = 0.65499). Doubling λ\lambda roughly doubled the exponent, so the shrink from 0.9050.905 to 0.8190.819 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 λ\lambda 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 λ\lambda, 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 λ\lambda alone sets the shrinkage. In fact the shrink per step is αλ\alpha\lambda, so a schedule that lowers the learning rate also weakens the decay. This is usually what you want, but it surprises people who tune λ\lambda and then change α\alpha.

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 αλ\alpha\lambda.
  • 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 λθ\lambda\theta 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 αλ\alpha\lambda per step.

QuestionComputation: a weight equals 1.01.0, the learning rate is α=0.01\alpha = 0.01 and the decay is λ=0.5\lambda = 0.5. The loss gradient is zero. What is the weight after one AdamW step?Show answer

The decay term is αλθ=0.01×0.5×1.0=0.005\alpha \lambda \theta = 0.01 \times 0.5 \times 1.0 = 0.005. The new weight is 1.0−0.005=0.9951.0 - 0.005 = 0.995.

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 v^\sqrt{\hat v}, the typical size of that weight's gradient. A large history gives a large v^\sqrt{\hat v}, 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 αλ\alpha\lambda, 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 λ\lambda just because α\alpha changed, unless you want the total decay over the run to match a specific target. If you do, compute the total shrink ∏t(1−αtλ)\prod_t (1 - \alpha_t\lambda) over the schedule.

Sources