Walk me through gradient descent variants
Written by the mlprep team · Updated
Walk me through gradient descent variants — SGD, momentum, Adam. How do you decide which to use?
Vanilla gradient descent#
Computes the gradient over the entire dataset. Guaranteed descent at each step but prohibitively slow — one update per full pass over the data. Never used for deep learning.
Stochastic Gradient Descent (SGD)#
Uses a single random example (or mini-batch) per update. Introduces noise, but that noise is actually useful — it helps escape shallow local minima and saddle points. Mini-batch SGD (batch size 32–512) is the practical default. The gradient is unbiased but high-variance.
Problem: a single learning rate for all parameters is rigid. Parameters with sparse gradients (rarely updated) and dense gradients (frequently updated) should ideally update at different rates.
Momentum#
Adds a velocity term that accumulates gradients over time:
β ≈ 0.9 is standard. Momentum smooths out oscillations in ravines (long narrow loss valleys) and accelerates in consistent gradient directions. Think of it as a ball rolling downhill that builds up speed in the direction it's been going.
AdaGrad#
Adapts the learning rate per parameter:
G accumulates the sum of squared gradients. Parameters updated frequently get smaller steps; rarely-updated ones get larger steps. Great for sparse features (NLP with word embeddings).
Problem: G only grows, so the learning rate monotonically decays to zero. Training stops learning before convergence.
RMSProp#
Fixes AdaGrad by using an exponential moving average instead of a sum:
The running average "forgets" old gradients, so the effective learning rate doesn't decay to zero.
Adam (Adaptive Moment Estimation)#
Adam = momentum + RMSProp + bias correction:
β₁ = 0.9, β₂ = 0.999, η = 1e-3 are good starting points. Works well with minimal tuning across a wide range of problems.
How to choose#
For most NLP / transformer training: Adam or AdamW (Adam + weight decay decoupled from the adaptive term). For vision / CNNs: SGD + momentum often generalizes better — it's been consistently shown that SGD with momentum finds flatter minima on ImageNet, which generalize better. For sparse data or embeddings: Adam almost always wins.
You mentioned Adam's bias correction — what exactly does it fix, and are there situations where Adam is the wrong choice?
What bias correction fixes#
The moment estimates m_t and v_t are initialized to zero vectors. At timestep t=1:
With β₁ = 0.9, m_1 = 0.1 · ∇L. The estimate is biased toward zero — it's only seen one gradient but is already weighted 90% toward zero from initialization. In the first few dozen steps, both moments severely underestimate the true mean and variance.
The bias correction factors 1/(1 - β₁ᵗ) and 1/(1 - β₂ᵗ) scale the estimates up to compensate. At t=1: 1/(1-0.9) = 10, which multiplies m_1 back to ∇L. By t=100, β₁¹⁰⁰ ≈ 0.000027, so the correction factor is ~1 and bias is negligible.
Without bias correction, the first training steps take much smaller updates than they should, wasting compute early in training when the loss landscape is most informative.
When Adam is the wrong choice#
Vision tasks that need to generalize: several papers (notably Wilson et al. 2017) showed that SGD + momentum finds flatter minima than Adam on standard vision benchmarks. Flatter minima are associated with better generalization — the loss surface is shallow around the solution, so small shifts in the data distribution don't cause large prediction changes. Adam can converge to sharper, narrower minima that have lower training loss but worse test performance.
Weight decay semantics: standard Adam's weight decay interacts poorly with the adaptive learning rate — parameters with small adaptive learning rates effectively get more regularization. AdamW decouples weight decay from the gradient update, which is theoretically cleaner and performs better in practice. Always use AdamW over Adam when regularization matters.
Very late in training: some practitioners switch from Adam to SGD near convergence (SWATS, or manual switch) to exploit SGD's tendency to settle in flatter regions.
Go deeper: Implement gradient descent from scratch covers the coding-round version with vectorized NumPy.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Adam combines momentum (smooth gradient direction) + RMSProp (adaptive per-parameter learning rate) + bias correction (fix for zero initialization of moments). It's the default for transformers and NLP. SGD + momentum often generalizes better for vision because it finds flatter minima. Always prefer AdamW over Adam when weight decay is used.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- What is learning rate scheduling and how does warmup interact with Adam's bias correction?
- How does weight decay in AdamW differ from L2 regularization in standard Adam?
- What are the practical signs that your optimizer has converged to a sharp minimum and how would you address it?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions