3.2.5Training Deep Networks

Adam and AdamW optimizers

1,984 words9 min readdifficulty · medium4 backlinks

WHY do we need Adam at all?

Plain SGD uses the same learning rate for every parameter: θθηgt,gt=θL\theta \leftarrow \theta - \eta\, g_t,\qquad g_t=\nabla_\theta L

Problem 1 — different scales. In deep nets, some parameters get huge gradients, others tiny. One global η\eta is too big for some, too small for others.

Problem 2 — noisy stochastic gradients. Each minibatch gives a noisy gtg_t. We want to average out the noise (momentum) but also normalize by how big/variable each gradient is.

Adam answers both by tracking two exponential moving averages (EMAs) per parameter.


Deriving Adam from first principles

Step 1 — First moment (momentum): smooth the direction

We EMA the gradient itself: mt=β1mt1+(1β1)gtm_t=\beta_1 m_{t-1}+(1-\beta_1)g_t Why? mtm_t estimates E[gt]\mathbb{E}[g_t] — the average gradient. This cancels noise and keeps consistent directions moving.

Step 2 — Second moment: smooth the magnitude

We EMA the squared gradient (elementwise): vt=β2vt1+(1β2)gt2v_t=\beta_2 v_{t-1}+(1-\beta_2)g_t^{2} Why? vtv_t estimates E[gt2]\mathbb{E}[g_t^2] — the size/variance of the gradient in each coordinate. Large vtv_t ⇒ that direction is steep/noisy ⇒ take smaller steps there.

Step 3 — Bias correction (the clever bit)

Because m0=v0=0m_0=v_0=0, early EMAs are biased toward zero. Let's prove the correction. Assume gg roughly stationary with mean gˉ\bar g. Unrolling: mt=(1β1)i=1tβ1tigi.m_t=(1-\beta_1)\sum_{i=1}^{t}\beta_1^{\,t-i}g_i. Taking expectations with E[gi]=gˉ\mathbb E[g_i]=\bar g: E[mt]=gˉ(1β1)i=1tβ1ti=gˉ(1β1)1β1t1β1=gˉ(1β1t).\mathbb E[m_t]=\bar g\,(1-\beta_1)\sum_{i=1}^{t}\beta_1^{\,t-i}=\bar g\,(1-\beta_1)\cdot\frac{1-\beta_1^{t}}{1-\beta_1}=\bar g\,(1-\beta_1^{t}). So E[mt]=gˉ(1β1t)\mathbb E[m_t]=\bar g(1-\beta_1^t) is too small by factor (1β1t)(1-\beta_1^t). Divide it out: m^t=mt1β1t,v^t=vt1β2t.\hat m_t=\frac{m_t}{1-\beta_1^{t}},\qquad \hat v_t=\frac{v_t}{1-\beta_2^{t}}. Now E[m^t]gˉ\mathbb E[\hat m_t]\approx\bar gunbiased. As tt\to\infty, β1t0\beta_1^t\to0 and the correction vanishes.

Step 4 — The update

  θt=θt1ηm^tv^t+ϵ  \boxed{\;\theta_t=\theta_{t-1}-\eta\,\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon}\;}


AdamW — decoupled weight decay

Adam-with-L2 (naive): fold decay into the gradient gtL+λθt1    decay gets scaled by 1/v^t.  g_t \leftarrow \nabla L + \lambda\theta_{t-1}\;\Rightarrow\;\text{decay gets scaled by }1/\sqrt{\hat v_t}. \;❌

AdamW: keep decay outside the adaptive machinery   θt=θt1η(m^tv^t+ϵ+λθt1)  \boxed{\;\theta_t=\theta_{t-1}-\eta\Big(\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon}+\lambda\,\theta_{t-1}\Big)\;}

Figure — Adam and AdamW optimizers

Worked examples


Common mistakes (Steel-manned)


Recall

Recall Active recall — cover the answers
  • What two statistics does Adam track per parameter? ⇒ EMA of the gradient (mtm_t) and EMA of the squared gradient (vtv_t).
  • Why divide by v^t\sqrt{\hat v_t}? ⇒ to normalize each coordinate by its gradient magnitude → adaptive per-parameter learning rate.
  • Why bias-correct? ⇒ EMAs start at 0, biased low by factor (1βt)(1-\beta^t); dividing removes it.
  • What's the steady-state Adam step on constant gradient? ⇒ ηsign(g)\eta\,\text{sign}(g).
  • What exactly does AdamW change? ⇒ it applies weight decay ηλθ\eta\lambda\theta outside the v^\sqrt{\hat v} normalization.
Recall Feynman: explain to a 12-year-old

Imagine hiking down a hill in fog. Momentum (mm) is remembering which way you've been walking so you don't zig-zag. The magnitude tracker (vv) notices which directions are steep and slippery, so you take tiny careful steps there and big confident steps on gentle slopes. Bias correction is because at the very start you have no memory yet, so you multiply up your first guesses to make them fair. AdamW adds a rule: "every step, shrink all your things a little toward zero, equally" — so nothing grows too big and messy. Adam = smart footwork; AdamW = smart footwork plus tidy-up.


