Mini-Batch SGD versus Adam: Update Rules and Trade-offs
Machine LearningModel answerLast updated
Derive the parameter update rules for mini-batch SGD (with and without momentum) and Adam, explain bias correction, and compare the two on speed, tuning, memory and generalization.
You are training a neural network with mini-batch gradients and must justify your choice of optimizer to a teammate.
Write up a comparison of stochastic gradient descent and Adam. Your answer should:
- Show how plain mini-batch SGD updates parameters, and how adding momentum changes that rule.
- Derive Adam's update. Define every symbol: the learning rate, the two moment decay coefficients, the numerical stabilizer and the time step.
- Explain why Adam applies bias correction, and what goes wrong in the first few steps without it.
- Compare the optimizers on convergence behavior, hyperparameter tuning effort, memory footprint and generalization.
- Keep the comparison fair: contrast Adam with momentum SGD as well as with plain SGD, and avoid claiming that either optimizer is always better.
Before answering, say which clarifying questions you would ask. For example: is the goal fast early progress or best final validation quality? Is weight decay implemented as an L2 term in the gradient or as decoupled decay? Are gradients sparse or very different in scale across parameters?
Hints
Hint 1
Start with the plain rule theta <- theta - lr * g, where g is the mini-batch gradient, then add a velocity term to get momentum SGD.
Hint 2
Adam keeps two exponential moving averages: one of the gradient (first moment) and one of the squared gradient (second moment). Both start at zero, so ask what their values look like at step 1 when the decay coefficients are close to 1.
Hint 3
For the comparison, think about per-parameter step sizes, extra optimizer state per parameter, sensitivity to learning rate, and how L2 regularization interacts with the adaptive denominator.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarifying questions
- Is the priority early training speed, final validation quality, or both?
- Is weight decay an L2 penalty added to the gradient, or decoupled decay applied directly to the weights?
- Are gradients sparse or on very different scales across parameters (embeddings, mixed-scale features)?
The answers shift the recommendation, so I would state my assumptions explicitly.
Mini-batch SGD
At step t, sample a mini-batch and compute the gradient of the loss on it:
g_t = ∇θ L(θ_{t-1}; batch_t)
Plain SGD:
θ_t = θ_{t-1} − η g_t
Every parameter shares one learning rate η. The gradient is a noisy but unbiased estimate of the full gradient, and that noise is cheap to obtain and partly helps escape sharp regions.
SGD with momentum keeps a velocity v (with coefficient μ, typically 0.9):
v_t = μ v_{t-1} + g_t θ_t = θ_{t-1} − η v_t
Momentum averages gradients over recent steps, damping oscillation across narrow valleys and accelerating along consistent directions. It is still a single global step size, though.
Adam
Adam keeps two moving averages per parameter, with decay coefficients β₁ (typically 0.9) and β₂ (typically 0.999):
- m_t = β₁ m_{t-1} + (1 − β₁) g_t (first moment, a smoothed gradient)
- v_t = β₂ v_{t-1} + (1 − β₂) g_t² (second moment, a smoothed squared gradient, elementwise)
with m₀ = v₀ = 0.
Bias correction:
m̂_t = m_t / (1 − β₁^t), v̂_t = v_t / (1 − β₂^t)
Update:
θ_t = θ_{t-1} − η · m̂_t / (√v̂_t + ε)
Here η is the learning rate (commonly 1e-3), ε (about 1e-8) is a numerical stabilizer that prevents division by zero, and t is the time step counting updates.
Intuitively, m̂ supplies direction (like momentum) and √v̂ estimates each parameter's typical gradient magnitude. Dividing by it normalizes the step, so each coordinate moves roughly η per step regardless of gradient scale.
Why bias correction is needed
Because m and v start at zero, they are biased toward zero early on. At t = 1 with β₂ = 0.999:
v₁ = 0.001 · g₁²
This is 1000 times too small. Without correction, √v₁ is about 0.03|g|, so the step would be about 30 times larger than intended. The first steps would be wild.
Formally, if gradients have a roughly stationary second moment, E[v_t] = (1 − β₂^t) E[g²]. Dividing by (1 − β₂^t) removes that factor. At t = 1 the correction gives m̂₁ = g₁ and v̂₁ = g₁², so the first step has magnitude about η per coordinate. As t grows, β^t → 0 and the correction disappears. The first-moment correction matters less (β₁ = 0.9 warms up in about 10 steps), but the second-moment correction is the important one.
Comparison
Convergence. Adam typically makes fast early progress and is robust to poorly scaled gradients, because of per-parameter normalization. It shines on sparse gradients (embeddings, NLP) and on transformers, where it is the default. Momentum SGD on well-conditioned problems, such as ResNets for vision with a good schedule, can match or beat Adam in the end. Plain SGD without momentum is usually the slowest and most sensitive to curvature.
Tuning. Adam's default (η = 1e-3, β = 0.9/0.999) often works with little tuning, so it is forgiving. SGD is more sensitive to η and needs a schedule (step decay, cosine, warmup) to do well. Both benefit from schedules; transformers with Adam almost always use warmup, in part because early v̂ estimates are noisy.
Memory. Plain SGD stores no extra state. Momentum SGD stores one buffer per parameter (1x parameters). Adam stores two (2x parameters), so optimizer state is 2x the model size in addition to weights and gradients. For large models this is significant, motivating variants such as 8-bit optimizer state, Adafactor or sharded optimizer states.
Generalization. A commonly observed pattern is that well-tuned SGD with momentum generalizes slightly better on some vision tasks, with Adam finding solutions that train faster but sometimes validate a bit worse. The evidence is task-dependent and shrinks with good regularization and tuning, so I would not treat it as a rule.
Weight decay pitfall
With L2 regularization, the term λθ is added to the gradient. In SGD this is equivalent to shrinking weights. In Adam the L2 term passes through the adaptive denominator, so parameters with large historical gradients are regularized less, and the effective decay becomes inconsistent. AdamW decouples decay:
θ_t = θ_{t-1} − η (m̂_t / (√v̂_t + ε) + λ θ_{t-1})
This restores the intended regularization and is the usual choice for Adam-family training.
Failure modes
- Unstable steps: a very small v̂ (rare parameters, gradient near zero) with tiny ε can create huge steps. Raise ε (e.g. 1e-6 to 1e-3), lower η, use warmup, or clip gradients.
- Stale or lagging estimates: with β₂ = 0.999 the second moment averages over roughly 1000 steps, so after a regime change (loss spike, data shift) the denominator is out of date.
- Non-convergence cases: Adam can fail to converge on some constructed convex problems because v can shrink; AMSGrad addresses this by keeping a running max of v̂.
- Coupled weight decay confusion: using L2 in Adam and expecting AdamW behavior.
- Unfair comparison: comparing tuned Adam to untuned plain SGD, or ignoring momentum.
Recommendation
Default to AdamW for transformers, sparse features or when tuning time is limited. Use momentum SGD with a tuned schedule when training convolutional vision models where it is known to do well, and when memory is tight. In practice, run a small learning-rate sweep for both and compare on validation metrics, not training loss.