Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 125/ 160
07. Deep Learning & NLP

Optimizers (SGD, Momentum, Adam, AdamW)

Update weights with SGD/Adam variants and understand weight decay decoupling.

IntermediateSGD · Adam3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

After checking which way is downhill, the optimiser decides how big a step to take. Simple versions take the same-sized steps, smarter ones remember past steps and adjust how big to go for each setting.

Think of it like…

Rolling a ball down a bumpy valley. Plain SGD is a ball in thick mud that follows each bump; momentum is a heavy ball that smooths bumps and builds speed along the valley floor; Adam also adjusts how hard it pushes in each direction depending on how steep and noisy that direction has been.

Where you’ve seen it

On one weight with gradient g = 0.4 and lr = 0.1, SGD moves it by 0.04 (1.00 → 0.96). With momentum β = 0.9 and a constant gradient of 1, the velocity grows 1, 1.9, 2.71 and approaches 1 / (1 − 0.9) = 10, so the effective step is up to ten times the base learning rate, which is why momentum runs often need a smaller lr. For Adam's very first step with g = 0.4: m = 0.1 × 0.4 = 0.04, v = 0.001 × 0.16 = 0.00016, bias correction divides by (1 − 0.9) and (1 − 0.999) to give m̂ = 0.4 and v̂ = 0.16, so the update is lr × 0.4 / √0.16 = lr × 1. With g = 40 instead of 0.4 the update is still lr × 1: Adam's steps are roughly lr in size regardless of gradient scale. The team fine-tunes a transformer with AdamW (lr 2e-5, weight decay 0.01, since 1e-3 would wipe out pretrained knowledge) and trains a ResNet from scratch with SGD (lr 0.1, momentum 0.9) and a cosine schedule. Memory matters too: a 10-million-parameter model in float32 takes 40 MB for weights, 40 MB for gradients and 80 MB for Adam's m and v, 160 MB in total, whereas SGD without momentum needs only 80 MB.

Words you’ll hear

Learning rate
The base step size multiplying each update.
Momentum
A velocity term that accumulates past gradients.
Adaptive optimiser
One that scales each parameter's step by its gradient history, as Adam does.
Decoupled weight decay
Shrinking weights directly rather than through the gradient, as in AdamW.

Visual diagram

Loading interactive diagram

Theory & deep dive

SGD, momentum and Adam

SGD uses a minibatch gradient as a noisy estimate of the full gradient; the noise helps escape sharp minima and the batch size sets how noisy it is. Its weakness is sensitivity to curvature: a steep direction forces a small lr that makes progress along flat directions slow. Momentum accumulates v ← βv + g and steps w ← w − lr·v. With a constant gradient the velocity converges to g / (1 − β), so β = 0.9 gives a 10× amplification, while oscillating gradients cancel. Nesterov momentum evaluates the gradient at the look-ahead point and is a small refinement.

Adam tracks m ← β₁m + (1 − β₁)g and v ← β₂v + (1 − β₂)g² with defaults β₁ = 0.9 and β₂ = 0.999. Both start at zero, so early estimates are biased low and Adam divides by (1 − β₁ᵗ) and (1 − β₂ᵗ). The update lr · m̂ / (√v̂ + ε) is a signal-to-noise ratio: parameters with large, consistent gradients move by about lr, while those with noisy or tiny gradients move less. For g = 0.4 at step 1, m̂ = 0.4 and √v̂ = 0.4, so the step equals lr; for g = 40 it is still lr. That is why Adam needs less learning-rate tuning but can generalise slightly worse than tuned SGD on some vision tasks.

Adam step 1 by hand

g = 0.4: m = 0.1 × 0.4 = 0.04; v = 0.001 × 0.16 = 0.00016. Bias-corrected m̂ = 0.04 / 0.1 = 0.4 and v̂ = 0.00016 / 0.001 = 0.16, so √v̂ = 0.4 and the update is lr × 0.4 / 0.4 = lr. Replace g with 40 and every quantity scales, but the ratio, and the step, are unchanged.

Key takeaway: Momentum smooths and accelerates; Adam normalises each parameter's step by its own gradient history.

Weight decay, AdamW and choosing an optimiser

L2 regularisation adds λw to the gradient. In SGD that is equivalent to weight decay, but in Adam the added term is then divided by √v̂, so weights with large gradient history are decayed less and the regularisation becomes uneven. AdamW applies the decay outside the adaptive scaling: w ← w − lr · (m̂ / (√v̂ + ε) + λw). With lr = 1e-3 and λ = 0.01 each step shrinks w by a factor of 1 − 10⁻⁵, so over 10,000 steps decay alone multiplies a weight by about e⁻⁰·¹ = 0.905. Do not decay biases and normalisation parameters; most libraries let you put them in a separate parameter group.

Practical defaults: AdamW (lr 1e-4 to 1e-3 for training from scratch, 1e-5 to 5e-5 for fine-tuning, weight decay 0.01 to 0.1) for transformers, embeddings and most tabular nets; SGD with momentum 0.9 (lr around 0.1 with a schedule, weight decay 5e-4) for CNNs when you can afford tuning. Pair any optimiser with a warm-up and decay schedule and gradient clipping. Optimiser state is real memory: Adam stores two extra tensors per parameter, so a 10-million-parameter float32 model uses 40 MB (weights) + 40 MB (grads) + 80 MB (m, v) = 160 MB before activations.

Decay over training

AdamW with lr = 1e-3 and λ = 0.01 shrinks each weight by a factor of (1 − 1e-5) per step. After 10,000 steps, ignoring the gradient term, the factor is 0.99999¹⁰⁰⁰⁰ ≈ e⁻⁰·¹ ≈ 0.905, so a weight of 1.0 would decay to about 0.905.

Key takeaway: Use AdamW for most modern work, SGD with momentum when tuned vision accuracy matters, and always combine them with a schedule.

How it works, step by step

Step 1 of 4
Data scientist

1. Start from a strong default

Pick AdamW for transformers and tabular nets, or SGD with momentum for CNNs, with the usual learning-rate range.

AdamW lr 1e-3 · fine-tune 2e-5
Key numbers at this step
Decay
0.01 – 0.1

When to use it & trade-offs

What it is

An optimiser turns gradients into weight updates. SGD moves each weight against its minibatch gradient: w ← w − lr·g. Momentum keeps a running velocity v ← βv + g so that consistent directions accelerate and noisy ones cancel. Adam keeps two running averages, the mean of gradients (first moment m) and the mean of squared gradients (second moment v), applies bias correction and updates w ← w − lr · m̂ / (√v̂ + ε), giving each parameter its own adaptive step size. AdamW fixes how weight decay is applied: instead of adding an L2 term to the gradient (where Adam's scaling distorts it), it shrinks the weights directly, w ← w − lr · (adam_step + λw), which regularises every weight uniformly.

Why it matters: The optimiser decides whether training converges, how fast and how well the result generalises. SGD with momentum and a decaying learning rate still reaches the best accuracy on many vision benchmarks but is sensitive to the learning rate; Adam-style methods converge quickly with little tuning and dominate transformers and NLP. Knowing the update rules lets you reason about learning-rate scale, why Adam's first step is about lr in size whatever the gradient scale, how much memory the optimiser state costs and why AdamW became the default for pretrained models.

When to reach for it

  • AdamW for fine-tuning pretrained transformers
  • SGD with momentum and cosine decay for ResNet-style training
  • Gradient clipping with any optimiser on unstable runs

When to avoid or be careful

  • Reusing a from-scratch learning rate when fine-tuning
  • Adam with an L2 penalty when AdamW is available
  • Applying weight decay to biases and normalisation parameters

Optimisers compared

SGD with momentum
Best for: CNNs with a tuned schedule

Strengths: Strong final accuracy; low memory.

Watch out: Sensitive to learning rate and scaling.

Adam
Best for: Fast convergence with little tuning

Strengths: Per-parameter adaptive steps.

Watch out: Coupled L2 decay is uneven; two extra state tensors.

AdamW
Best for: Transformers, fine-tuning, most modern models

Strengths: Adam's speed with proper decoupled weight decay.

Watch out: Weight decay and lr still need tuning.

Interview signals & numbers to know

When this topic comes up

  • “How does Adam differ from SGD with momentum?”
  • Why AdamW decouples weight decay
  • Bias correction in Adam
  • Optimiser memory cost
  • Choosing the learning rate

Benchmarks & rules of thumb

β₁ 0.9 · β₂ 0.999 -
Adam's default moment decay rates
1 / (1 − β) -
steady-state momentum amplification, 10 for β = 0.9
2 extra tensors -
Adam's optimiser state per parameter

Common mistakes & follow-ups

Common interview mistakes

  • Using Adam's default lr of 1e-3 on a pretrained transformer.
  • Comparing optimisers without tuning each one's learning rate.
  • Forgetting that Adam's state doubles the optimiser memory.

Likely interviewer follow-ups

  • Why does Adam use bias correction?
  • What is the difference between L2 regularisation and weight decay in Adam?
  • When would you still choose SGD?
Interview tip: Mention that Adam's first step is about lr whatever the gradient scale; it shows you have read the update rule, not just the name.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Optimizers (SGD, Momentum, Adam, AdamW) with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Forward & Backward Propagation
Next topic
Regularization (Dropout, BatchNorm, Weight Decay)