Summary
Adam saves a smoothed gradient and a smoothed squared gradient for each parameter. It divides directional history by a coordinate-scale estimate. Bias correction compensates for zero-initialized buffers, not gradient noise in general.
Task: start , , , and learning rate . Apply prescribed gradients , then . Use only for this nonzero-denominator fixture; production implementations use positive epsilon. Does the second coordinate stop when its current gradient becomes zero?
The mechanism
All operations below are coordinatewise; the step counter counts optimizer updates, starting at one. Momentum saves a decaying sum; Adam’s first moment includes the additional factor . Its second moment averages , not squared deviations from a mean.
| Step | |||||
|---|---|---|---|---|---|
| 1 | |||||
| 2 |
At step one, divide each buffer by . Both update magnitudes are , giving . At step two, divide by . The second displacement is
Thus . The vanished current gradient does not erase saved directional history.
The complete state update is:
m_t = beta1 * m_{t-1} + (1 - beta1) * g_t
v_t = beta2 * v_{t-1} + (1 - beta2) * g_t^2
# bias correction (for early steps when m, v are biased toward 0)
m_hat = m_t / (1 - beta1^t)
v_hat = v_t / (1 - beta2^t)
theta_t = theta_{t-1} - lr * m_hat / (sqrt(v_hat) + eps)
The Adam paper uses this outside-the-square-root epsilon convention. Actual APIs may expose additional choices; inspect the specified implementation.
Why those correction factors? For constant gradient mean and zero initial state, . Likewise, constant raw second moment gives . The denominators remove those initialization factors. Under changing distributions, corrected buffers still represent weighted history, not unbiased estimates of every current quantity. The ratio of corrected moments is not thereby an unbiased update.
Remove first-moment memory
New task: set , retaining the same gradients and . Now , while is unchanged. Check: ; the second coordinate no longer moves on step two. Use when: testing whether saved directional history causes movement after a gradient disappears.
Enter epsilon’s scale
New task: use first gradient and with the same learning rate. The first-step coordinate ratio is , not merely its sign. Check: , rather than . Use when: gradient magnitudes approach epsilon; scale cancellation is then only an approximation.
Failure and transfer
Clipping the current gradient does not erase or directly bound the complete Adam displacement. Even a wholly zero current gradient can leave a nonzero moment-driven step. Normalization also does not guarantee decreasing loss on each update.
Reconstruct: why is not the gradient variance?
Answer
It averages raw squares. Variance would subtract a squared mean under compatible weighting and assumptions; Adam’s two histories can use different decay rates.
What an interviewer expects you to say
Reconstruct buffers, step count, correction factors, and the denominator before discussing optimizer preferences. Two FP32 moment arrays require bytes: decimal GB for billion parameters. That excludes weights, gradients, master copies, activations, and communication buffers.
AdamW: the fix that matters
AdamW applies decay directly to parameters, separately from adaptive gradient normalization:
theta_t = theta_{t-1} - lr * m_hat / (sqrt(v_hat) + eps) - lr * weight_decay * theta_{t-1}
AdamW gives equal weights equal explicit decay despite different histories
Derivation
Which route changes when only the frozen preconditioner changes?
See the decay derivation and Loshchilov and Hutter. Check API decay mode and excluded parameter groups rather than inferring behavior from the optimizer’s name alone.
When Adam goes wrong
Warmup changes the learning-rate schedule; bias correction changes moment initialization factors. Neither guarantees stability, and no fixed warmup percentage suits every workload. Inspect actual update norms, nonfinite values, loss scale, and sampling changes. Sparse-gradient support and inactive-coordinate updates depend on the optimizer implementation.
Modern optimizer choices
| Choice | Distinct computation or state tradeoff |
|---|---|
| LAMB | Adds a layerwise trust ratio to an adaptive update. |
| AdaFactor | Factors matrix second-moment statistics; optional first moments add separate state. |
| Lion | Uses signed updates and directional momentum without Adam’s squared-gradient buffer. |
| Sophia | Uses estimated diagonal curvature in its preconditioner. |
| Shampoo | Uses matrix statistics and Kronecker-structured preconditioning, not an exact full Hessian. |
| Muon | Approximately orthogonalizes matrix momentum updates; other parameter groups need their specified treatment. |
Memory, communication, numerical precision, and matched empirical quality determine suitability, not a universal production ranking.
Common confusions
Adam still needs a learning rate. RMSProp-like squared-gradient normalization does not by itself specify Adam’s first moment and bias correction. Save moments, update count, parameter groups, and schedule state when resuming.
Why interviewers ask
The useful test is whether you can reconstruct an update and separate assumptions from an empirical optimizer preference.
Related interview: How do you choose a learning rate?, How would you debug a model that’s not learning?.