ML Systems Lab Open interactive version →
Intermediate 50 min read adamadamwbias-correctionweight-decaytransformers

Adam and AdamW

Combining momentum and RMSProp, bias correction, and why weight decay is not L2 regularization.

You are training a transformer. Loss starts at 4.2, decreases for a few thousand steps with SGD, then plateaus at 3.8 for 10,000 steps before barely moving again. The problem is not your data or architecture — it is that your transformer has attention matrices, embedding tables, and feedforward layers all updating simultaneously, each at wildly different gradient scales. A single global learning rate is completely wrong for all of them at once.

This is the problem Adam was built to solve. By 2014, practitioners had two partial solutions sitting separately on the shelf. RMSProp tracked each parameter's gradient magnitude via an exponential moving average $v_t = β_2 v_{t-1} + (1-β_2)g_t^2$, then divided by $\sqrt{v_t}$ to normalize steps — slow-updating parameters got large steps, fast-updating ones got small steps. SGD with momentum tracked gradient direction history $m_t = β_1 m_{t-1} + (1-β_1)g_t$, smoothing out oscillations and building velocity in consistent directions. Adam (Kingma & Ba, 2014) ran both simultaneously. The $m_t$ term provides direction stability. The $v_t$ term provides per-parameter scale adaptation. Dividing the smoothed direction by the smoothed magnitude gives a step that is both directionally stable and scale-normalized: $θ_t = θ_{t-1} - α \cdot \hat{m}_t / (\sqrt{\hat{v}_t} + ε)$.

There is a critical initialization trap. At step 1, both $m_0 = 0$ and $v_0 = 0$. So $m_1 = (1-β_1)g_1 = 0.1 g_1$ — the first moment is 10x too small. $v_1 = (1-β_2)g_1^2 = 0.001 g_1^2$ — the second moment is 1000x too small. Without correction, the ratio $m_1 / \sqrt{v_1}$ is inflated by a fixed, predictable factor — $(1-β_1)/\sqrt{1-β_2} ≈ 3.16$ for the default $β_1=0.9$, $β_2=0.999$ — independent of the gradient magnitude. Bias correction divides by the initialization factor: $\hat{m}_t = m_t / (1 - β_1^t)$, $\hat{v}_t = v_t / (1 - β_2^t)$. At $t=1$: $\hat{m}_1 = m_1 / 0.1 = g_1$, $\hat{v}_1 = v_1 / 0.001 = g_1^2$. Correct. Without this, early transformer training can corrupt embeddings in ways that are nearly impossible to recover from.

NOT this. Most people think Adam + L2 regularization in the loss = weight decay. They are not equivalent, and the difference is not small. When you add $λ||θ||^2$ to the loss, the gradient becomes $g + λθ$. Adam then divides this combined gradient by $\sqrt{\hat{v}}$. For a parameter with a large gradient history, $\sqrt{\hat{v}}$ is large — the regularization term $λθ$ gets divided down to almost nothing. The parameters that receive the most gradient (probably the most important ones) get the least regularization. This is backwards.

AdamW (Loshchilov & Hutter, 2019) fixes this surgically. Instead of modifying the gradient, weight decay is applied directly to the parameters before the gradient step: $θ_t ← (1-αλ)θ_{t-1} - α\hat{m}_t/(\sqrt{\hat{v}_t}+ε)$. The $(1-αλ)$ factor decays every parameter by the same fraction per step, completely independent of gradient history. This is true weight decay. Adam + L2 in the loss is not.

Every GPT, BERT, and Llama-class model is trained with AdamW, not Adam. The difference is real but the exact magnitude varies by model and run rather than a single fixed percentage — what matters mechanically is that AdamW decay is proportional to the parameter itself, not skewed by gradient history the way Adam-plus-L2 is. For your transformer that was plateauing at 3.8: switch to AdamW, set weight_decay=0.1, add warmup. The loss plateau disappears because the per-parameter adaptation of Adam finally has matching regularization.

Key points

Takeaway

Adam combines momentum (direction stability) and RMSProp (per-parameter scale adaptation) with bias correction; AdamW corrects Adam's broken regularization by applying weight decay directly to parameters instead of through the gradient, which is why AdamW is the standard for every serious language model.

Recap

Check your understanding

Q1. Without bias correction, what happens to Adam's step size in the first 10 training steps when β1=0.9, β2=0.999? Why does this matter for training stability?

Q2. Which two of the following correctly explain why adding L2 regularization to the loss does not behave as expected in Adam, but AdamW's weight decay does?

Q3. A colleague proposes switching a ResNet-50 ImageNet training from SGD+momentum to Adam because "Adam converges faster." What do you predict about final test accuracy, and what would you recommend instead?

Q4. What happens to Adam's behavior when β2 is set very close to 1 (say, 0.9999)? When would you deliberately use a lower β2 (say, 0.9)?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →