Optimizers: SGD, momentum, Adam

core 11 minfundamentalsoptimizationtraining

Builds on:Backpropagation

The idea

Every optimizer in common use is gradient descent with refinements. The ladder, one rung at a time:

SGD. θ←θ−η gt\theta \leftarrow \theta - \eta\, g_t, with gtg_t the gradient on a random minibatch. Unbiased but noisy, and one global η\eta has to suit every parameter in the network at once.

Watch the learning rate itself decide the outcome on a single parameter — too small crawls, too large overshoots and oscillates, too large still and it diverges:

Each step scales distance-to-minimum by (1 − 2η) ≈ 0.40 — at η = 0.30 that is converging smoothly.

+ Momentum. Keep a velocity — an exponentially weighted average of recent gradients — and step along it:

vt=βvt−1+gt,θ←θ−η vtv_t = \beta v_{t-1} + g_t, \qquad \theta \leftarrow \theta - \eta\, v_t

With β=0.9\beta = 0.9, a direction the gradients agree on compounds toward a terminal velocity of g/(1−β)=10gg/(1-\beta) = 10g, while components that alternate sign cancel. Effect: faster along consistent directions, damped oscillation across steep narrow ravines, minibatch noise averaged out.

The ravine language is literal — here is a basin with exactly that kind of ridge running across it, and a ball spiraling down into it:

Loading the interactive 3D loss-surface visualization…

+ Per-parameter scale (RMSProp idea). Track a running average of the squared gradient and divide by its root, so each parameter gets a step size inversely proportional to its typical gradient magnitude. Parameters with rare, small gradients (embedding rows, early layers) stop being starved by a global η\eta chosen for the loudest layer.

Adam = both + bias correction.

mt=β1mt−1+(1−β1) gt,vt=β2vt−1+(1−β2) gt2m_t = \beta_1 m_{t-1} + (1-\beta_1)\, g_t, \qquad v_t = \beta_2 v_{t-1} + (1-\beta_2)\, g_t^2

m^t=mt1−β1t,v^t=vt1−β2t,θ←θ−η m^tv^t+ϵ.\begin{aligned} \hat{m}_t &= \frac{m_t}{1-\beta_1^t}, \\ \hat{v}_t &= \frac{v_t}{1-\beta_2^t}, \\ \theta &\leftarrow \theta - \eta\, \frac{\hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon}. \end{aligned}

Defaults: β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ϵ=10−8\epsilon = 10^{-8}. The m^/v^\hat{m}/\sqrt{\hat{v}} ratio is roughly gradient-scale-free, which is why Adam works out of the box across wildly different architectures.

AdamW fixes one subtlety: L2 regularization added into the gradient gets divided by v^\sqrt{\hat{v}} like everything else, so exactly the weights with large gradient variance feel less decay. AdamW applies decay directly to the weights, θ←(1−ηλ)θ\theta \leftarrow (1 - \eta\lambda)\theta, outside the adaptive machinery — the standard for transformers.

Worked example

Bias correction, with numbers. Single parameter, constant gradient g=1g = 1, defaults above. At t=1t = 1:

m1=0.1⋅1=0.1,v1=0.001⋅1=0.001m_1 = 0.1 \cdot 1 = 0.1, \qquad v_1 = 0.001 \cdot 1 = 0.001

Uncorrected, the step would be η m1/v1=η⋅0.1/0.0316≈3.16 η\eta\, m_1/\sqrt{v_1} = \eta \cdot 0.1/0.0316 \approx 3.16\,\eta — a 3x overshoot on step one, purely an artifact of the zero-initialized averages (and it gets worse for smaller β1\beta_1/larger β2\beta_2 gaps). With correction:

m^1=0.11−0.9=1,v^1=0.0011−0.999=1,step=η⋅11+ϵ≈η.\begin{aligned} \hat{m}_1 &= \frac{0.1}{1 - 0.9} = 1, \\ \hat{v}_1 &= \frac{0.001}{1 - 0.999} = 1, \\ \text{step} &= \eta \cdot \frac{1}{\sqrt{1} + \epsilon} \approx \eta. \end{aligned}

Exactly the sensible thing: first step of size η\eta in the gradient direction.

Scale invariance, same setup. Repeat with g=0.001g = 0.001 everywhere: m^1=0.001\hat{m}_1 = 0.001, v^1=10−6\hat{v}_1 = 10^{-6}, step =η⋅0.001/0.001=η= \eta \cdot 0.001/0.001 = \eta. Identical. Adam takes η\eta-sized steps whether the raw gradients are of order 11 or 10−310^{-3} — with SGD, those two scenarios would need learning rates 1000x apart. That is the practical content of "adaptive".

Cheat sheet

OptimizerUpdateAddsState/param
SGDθ←θ−ηg\theta \leftarrow \theta - \eta g—0
SGD + momentumv←βv+gv \leftarrow \beta v + g; θ←θ−ηv\theta \leftarrow \theta - \eta vdirection smoothing1
RMSPropθ←θ−ηg/(s+ϵ)\theta \leftarrow \theta - \eta g / (\sqrt{s} + \epsilon), ss EMA of g2g^2per-param scale1
Adammomentum + RMS scale + bias correctionboth2
AdamWAdam + decoupled decay (1−ηλ)θ(1 - \eta\lambda)\thetacorrect weight decay2

Symptoms → fixes