Gradient descent and its variants
The optimiser behind most of machine learning: learning rates, momentum, SGD, mini-batches and Adam.
Animated explanation
The idea
Training a model means finding weights with low loss. Picture the loss as a landscape over all possible weights. You stand somewhere on it, blindfolded, and can only feel the slope under your feet. The sensible move: take a step downhill, feel again, repeat.
The gradient points uphill, so we step along . The learning rate sets the step size. Too small and you crawl; too large and you overshoot and bounce, or even fly off. Almost every model in this course, from logistic regression to deep reinforcement learning, is trained by some version of this loop.
Why the learning rate is so sensitive
Near a minimum, the loss looks like a bowl . Each step multiplies the error along the bowl's -th axis by , where are the eigenvalues of (the curvatures). Stability needs for all , so . But progress along the flattest axis is governed by .
The ratio (the condition number) therefore sets how slow plain gradient descent is. Feature scaling, momentum and adaptive methods all exist to fight bad conditioning.
Stochastic and mini-batch gradients
When the loss is an average over examples, the full gradient costs . A random mini-batch gives an unbiased estimate for a fraction of the price. The steps are noisy, so we usually decay the learning rate over time (e.g. or a cosine schedule) and shuffle the data each epoch.
Momentum and Adam
Momentum keeps a running velocity, so consistent gradient directions accelerate while oscillating ones cancel. Adam keeps running averages of the gradient () and of its square (), corrects their start-up bias, and steps by : each weight gets its own effective step size. Adam with is the default for neural networks; plain SGD with momentum often generalises slightly better when well tuned.
The algorithm, step by step
- 1Initialise (zeros for convex models, small random values for networks).
- 2Repeat for each epoch: shuffle the data and split it into mini-batches.
- 3For each batch : compute .
- 4Update: , then (β = 0 gives plain SGD).
- 5Stop when validation loss stops improving (early stopping) or after a fixed budget.
The maths
Gradient descent
Momentum
Adam
Stability on a quadratic
Strengths and weaknesses
Strengths
- Works for any differentiable loss, at any scale
- Mini-batches make each step cheap
- Momentum and Adam make it robust in practice
Weaknesses
- Needs a learning rate (and schedule) to be tuned
- Slow on badly conditioned problems without scaling
- On non-convex losses it finds a local, not global, minimum
Reach for it when…
Check your understanding
Lambdian Lab
Three optimisers on one problem
Fit a linear regression (no bias) by minimising the MSE .
- gd(X, y, lr, steps): plain batch gradient descent from zeros; return w.
- momentum(X, y, lr, beta, steps): heavy-ball momentum.
- sgd(X, y, lr, epochs, batch, seed): mini-batch SGD, reshuffling each epoch with np.random.default_rng(seed).
- Then plot the loss per step for gd and momentum (matplotlib) to see the difference.