Compare SGD, Adam, and AdamW — when would you use each?
Written by the mlprep team · Updated
Compare SGD, Adam, and AdamW. What does each actually do, and when would you prefer SGD over Adam despite Adam being adaptive?
SGD with Momentum#
Vanilla SGD: w ← w - η·∇L. Gradient is noisy; learning can be slow along directions of low curvature.
SGD + Momentum accumulates velocity across steps:
v ← β·v + ∇L
w ← w - η·v
Momentum (β≈0.9) accumulates gradient signal in consistent directions and dampens oscillations in high-curvature directions. Like a ball rolling — it accelerates in the relevant direction.
SGD characteristics:
- All parameters share the same learning rate
- Simple, requires careful tuning of η and β
- Known to generalize well — often finds flatter minima with better test performance
- Slower to converge than Adam but may reach a better final solution
Adam#
Maintains per-parameter first moment (mean, like momentum) and second moment (uncentered variance):
m ← β₁·m + (1-β₁)·∇L (first moment)
v ← β₂·v + (1-β₂)·∇L² (second moment)
m̂ = m / (1 - β₁ᵗ) (bias correction)
v̂ = v / (1 - β₂ᵗ)
w ← w - η · m̂ / (√v̂ + ε)
Typical values: β₁=0.9, β₂=0.999, ε=1e-8.
The update magnitude is roughly η for all parameters — parameters with high variance gradients (noisy, frequently updated) get smaller effective learning rates. Parameters with low variance gradients (rare features) get larger effective updates. This makes Adam well-suited for sparse gradients (NLP, embeddings) and problems with varying gradient magnitudes.
Why Adam can generalize worse than SGD#
Adam's adaptive learning rates allow it to converge fast but often to sharp minima — regions of the loss landscape where the loss is low but the landscape is steep. Sharp minima tend to generalize worse than flat ones. SGD + momentum with proper tuning often finds flatter regions.
In practice: vision tasks — SGD + momentum with cosine schedule often outperforms Adam. NLP / transformer training — Adam is almost universally used, often with cosine or linear warmup.
AdamW — the fix for weight decay#
L2 regularization adds λ‖w‖² to the loss, which adds λw to the gradient. In Adam, the gradient gets adaptively scaled by √v̂. This means the weight decay contribution is also adaptively scaled — effectively different amounts of regularization for different parameters, breaking the intended behavior.
AdamW decouples weight decay from the gradient update:
w ← (1 - η·λ) · w - η · m̂ / (√v̂ + ε)
Weight decay is applied directly to weights, not as a gradient term. This makes regularization consistent across all parameters regardless of their gradient variance. AdamW is strictly better than Adam + L2 regularization for neural networks. It's the default optimizer for nearly all modern transformer training (BERT, GPT, etc.).
Practical guidance#
| Use case | Optimizer | |----------|----------| | CNNs / ResNets (image classification) | SGD + momentum + cosine schedule | | Transformers (NLP, vision) | AdamW + warmup + cosine/linear decay | | RNNs | Adam or AdamW | | Sparse problems (embeddings) | Adam or AdamW | | When regularization matters | AdamW over Adam |
Learning rate schedule always matters: cosine annealing, warmup (especially for transformers), and linear decay all significantly affect final performance.
Senior/staff considerations#
Optimizer choice is part of a training system, not a standalone preference. The right answer depends on architecture, batch size, schedule, regularization, data scale, and how much tuning budget you have.
For deep nets, compare optimizers under tuned learning rates and schedules. AdamW with a bad learning rate can lose to SGD; SGD with no warmup or poor decay can look unfairly bad. A senior answer should mention that optimizer comparisons are only meaningful after tuning the schedule and weight decay.
Important practical details:
- AdamW is usually preferred over Adam with L2-style regularization for neural nets because weight decay is decoupled from adaptive gradient scaling.
- Large-batch training often needs LR scaling, warmup, gradient clipping, and sometimes adaptive optimizers to stay stable.
- Sparse embeddings often benefit from adaptive methods because rare parameters get useful updates.
- Fine-tuning large pretrained models usually uses AdamW with small learning rates, warmup, and careful weight decay exclusions for bias and normalization parameters.
- Generalization differences are empirical, not a law. SGD often performs well for classic vision training, but AdamW is standard for transformers and many large-scale regimes.
Common traps#
- Saying Adam is "always better" because it converges faster.
- Saying SGD "generalizes better" as a universal theorem.
- Forgetting that learning rate schedule often matters more than the optimizer name.
- Applying weight decay blindly to LayerNorm, bias terms, or embeddings without checking the training recipe.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
SGD: one LR for all params, robust generalization in many vision settings, needs tuning. Adam: adaptive per-parameter LR, fast convergence, useful for sparse or unstable gradients. AdamW: Adam + decoupled weight decay — preferred regularization behavior for modern neural networks and default for transformers. Pick SGD+momentum for many CNN baselines where you can tune, AdamW for transformers and fine-tuning. Be cautious with Adam + L2-style regularization; AdamW is usually the cleaner choice.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- What is learning rate warmup, why do transformers need it, and what happens if you skip it?
- Why does Adam find sharper minima than SGD, and how does the Lion optimizer try to address this?
- What is gradient clipping, when should you use it, and how does it interact with adaptive optimizers?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions