Optimizers (SGD · Momentum · Adam)
Optimizers (SGD · Momentum · Adam) — Optimizers are the update rules that drive gradient descent. Stochastic gradient descent steps on noisy mini-batch gradients; momentum accumulates velocity to power through ravines; Adam adapts a per-parameter step size. They differ most on hard surfaces like ravines and saddles.
Plain gradient descent zig-zags through narrow valleys and stalls on plateaus. Momentum gives it inertia like a rolling ball; Adam adapts each parameter’s step size automatically. Race all three down the same surface and the differences are obvious.
- SGD
- Momentum
- Adam
Optimizer controls
The idea in plain words
Plain gradient descent always steps straight downhill, which zig-zags painfully across a narrow valley. Optimizers change the update rule. Momentum accumulates velocity like a rolling ball, powering through ravines. Adam adapts a separate step size for each direction, so steep and shallow axes both move sensibly.
Race all three down the same surface and the differences are obvious: on a ravine, SGD stutters, Momentum overshoots and recovers, Adam glides. On a saddle, plain SGD can stall where the gradient nearly vanishes.
Now, the math
Each optimizer transforms the raw gradient before stepping:
- the gradient of the loss at the current point.
- momentum’s velocity — an exponential average of past gradients.
- Adam’s bias-corrected first and second moment estimates.
▸ Show the derivation
Momentum’s β (here 0.9) means each step remembers ~10 previous gradients, cancelling the side-to-side oscillation in a ravine while reinforcing the consistent downhill direction. Adam divides by √ŝ, so a direction with large gradients gets a smaller effective step — which is why it handles badly-scaled surfaces that cripple plain SGD.
Trace it by hand
All three optimizers start at (2, 2) on the ravine surface — loss equals half of (4 times x squared plus 0.35 times y squared) — with eta = 0.1 and momentum beta = 0.9, exactly as in the race interactive. Numbers come from the repo's own optimizer code, rounded to 2 decimals.
The gradient at the shared start
The steep wall of the ravine makes the x gradient about 11 times the y gradient — this imbalance is the whole story.
SGD: step straight downhill
Its second step lands at (0.72, 1.86) with loss 1.64 — progress along the valley floor stays painfully slow.
Momentum: velocity accumulates
Momentum's first step matches SGD (velocity starts at zero); by step two the remembered gradient makes the x move 2.5 times bigger.
Adam: normalize each direction
At step one the bias corrections cancel, leaving g over its magnitude per axis. The demo scales Adam's eta by 6, giving the 0.6 step; step two lands at (0.81, 0.81).
Loss after two steps of each
Same surface, same start, same learning rate — only the update rule differs.
What just happened: From an 11-to-1 gradient imbalance, SGD crawled along y while momentum's accumulated velocity slammed x to exactly 0 in two steps, and Adam ignored the imbalance entirely, stepping 0.6 on both axes. The transform applied to the raw gradient is what separates them.
Now Break It
Try this: Raise the learning rate until all three diverge; pick a saddle where plain SGD stalls.
Control: Learning-rate slider (set high) / surface picker (saddle)
What happens: Diverged! The learning rate is too high — every optimizer overshoots and the loss explodes.
Where optimizers (sgd · momentum · adam) is used
Optimizers decide how the gradient turns into an actual parameter update, and the difference between them shows up in real training runs. Plain stochastic gradient descent, often with a momentum term, remains the standard for training image classifiers like ResNet, where it frequently produces the best final accuracy. Momentum accelerates progress by accumulating a velocity across steps, helping the update roll through flat regions and dampen oscillations in narrow valleys. Adam, which adapts a separate step size for each parameter using running estimates of the gradient and its square, is the default for training transformers and large language models because it converges quickly and tolerates sparse, noisy gradients. Variants such as AdamW, RMSProp, and Adagrad appear across recommendation systems, speech models, and reinforcement learning where different gradient behaviors demand different update rules.
A common misconception is that Adam is always better than SGD. Adam usually trains faster and needs less learning-rate tuning, but well-tuned SGD with momentum often generalizes better on vision benchmarks, which is why both persist. A second pitfall is thinking an optimizer removes the need to set a learning rate. Every optimizer here, including Adam, still has a base learning rate that must be chosen, and a bad value causes divergence or stagnation regardless of the method. People also confuse the optimizer with the loss function: the loss defines what to minimize, while the optimizer defines how to move parameters toward that minimum. Choosing an optimizer is about the update rule, not the objective itself.
Frequently asked questions
What does an optimizer do in machine learning?
What is the difference between SGD, Momentum, and Adam?
Which optimizer should I use?
Is Adam always better than SGD?
Do I still need to set a learning rate when using Adam?
What is momentum and why does it help?
Written & reviewed by the ML Visualization team · Last updated .