All courses

AdamW and decoupled weight decay

Explain why adding L2 to the loss interacts badly with Adam's rescaling, and how AdamW applies θ ← θ − ηλθ separately. Explain why norm gains and biases are usually exempt from decay.

Builds on: The Adam optimizer, LayerNorm and RMSNorm

▶ Watch this lesson Start the whole course

What the animation says

What decay should do

Weight decay pulls every weight toward zero by the same fraction each step. Weights stay small unless the data keeps pushing them up. With plain gradient descent, you add a penalty to the loss: half lambda times the squared weights. Its gradient is lambda times the weight, so each weight shrinks in proportion to itself. But LLMs train with Adam, which divides every step by the size of recent gradients. Two equal weights: one with big noisy gradients, one with tiny ones. Under Adam plus this penalty, which shrinks faster?

Adam rescales the decay too

The quiet one shrinks faster. The penalty's gradient, lambda theta, joins the loss gradient before Adam sees it. So it flows into both moving averages and gets divided by root v-hat, like everything else. Numbers: learning rate a thousandth, lambda a hundredth, both weights equal one. A's root v-hat is one, so its decay step is ten to the minus five. B's is point oh five, so its step is twenty times bigger. The pull now depends on gradient history, not on the weight itself.

AdamW: decay outside

The fix is AdamW, decoupled weight decay. Adam plus L2 adds lambda theta to the gradient, then builds both moments from that sum. AdamW builds the moments from the loss gradient only. Then, after the usual Adam step, it subtracts learning rate times lambda times theta, straight from the weight. That decay never touches v-hat, so nothing divides it. Your turn: drag the decay term to where AdamW puts it.

Equal pull again

Same two weights, now under AdamW. The decay term is learning rate times lambda times theta. No v-hat anywhere in it. A gets ten to the minus five. B gets ten to the minus five. Equal pull, as intended. Each step, every decayed weight is multiplied by one minus learning rate times lambda. In practice lambda is often point one, and epsilon is just a tiny constant.

Which parameters decay

One more rule: decay the weight matrices, skip the one-dimensional vectors. A norm gain's neutral value is one, not zero. Decay drags it down, and the loss must keep fighting back. Biases and gains are tiny next to the big matrices, so decaying them buys almost no regularization. In code: if a parameter has two or more dimensions, apply the decay line. Otherwise, leave it alone. Tap the parameters AdamW usually leaves alone.

Why can't Adam just add L2 to the loss?

Weight decay is meant to pull every weight toward zero by the same fraction each step. Weights stay small unless the data keeps pushing them up. With plain SGD, adding $\tfrac{\lambda}{2}\|\theta\|^2$ to the loss does exactly that. LLMs are trained with Adam, though, and there the same trick quietly gives some weights strong decay and others almost none. Why does that happen, and what do we do instead?

Adam rescales the decay too

The L2 penalty's gradient is $\lambda\theta$. It is added to the loss gradient $g$, so it flows into $m$ and $v$ and gets divided by $\sqrt{\hat v}$ like everything else. - Weights with large, noisy gradients (big $\hat v$) have their decay divided by a big number, so they barely shrink. - Weights with tiny gradients (small $\hat v$) have their decay divided by a small number, so they shrink far more. The pull toward zero now depends on gradient history, not on the weight itself.

AdamW: decay outside the moments

**Adam + L2:** $\tilde g = g + \lambda\theta$. Build $m, v$ from $\tilde g$, then $\theta \leftarrow \theta - \eta\,\hat m/(\sqrt{\hat v}+\epsilon)$. **AdamW:** build $m, v$ from $g$ only, then $$\theta \leftarrow \theta - \eta\frac{\hat m}{\sqrt{\hat v}+\epsilon} - \eta\lambda\theta.$$ The last term shrinks every decayed weight by the same factor $(1-\eta\lambda)$ per step, whatever its gradients. $\lambda$ is the decay coefficient (often $0.1$) and $\epsilon$ is a tiny constant.

Two weights, two decay amounts

Set $\eta=10^{-3}$, $\lambda=0.01$ and both weights $\theta=1$, so $\lambda\theta=0.01$. Weight A has $\sqrt{\hat v}=1$; weight B has $\sqrt{\hat v}=0.05$. Assume the loss gradient dominates $v$. **Adam + L2:** the decay part of the step is $\eta\lambda\theta/\sqrt{\hat v}$. A gets $10^{-5}$ and B gets $2\times10^{-4}$, so B shrinks 20× faster. **AdamW:** both get $\eta\lambda\theta = 10^{-5}$. Equal pull, as intended.

Which parameters get decayed

Common rule: decay matrices and skip 1-D vectors (biases, norm gains). - A norm gain's neutral value is 1, not 0. Decay drags it toward 0 and shrinks the normalized output whose scale the norm was meant to fix. The loss gradient must keep fighting back. - Biases and gains are a few vectors of length $d$ or $d_{ff}$ per layer, a tiny fraction of the parameters next to $d\times d_{ff}$ matrices. Decaying them buys almost no regularization.
import numpy as np
params = {"W_mlp": np.ones((4, 16)), "b_mlp": np.zeros(16),
          "norm_gain": np.ones(4)}
eta, lam = 1e-3, 0.1
for name, p in params.items():
    if p.ndim >= 2:            # weight matrices: decay
        p -= eta * lam * p     # AdamW decay term
    # 1-D vectors (biases, norm gains): no decay
print({k: float(v.ravel()[0]) for k, v in params.items()})
# {'W_mlp': 0.9999, 'b_mlp': 0.0, 'norm_gain': 1.0}

Remember

Under Adam, an L2 term in the loss gets divided by $\sqrt{\hat v}$. Decay becomes uneven: weak for weights with big gradients, strong for quiet ones. AdamW subtracts $\eta\lambda\theta$ directly, outside the moments, and usually only from weight matrices, not from norm gains or biases.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.