Start studying
Module 2 · Deep Learning Module contents
Module 2  ·  Card 18 / 25intermediate

AdamW

AdamW is Adam with weight decay handled correctly.

Plain Adam mixes L2 penalty into the gradient, which interacts badly with its adaptive scaling.

AdamW instead decays the weights as a separate step.

This gives noticeably better generalisation in practice.

It is the standard optimizer for training transformers and large models.

Learn in depth ↓
Module 02 · Deep Learning · 25 concepts
In depth  /  15 lines

AdamW, in more detail

AdamW is Adam with one bug fixed, and that fix is worth a version of its own.

Weight decay pulls every weight slightly towards zero on each step.

It keeps weights small, which limits how sharply the model can fit its training data.

The old way was to add an L2 penalty to the loss and let the gradient carry it.

That works with plain SGD, where the penalty reaches every weight equally.

It breaks with Adam, which divides each gradient by that weight's own noise estimate.

The decay is divided too, so noisy weights are barely decayed and quiet ones are decayed hard.

The regularisation you asked for lands unevenly and largely at random.

AdamW takes the decay out of the gradient and applies it as a separate subtraction.

Every weight is then decayed by the same fraction, as intended.

The W stands for decoupled weight decay, which is exactly what changed.

The measured result is better generalisation across most tasks.

It is the standard optimizer for transformers and every large model in current use.

Biases and normalisation parameters are usually excluded from decay.

If you are choosing between them today, choose AdamW.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath AdamW

The bug it fixes

L2 regularisation and weight decay are the same thing for plain SGD, and they are not the same thing for Adam.

L′=L+λ2∥θ∥2  ⟹  g′=g+λθ\mathcal{L}' = \mathcal{L} + \frac{\lambda}{2}\|\boldsymbol{\theta}\|^2 \implies \mathbf{g}' = \mathbf{g} + \lambda \boldsymbol{\theta}

L2 adds the penalty to the loss, so the decay term enters as part of the gradient.

θ←θ−ηg+λθv^+ϵ\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \eta \frac{\mathbf{g} + \lambda \boldsymbol{\theta}}{\sqrt{\hat{v}} + \epsilon}

In Adam that gradient is then divided by the running root mean square of past gradients, which divides the decay term too.

A parameter with large historical gradients gets a large denominator, so its decay is scaled down. A parameter with tiny gradients is decayed hard.

The amount of regularisation a weight receives therefore depends on its gradient history, which is not what anybody asking for weight decay meant.

Decoupling it

θ←θ−η(m^v^+ϵ+λθ)\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \eta \left( \frac{\hat{m}}{\sqrt{\hat{v}} + \epsilon} + \lambda \boldsymbol{\theta} \right)

AdamW applies the decay to the parameters directly, outside the adaptive rescaling.

  • m^\hat{m}: the bias-corrected first moment, the mean of recent gradients.
  • v^\hat{v}: the bias-corrected second moment, the mean of their squares.
  • λ\lambda: the decay rate, now meaning the same thing for every parameter.

Every weight is now pulled towards zero by the same proportion per step, which is what weight decay is supposed to be.

One step, in order

  1. Compute the gradient of the batch.
  2. Update the first and second moments as in Adam.
  3. Apply bias correction to both.
  4. Take the adaptive step from those moments.
  5. Separately, multiply the parameters by 1−ηλ1 - \eta\lambda.

Steps four and five are independent, and keeping them independent is the entire contribution of the method.

What it changed in practice

λ≈0.01 to 0.1\lambda \approx 0.01 \text{ to } 0.1

Decoupling made the decay rate tunable again. Under Adam with L2, changing the learning rate silently changed the effective regularisation, so the two could not be tuned separately.

AdamW is now the default optimiser for transformers, and every current large language model is trained with it.

What is usually excluded from the decay

Biases, and the gain and shift parameters of normalisation layers, are normally left out of the decay entirely.

They are few, they are not a capacity risk, and shrinking a normalisation gain towards zero attacks the layer's ability to scale rather than its ability to overfit.

Most frameworks do not do this by default, so it is a parameter group that has to be written by hand, and it is worth a few tenths of a point.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What does AdamW correct in the original Adam?
Deep Learning

25 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close