Topic 18 of 62 · How a model learns
Adam vs AdamW
MediumWhat changes between Adam with L2 regularization and AdamW, and why does it matter?
1 · Start with the intuition
There are two different jobs here. Adam uses past gradients to choose an update for each parameter. Weight decay asks each selected weight to shrink by a small fraction. If we put that shrinkage into the gradient, Adam will treat it like any other signal: mix it into its history and adaptively scale it. AdamW keeps the jobs separate, so the shrinkage means what we intended.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Update task moments
Update the exponential moving averages of the task gradient (mₜ) and squared task gradient (vₜ).
Next, we will look at: Compute the adaptive step. Move on when this operation makes sense.
Visualizing the behavior
How much each weight shrinks per step (η = 0.001, λ = 0.1, θ = 1)
IllustrativeAdamW: decay = η·λ·θ, the same for every weight
Adam + L2: decay part ≈ η·λ·θ / √v̂
3 · The governing equation
gₜ = ∇L_task(θₜ₋₁) mₜ = β₁mₜ₋₁ + (1 − β₁)gₜ, vₜ = β₂vₜ₋₁ + (1 − β₂)gₜ² m̂ₜ = mₜ / (1 − β₁ᵗ), v̂ₜ = vₜ / (1 − β₂ᵗ) AdamW: θₜ = (1 − ηλ)θₜ₋₁ − η m̂ₜ / (√v̂ₜ + ε)
Notation: η is the learning rate, λ is the weight-decay coefficient, and β₁, β₂ are the exponential decay rates for the first and second moment estimates (applied coordinate-wise).
Start with plain SGD: θ − η(g + λθ) can be rearranged as (1 − ηλ)θ − ηg. Here, an L2 penalty and direct weight decay give the same update. Now replace SGD with Adam. The combined gradient g + λθ enters both moment estimates before the adaptive update is computed, so that simple rearrangement no longer works. AdamW restores a direct shrinkage step, (1 − ηλ)θ, alongside the task-gradient update. Notice that decay still depends on η; decoupled does not mean independent of the learning rate.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Saying “AdamW is just Adam with weight decay turned on.” Standard Adam implementations already had a weight_decay argument, but it implemented coupled L2 regularization inside the gradient. The key change in AdamW is decoupling the decay from the adaptive second-moment scaling.
In practice · implementation & engineering rule of thumb
Split parameters into two optimizer groups: apply weight_decay (e.g. 0.1) only to 2D+ weight matrices, and set weight_decay=0.0 on 1D tensors (biases, RMSNorm/LayerNorm gains).
decay_params = [p for n, p in model.named_parameters() if p.requires_grad and p.ndim >= 2]
nodecay_params = [p for n, p in model.named_parameters() if p.requires_grad and p.ndim < 2]
optimizer = torch.optim.AdamW([
{"params": decay_params, "weight_decay": 0.1},
{"params": nodecay_params, "weight_decay": 0.0},
], lr=3e-4, betas=(0.9, 0.95), fused=True)Go one level deeper
Follow-ups that often come after a correct first answer.
- Why bias correction exists
- Both moments start at zero, so early estimates are biased toward zero. At step 1 with β₂ = 0.999, v₁ = 0.001·g², and dividing by 1 − β₂¹ = 0.001 recovers g². Without the correction, the first steps would be far too large, because √v would be tiny.
- What to decay, and how it interacts with the schedule
- A common practice is to exclude biases and normalization gains (and often embeddings) from weight decay. Because AdamW’s shrink per step is ηₜ·λ, changing the learning-rate schedule also changes the effective regularization, so λ is usually retuned alongside it. Adam also stores two extra FP32 states per parameter, 4 bytes each (8 bytes together).
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumIf λ = 0, is there any difference between Adam and AdamW?
Show follow-up reasoning
No. When weight decay λ is zero, the (1 − ηλ) factor is 1 and the L2 gradient term λθ is 0, so the two update rules are identical.