Skip to content
mlmentorship

Adam: trace direction and coordinate scale

Compute two moment updates, reconstruct a step with zero current gradient, and separate bias correction, epsilon, decay, and state memory.

Published · 7 min read ·Core ·Intermediate
Reading depth

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

Identical weights, different gradient historiesIn this illustrative frozen-preconditioner step, AdamW applies the same direct decay to both weights.Shrinkage: coupled or directSame weights; frozen preconditionersFrozen gradient scale: 1w/ 10.0010.001Frozen gradient scale: 10w/ 100.00010.001Blue: coupled through the gradientscaleGreen: direct decay bypasses thatscale
Derivation
Equal weights, unequal coupled penaltiesWith theta one, alpha .01, lambda .1, and fixed adaptive scales one and ten, coupled L2 contributes shrinkage .001 and .0001. AdamW contributes equal .001 shrinkage. Leftward arrows share the same parameter-distance scale; this isolates the penalty and is not a full Adam recurrence.Fixed preconditioner; not the full AdamrecurrenceSame start: theta = 1alpha = 0.01; lambda = 0.1Adam + coupled L2A: s = 1shrink 0.001; end 0.999B: s = 10shrink 0.0001; end 0.9999AdamW: decoupledA: s = 1shrink 0.001; end 0.999B: s = 10shrink 0.001; end 0.999Every arrow uses the same parameter-distancescale.
Read it this way: Compare arrows on one zoomed parameter scale. Coupled L2 is divided by the fixed adaptive scale. AdamW is not. Coupled penalties also affect moment buffers.
Check: Which method gives equal weights equal explicit decay despite different history scales?
Read it this way: In this illustrative frozen-preconditioner step, AdamW applies the same direct decay to both weights.
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

ChoiceDistinct computation or state tradeoff
LAMBAdds a layerwise trust ratio to an adaptive update.
AdaFactorFactors matrix second-moment statistics; optional first moments add separate state.
LionUses signed updates and directional momentum without Adam’s squared-gradient buffer.
SophiaUses estimated diagonal curvature in its preconditioner.
ShampooUses matrix statistics and Kronecker-structured preconditioning, not an exact full Hessian.
MuonApproximately 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?.