Optimizers, In Full Depth
Calculus & Optimization introduced gradient descent and briefly named the adaptive optimizers. This page derives each one and shows, visually, why the field moved from plain SGD to today's defaults.
SGD, Momentum, and Adam racing toward the minimum of an elongated ("ravine") loss surface
Watch the three paths above on this deliberately elongated bowl (steep in one direction, shallow in the other — a stand-in for real loss surfaces, which are rarely nicely circular). Plain SGD (red) crawls along the shallow direction and stalls far from the minimum within the step budget. Momentum (gold) overshoots and oscillates before settling — it builds up speed but has to fight that speed to turn. Adam (green) adapts its step size per-dimension and reaches the minimum's neighborhood fastest, though it overshoots once too. This single picture motivates every optimizer below.
Now try it yourself — same bowl, your starting point, your learning rate:
Gradient Descent Variants (by batch size)
- Batch Gradient Descent: compute the gradient using the entire dataset, then take one step. Accurate direction, but one step per full pass over the data — painfully slow for large datasets, and requires holding the whole dataset's gradients in memory at once.
- Stochastic Gradient Descent (SGD): one example at a time. Extremely noisy gradient estimates, but that noise actually helps escape shallow local minima and saddle points — and it enables starting to learn before even seeing the full dataset once.
- Mini-Batch Gradient Descent: the practical universal default — a small batch (32-512 examples) per step. Balances gradient accuracy against update frequency, and maps naturally onto GPU parallelism (see Algorithms & Data Structures). When people say "SGD" in a deep learning context, they almost always mean this.
The base update rule, regardless of batch size: .
Momentum-Based Methods
Momentum: accumulate a running (exponentially-weighted) average of past gradients, and step in that direction instead of the raw current gradient:
Like a ball rolling downhill: it builds speed in a consistent direction and dampens oscillation across a ravine's steep walls (see the chart above). (typically 0.9) controls how much history persists.
Nesterov Momentum: a lookahead correction — compute the gradient not at the current position, but at the position momentum is about to carry you to, then adjust. This "look before you leap" adjustment gives noticeably better convergence than vanilla momentum on the same budget, at almost no extra cost.
Adaptive Learning Rate Methods
Momentum uses the same effective learning rate for every parameter. Adaptive methods scale the learning rate per-parameter, based on that parameter's own gradient history.
AdaGrad: divide each parameter's learning rate by the square root of the sum of all its squared past gradients: , where accumulates over every step so far. Parameters that get frequent large gradients (common features) end up with a smaller effective learning rate; rare features keep a larger one — good for sparse data. Fatal flaw: only ever grows, so the effective learning rate eventually decays to ≈0 and training stalls, even if far from converged.
RMSProp: fixes AdaGrad's decay problem directly — replace the sum of squared gradients with an exponentially-decaying moving average, so old gradients get forgotten instead of accumulating forever: . The learning rate can now go back up if recent gradients shrink, instead of monotonically dying.
AdaDelta: a further refinement of RMSProp that removes the need to manually set a global learning rate at all — it uses the ratio of recent parameter update magnitudes to recent gradient magnitudes to derive an implicit step size automatically.
Adam and Its Descendants
Adam (Adaptive Moment Estimation): combines momentum (a moving average of the gradient itself — the "first moment") with RMSProp-style adaptive scaling (a moving average of the squared gradient — the "second moment"):
The step is bias correction — early in training, and start at zero and are biased toward zero for the first several steps; dividing by corrects for this so early updates aren't artificially shrunk. Adam is the default optimizer for the overwhelming majority of deep learning today, including Transformers (see Attention & Transformers).
AdamW: a small but important fix to Adam — standard Adam applies L2 weight decay by adding it into the gradient before the adaptive scaling, which means the effective decay strength gets distorted by each parameter's own . AdamW instead applies weight decay directly to the weights, decoupled from the adaptive step — this is now the actual default in most modern training code (including every major LLM), not plain Adam.
AdaMax: a variant of Adam using the norm (max of past gradient magnitudes) instead of the norm for the second moment — occasionally more stable, rarely the default.
Nadam: Adam with Nesterov's lookahead correction folded in, combining both ideas.
Lion (EvoLved Sign Momentum): a much simpler, more memory-efficient recent optimizer that only tracks momentum (no second moment) and uses the sign of the momentum for the update rather than its magnitude — found via automated search rather than hand-derivation, and competitive with AdamW on some large-scale training runs while using less memory (no to store).
LAMB (Layer-wise Adaptive Moments): wraps Adam's update with an additional per-layer scaling based on the ratio of weight norm to update norm — specifically designed to keep training stable when using very large batch sizes (thousands+), where plain Adam can become unstable.
Choosing an Optimizer
- Default, almost always: AdamW.
- Memory-constrained / very large models: Lion (fewer optimizer states to store) or 8-bit Adam variants.
- Very large batch, distributed training: LAMB.
- Classical ML / convex problems (see Linear Regression): plain SGD or even the closed-form solution, since there's no messy non-convex surface to navigate.
- Sparse features (e.g. large embedding tables): AdaGrad's original motivation still applies, though Adam-family optimizers now dominate in practice regardless.
Minimal Implementation
Next: Weight Initialization, Regularization & LR Scheduling — the remaining training-loop decisions that determine whether an optimizer like this actually converges well.