Training a model means minimizing a loss over parameters, and almost every method in use is some variant of “step downhill using the gradient”. The variants differ in how much data goes into each gradient, how past gradients are remembered, and how the step size is chosen. I find the clearest way to understand them is a two-dimensional quadratic with a long narrow valley, where each trick has a visible job to do.

Walk downhill one small step at a time until you reach the bottom of the valley. Image: Babayaga94, CC0, via Wikimedia Commons.

Mental Model

  • Plain gradient descent is . Its speed is limited by the condition number , the ratio of the steepest to the flattest curvature.
  • Stochastic and mini-batch gradients are noisy but unbiased estimates of the full gradient, and they are far cheaper per step.
  • Momentum averages gradients over time, which damps oscillation across a valley and builds speed along it.
  • Adaptive methods (RMSProp, Adam) rescale each coordinate by a running estimate of its gradient magnitude.
  • The learning rate usually needs a schedule: a short warmup, then a decay.

1. Batch, Stochastic and Mini-Batch

For a loss that averages over examples, , the full gradient needs a pass over all of them. Three options:

VariantGradient fromCost per stepGradient noise
Batchall exampleshighnone
SGD1 exampletinyvery high
Mini-batch examples (often 32 to 512)moderate, parallelizes on a GPUshrinks like

Each mini-batch gradient is an unbiased estimate of the full gradient, so on average you still move downhill. The noise also has a benefit: it helps the iterates leave sharp minima and saddle points, and with a constant learning rate it makes the parameters jitter in a ball around the optimum rather than settle exactly. That is why a decaying learning rate (section 5) is standard. “SGD” in a deep learning library almost always means mini-batch SGD.

2. Learning Rate and Conditioning

Take with eigenvalues (smallest) and (largest) of . Gradient descent gives , so along each eigendirection the error is multiplied by . Two constraints follow.

For the steep direction to stay stable you need , i.e. . For the flat direction to make progress you want small, which asks for a large . The best fixed step is , and the per-step contraction is then

With , : the step must stay below , the best step is , and the error shrinks by only per iteration. Reaching a 100x smaller error takes on the order of steps. Neural network losses are not quadratic, but near a minimum the Hessian eigenvalue spread plays the same role, and it is typically wide.

Intuition

The loss surface is a long narrow canyon. A step size that is safe across the canyon is tiny along it. Gradient descent zigzags between the walls and creeps along the floor.

3. Momentum

Heavy-ball momentum keeps a velocity that accumulates gradients:

Across the canyon the gradient flips sign every step, so the contributions cancel in . Along the canyon they all point the same way and add up, so the effective step grows toward . On quadratics, tuned momentum improves the rate from about to about steps. With the velocity averages over roughly the last 10 gradients. Nesterov momentum evaluates the gradient at the look-ahead point and is a small, usually beneficial change.

4. RMSProp and Adam

RMSProp divides each coordinate’s gradient by a running root-mean-square of its recent values:

Coordinates with consistently large gradients get smaller steps and coordinates with small gradients get larger ones, which partly undoes bad scaling between parameters. Adam (Kingma and Ba, 2015) combines this with momentum on the gradient itself:

The usual defaults are , , . The bias correction exists because . At , is ten times too small, while dividing by restores . For the effect is bigger (). Without the correction the first steps would be badly scaled. As grows the denominators approach 1 and the correction disappears.

Since is roughly of size 1, each parameter moves by about per step regardless of gradient scale. That makes easy to reason about (values near to are common) and makes Adam forgiving of badly scaled gradients.

AdamW Versus L2

L2 regularization adds to the gradient. Inside Adam that term is divided by along with everything else, so parameters with a large gradient history are regularized less, and the penalty no longer behaves like weight decay. AdamW (Loshchilov and Hutter, 2019) decouples them and applies the decay directly:

For plain SGD the two forms are equivalent up to rescaling . For Adam they are different, and AdamW is the standard choice for training transformers.

5. Learning-Rate Schedules

A constant rate is rarely best. Two pieces recur.

Warmup raises the rate linearly from near zero to over the first steps. Early in training the second-moment estimate is built from few, noisy gradients and activations are far from a sensible regime, so large steps can blow up. Warmup is usually a few hundred to a few thousand steps for large models.

Cosine decay then lowers the rate smoothly:

It decays slowly at first, fastest in the middle and slowly at the end, ending near . Step decay (divide by 10 at fixed epochs) and linear decay are the main alternatives. The decay lets the noisy iterates settle into a minimum instead of orbiting it.

6. Experiment: An Ill-Conditioned Quadratic

import numpy as np
 
lam = np.array([1.0, 100.0])            # Hessian eigenvalues, kappa = 100
grad = lambda x: lam * x
f = lambda x: 0.5 * np.sum(lam * x**2)
 
def run(update, steps=200):
    x, state = np.array([1.0, 1.0]), {}
    for t in range(1, steps + 1):
        x = update(x, grad(x), state, t)
    return f(x)
 
def gd(lr):
    return lambda x, g, s, t: x - lr * g
 
def momentum(lr, b=0.9):
    def u(x, g, s, t):
        s["v"] = b * s.get("v", 0) + g
        return x - lr * s["v"]
    return u
 
def adam(lr, b1=0.9, b2=0.999, eps=1e-8):
    def u(x, g, s, t):
        s["m"] = b1 * s.get("m", 0) + (1 - b1) * g
        s["v"] = b2 * s.get("v", 0) + (1 - b2) * g * g
        mh, vh = s["m"] / (1 - b1**t), s["v"] / (1 - b2**t)
        return x - lr * mh / (np.sqrt(vh) + eps)
    return u
 
for name, u in [("GD lr=0.0198", gd(0.0198)), ("GD lr=0.021", gd(0.021)),
                ("momentum lr=0.01", momentum(0.01)), ("Adam lr=0.1", adam(0.1))]:
    print(f"{name:18s} f after 200 steps = {run(u):.2e}")

Running this from the start point , where , gives these values of after 200 steps:

Method after 200 steps
GD, (near the best fixed step)
GD, (just above ) (diverged)
Momentum, ,
Adam,

The first two rows are the stability limit in action: a 6% larger step turns convergence into explosion. Momentum gets about six orders of magnitude further than tuned plain GD, matching the versus picture. Adam does well here partly because the problem is axis-aligned, so per-coordinate scaling removes the ill-conditioning entirely. Rotate the quadratic so its valley lies along a diagonal and the diagonal preconditioner helps much less. Adam with a constant step also does not converge exactly in noisy settings, which brings back the schedules from section 5.

Common mistake

Comparing optimizers at one shared learning rate says almost nothing, because each has a different good range. Tune per method, and report the best of each.