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
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.
Adam rescales the decay too
AdamW: decay outside the moments
Two weights, two decay amounts
Which parameters get decayed
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
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.
Bytewise
All courses