Flashcards

Adam first moment update formula
mt=β1mt1+(1β1)gtm_t=\beta_1 m_{t-1}+(1-\beta_1)g_t
Adam second moment update formula
vt=β2vt1+(1β2)gt2v_t=\beta_2 v_{t-1}+(1-\beta_2)g_t^2
Adam bias-corrected estimates
m^t=mt/(1β1t)\hat m_t=m_t/(1-\beta_1^t), v^t=vt/(1β2t)\hat v_t=v_t/(1-\beta_2^t)
Adam parameter update
θt=θt1ηm^t/(v^t+ϵ)\theta_t=\theta_{t-1}-\eta\,\hat m_t/(\sqrt{\hat v_t}+\epsilon)
Why bias correction is needed
EMAs initialized at 0 are biased toward 0 by factor (1βt)(1-\beta^t); dividing by it makes the estimate unbiased.
Default Adam hyperparameters
β1=0.9, β2=0.999, ϵ=108\beta_1=0.9,\ \beta_2=0.999,\ \epsilon=10^{-8}
Interpretation of m^/v^\hat m/\sqrt{\hat v}
a signal-to-noise ratio; consistent gradients → step ≈ η, noisy gradients → small step.
Steady-state Adam step on constant gradient g
ηsign(g)\eta\,\text{sign}(g) — magnitude independent of g|g|.
What AdamW changes vs Adam+L2
applies weight decay as ηλθ\eta\lambda\theta decoupled from (outside) the v^\sqrt{\hat v} normalization.
Why Adam+L2 decay is wrong
the λθ\lambda\theta term gets divided by v^\sqrt{\hat v}, so decay becomes gradient-dependent instead of uniform.
AdamW update equation
θt=θt1ηm^t/(v^t+ϵ)ηλθt1\theta_t=\theta_{t-1}-\eta\,\hat m_t/(\sqrt{\hat v_t}+\epsilon)-\eta\lambda\theta_{t-1}
Role of ϵ\epsilon beyond avoiding /0
caps the max effective learning rate (η/ϵ\le\eta/\epsilon) when v^\hat v is tiny.

Connections

  • Stochastic Gradient Descent — the baseline Adam generalizes.
  • Momentum and Nesterov Acceleration — source of the first-moment term.
  • RMSProp — source of the second-moment normalization.
  • Exponential Moving Average — the core smoothing tool and its bias.
  • Weight Decay and L2 Regularization — what AdamW decouples.
  • Learning Rate Schedules — warmup pairs naturally with Adam's early instability.
  • Bias-Variance in Gradient Estimation — why we smooth gradients.

Concept Map

too rigid for

motivates

used to build

used to build

smooths direction

smooths magnitude

biased toward zero

biased toward zero

gives

feeds

defines

decouple weight decay

Plain SGD one global lr

Scale + noise problems

Adam optimizer

Exponential moving average

First moment m_t

Second moment v_t

Momentum

RMSProp scaling

Bias correction

Unbiased m-hat v-hat

Update theta = -eta m-hat / sqrt v-hat

AdamW fixes regularization

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, plain SGD har parameter ke liye same learning rate use karta hai, jo ill-conditioned problems (kuch directions steep, kuch flat) mein bekaar hai — wo zig-zag karta hai ya diverge ho jaata hai. Adam iska smart solution hai: har parameter ke liye do cheezein track karta hai — ek momentum (mtm_t, gradient ka smooth average, direction batata hai) aur doosra squared gradient ka average (vtv_t, magnitude batata hai). Fir update karta hai m^/v^\hat m/\sqrt{\hat v} se, matlab har axis ko uski apni magnitude se normalize kar deta hai. Isliye steep directions mein chhota step, flat directions mein bada step — automatic adaptive learning rate.

Bias correction ka funda simple hai: mm aur vv dono zero se start hote hain, to shuru mein value bahut chhoti (biased) aati hai. Isliye (1βt)(1-\beta^t) se divide karke usko "un-shrink" karte hain. Yeh training ke shuruaat mein bahut zaroori hai, warna early steps blow up ho sakte hain. Steady state mein constant gradient par Adam ka step ηsign(g)\eta\,\text{sign}(g) ho jaata hai — magnitude gradient ke size se independent, sirf direction pe depend.

AdamW ek chhota par important fix hai. Normal weight decay (L2) mein tum λθ\lambda\theta ko gradient mein add karte ho, par Adam usko v^\sqrt{\hat v} se divide kar deta hai — matlab jinke gradient bade, unpe decay kam, jinke chhote unpe zyada. Yeh galat hai! AdamW decay ko alag rakhta hai: har step ηλθ\eta\lambda\theta seedha subtract, sab weights pe equal shrinkage. Isiliye aajkal transformers mein AdamW default optimizer hai. Yaad rakho: Mean upar, RMS neeche, Weight-decay alag.

Go deeper — visual, from zero

Test yourself — Training Deep Networks

Connections