The Adam Optimiser
Intuition
DL-14 smooths the direction of the gradient. Adam also adapts the step size of each parameter: parameters whose gradients are consistently large take proportionally smaller steps, and rarely-updated parameters take larger ones. It is the default optimiser for most deep learning.
Precise explanation
For each parameter, keep (moving average of gradients) and (moving average of squared gradients), both starting at 0, and a step counter shared by all parameters. At step :
- Bias correction: because and start at 0 they are too small early on; dividing by fixes that. At , and , so the first step is .
- Multiplying all gradients by a constant leaves the steps (almost) unchanged — Adam is scale-invariant, unlike plain SGD.
- Defaults from the paper: , , .
Worked example
, lr , first step: , , , , step .
Common mistakes
- Incrementing once per parameter instead of once per
stepcall. - Forgetting bias correction, giving tiny early steps.
- Putting inside the square root (a different, but common, variant; the paper puts it outside).
Why this matters
Adam (and its variant AdamW) trains most transformers and CNNs; knowing its update explains learning-rate choices and warm-up schedules.
Library implementation
torch.optim.Adam(parameters, lr=..., betas=(0.9, 0.999), eps=1e-8) implements the same update
(with ε outside the square root). In PyTorch the optimiser modifies tensors in place after
loss.backward(); here the dictionary interface makes each step explicit. The comparison below
runs only when PyTorch is installed.
References
- Adam: A Method for Stochastic Optimization — Kingma & Ba (
S095) - Dive into Deep Learning — Aston Zhang, Zachary C. Lipton, Mu Li, Alexander J. Smola (
S013)