The interview problem
A training run switches from SGD to momentum and then Adam. Loss initially falls faster, but later oscillates. After restoring only model weights, the trajectory changes again. Explain the updates with a scalar example, compare adaptive scaling and weight decay, and specify what a faithful checkpoint needs.
Original fundamentals practice, not a reported company interview. Numerical examples are hypothetical. Official technical references retrieved September 27, 2026.
1. State the convention before comparing numbers
Let gₜ be the gradient evaluated at θₜ₋₁. Plain SGD uses θₜ = θₜ₋₁ − ηgₜ. For this exercise, momentum uses vₜ = μvₜ₋₁ + gₜ and θₜ = θₜ₋₁ − ηvₜ, with v₀ = 0. There is no dampening, Nesterov lookahead or weight decay unless specified.
Another convention uses an exponential average mₜ = μmₜ₋₁ + (1 − μ)gₜ. For the same supplied gradient sequence and matching zero initialization, mₜ = (1 − μ)vₜ. Once parameter trajectories differ, their gradients need not match. Using the same numerical learning rate does not make these conventions equivalent. Some implementations put the learning rate inside the buffer; changing the learning rate over time can then change how past gradients are weighted.
2. Compute momentum on a quadratic
Use f(θ) = θ²/2, so g = θ, with θ₀ = 1, η = 0.1 and μ = 0.9. The first update gives v₁ = 1 and θ₁ = 0.9. Next v₂ = 0.9 × 1 + 0.9 = 1.8, so θ₂ = 0.72. Third v₃ = 0.9 × 1.8 + 0.72 = 2.34, so θ₃ = 0.486.
| Step | Plain SGD θ | Momentum θ | Momentum buffer |
|---|---|---|---|
| 0 | 1 | 1 | 0 |
| 1 | 0.9 | 0.9 | 1 |
| 2 | 0.81 | 0.72 | 1.8 |
| 3 | 0.729 | 0.486 | 2.34 |
Momentum initially moves faster toward zero because gradients point in a consistent direction. Continuing gives θ₄ = 0.2268 and θ₅ = −0.02916. The fifth update crosses the optimum because the buffer retains earlier direction. Crossing is not by itself divergence, but a large learning rate or unsuitable momentum can produce harmful oscillation.
For a constant externally supplied gradient g, this unnormalized buffer approaches g/(1 − μ). With μ = 0.9, that is 10g. This explains why changing momentum without retuning the learning rate changes effective step magnitude. A real gradient changes with parameters, so the constant-gradient limit is an intuition, not a full convergence analysis.
3. Work through adaptive moments
Adam tracks mₜ = β₁mₜ₋₁ + (1 − β₁)gₜ and vₜ = β₂vₜ₋₁ + (1 − β₂)gₜ², starting at zero. Bias-correct them as m̂ₜ = mₜ/(1 − β₁ᵗ) and v̂ₜ = vₜ/(1 − β₂ᵗ), then update θ by −ηm̂ₜ/(sqrt(v̂ₜ) + ε), coordinate by coordinate. Here v tracks a second moment, not a centered variance.
For a first gradient of 2, β₁ = 0.9 and β₂ = 0.999, m₁ = 0.2 and v₁ = 0.004. Corrections give m̂₁ = 2 and v̂₁ = 4. With η = 0.1 and negligible ε, the step is approximately −0.1. Without those corrections, the ratio is 0.2/sqrt(0.004) ≈ 3.162, producing a step of −0.3162 under this formula. Bias correction addresses zero initialization; it does not remove minibatch sampling bias or data bias.
For first-step gradients (2, 200), the corrected adaptive steps are approximately (−0.1, −0.1) when ε is negligible. Plain SGD with η = 0.1 instead gives (−0.2, −20). This demonstrates coordinate scaling, not a guarantee that equal-sized coordinate steps are desirable. Adam still requires learning-rate tuning and validation; a schedule is an additional design choice.
4. Do not treat epsilon as a decorative constant
Placing ε outside versus inside the square root defines different algorithms. If m̂ = 10^-6, v̂ = 10^-12 and ε = 10^-8, the ratio m̂/(sqrt(v̂)+ε) is about 0.9901. With sqrt(v̂+ε) in the denominator it is about 0.0100. At learning rate 0.001, step magnitudes are approximately 0.0009901 and 0.0000100, nearly a 99-fold difference.
The same numeric ε is not directly comparable across placements because its role and units differ. Mixed precision, tiny gradients and denominator rounding can make these details significant. Pin optimizer implementation, precision and hyperparameters when reproducing a run.
5. Distinguish L2 gradients from decoupled decay
An L2 penalty λ||θ||²/2 adds λθ to the gradient. In adaptive optimization that term enters moment estimates and is rescaled coordinate-wise. AdamW instead applies parameter shrinkage separately from the adaptive gradient update, commonly θ ← (1 − ηλ)θ before subtracting the adaptive step. These are not generally equivalent.
Consider θ = 2, zero data gradient, λ = 0.1, η = 0.01 and initially zero moments. Coupled L2 gives gradient 0.2; the bias-corrected first Adam step is approximately −0.01 with negligible ε, leaving θ ≈ 1.99. Decoupled AdamW shrinkage gives 2 × (1 − 0.001) = 1.998, with zero adaptive data-gradient step. Here zero means an explicit zero gradient tensor, not an absent gradient that an implementation may skip entirely. The different result follows from whether decay is normalized through the moments.
For plain SGD without momentum, adding λθ and multiplicative shrinkage can be algebraically equivalent with matching conventions. Do not carry that equivalence automatically to momentum or adaptive methods. Record which parameter groups receive decay; biases, normalization parameters and other groups may be treated differently by a training recipe.
6. Restore optimizer state, not just weights
Return to the quadratic after step two, θ = 0.72 and v = 1.8. A faithful next momentum update gives θ₃ = 0.486. If a restart restores θ but resets v to zero, the next update gives v = 0.72 and θ = 0.648. Identical weights do not define an identical optimization state.
Checkpoint optimizer buffers, step counters, parameter-group mapping, scheduler state and mixed-precision loss-scaler state when used. Reproducible continuation also needs data-loader position and random-number state, plus compatible software and numerical kernels. Restoring state may still not give bitwise identity with nondeterministic kernels or a changed distributed topology.
For one billion parameters, one FP32 momentum buffer occupies four billion bytes, or 4 GB decimal. Two FP32 Adam moment buffers occupy 8 GB, before parameters, gradients, master copies, activations and communication buffers. Sharding and lower-precision state change this footprint and can change numerical behavior; state storage is part of optimizer selection.
7. Diagnose oscillation with controlled comparisons
Inspect gradient norms, update-to-weight ratios, loss by step, learning-rate transitions and buffer norms. Verify loss reduction and gradient accumulation scaling before blaming the optimizer. Compare methods with separately tuned learning rates and equal data/compute budgets rather than swapping names while holding all hyperparameters fixed.
Check clipping order, decay convention, skipped updates after nonfinite gradients and scheduler advancement. A skipped parameter update that still advances a schedule or counter may differ from the intended algorithm. Use a small scalar update test against the exact library version to establish semantics before interpreting a large training run.
Evaluate held-out quality and robustness across seeds, not only training loss speed. Adaptive scaling may help poorly scaled coordinates; momentum can exploit persistent direction. Neither observation establishes universal superiority or guarantees that an optimizer escapes every poor solution.
8. Follow-ups and self-review
Follow-ups: Why can the step remain nonzero when the current gradient is zero? The momentum buffer still contains history. Is Adam's second moment the variance? No; variance would subtract the squared mean under suitable definitions. Does bias correction make a classifier calibrated? No; it corrects moment initialization. Why does resetting the optimizer alter fine-tuning? It changes history and step scaling, so it is a deliberate new training procedure.
Score 0–2 each for precise update conventions, correct scalar arithmetic, understanding adaptive normalization, distinguishing weight-decay variants, and restoring a complete training state. A strong answer reasons from equations and measured behavior rather than optimizer reputation.
Sources and next steps
- PyTorch SGD documentation — momentum conventions and initialization details.
- PyTorch AdamW documentation — moment updates, bias correction and decoupled decay.
The scalar trajectories, epsilon comparison, decay counterexample and memory arithmetic are original exercises. Continue with gradient accumulation.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.