Classical machine learningML 02
Gradient Descent
Most models are not solved with a formula but trained: start with some guess for the parameters, measure how wrong the model is, and nudge the parameters in the direction that reduces the error fastest. That direction is the negative gradient, and repeating the nudge is gradient descent. Here it fits the straight line from the regression demo, SBP = a + b × age, so every guess is a point on the map of the error on the left and a line through the data on the right. Press play to watch it walk downhill, or click the map to start somewhere else. Then switch to age in years to see how much the scale of the data matters.
Progress
Loss at each step
the landscape shows the mean squared error for every possible line: across, the slope b; up, the intercept a · each orange dot is one step · loss chart on a log scale
The learning rate
Each step moves the parameters by the learning rate times the gradient. Too small and the walk takes forever; too large and each step overshoots the bottom of the valley and lands higher up the opposite wall, so the error grows instead of shrinking. Try raising the learning rate step by step: with standardised age the walk overshoots and zigzags above 0.5 and diverges above 1; with age in years the same happens at rates two thousand times smaller: it zigzags across the valley above about 1.6 × 10⁻⁴ and diverges above about 3.2 × 10⁻⁴.
Why scaling matters
With age in years (25 to 80), a small change in the slope moves the line far more than the same change in the intercept, so the error landscape is a long, narrow valley: steep across, almost flat along. A learning rate small enough not to overshoot across the valley crawls along it: the path snaps sideways to the valley floor within a few steps and then barely moves, because it is now creeping along a nearly flat slope. From the default start, plain gradient descent at 2.8 × 10⁻⁴ needs about 157,000 steps to arrive. Momentum at the same rate needs about 16,000, roughly ten times fewer, as keeping 90% of each step predicts; its default rate is lower, 10⁻⁴, which takes about 44,000. Standardising age (subtracting the mean and dividing by the standard deviation) turns the valley into a round bowl, and the same method arrives in under a hundred steps. The condition number in the table, the ratio of the steepest to the flattest curvature, measures the problem: about 27,000 in years, exactly 1 when standardised (with the standard deviation computed over all n points, dividing by n). This is why features are usually standardised before training.
Momentum, Adam and mini-batches
Momentum (Polyak, 1964) lets each step keep 90% of the previous one, so the zigzags across the valley cancel out while progress along it builds up speed. Adam (Kingma & Ba, 2015) also keeps a running average of the size of each parameter’s gradient and divides by it, so every parameter moves at a similar pace whatever its scale. With large datasets, computing the gradient over every point for every step is too slow, so each step uses a small random mini-batch instead: stochastic gradient descent, going back to Robbins and Monro (1951). The path becomes noisy but much cheaper per step; set the batch to 1 or 5 to see it. Neural networks, including large language models, are trained with variants of these same methods. For this line there is also an exact formula (the regression demo uses it), which makes it easy to check that every method ends up in the same place.
References
- Cauchy, A. (1847). Méthode générale pour la résolution des systèmes d’équations simultanées. Comptes rendus hebdomadaires des séances de l’Académie des sciences, 25, 536–538.
- Robbins, H., & Monro, S. (1951). A stochastic approximation method. The Annals of Mathematical Statistics, 22(3), 400–407. doi.org/10.1214/aoms/1177729586
- Polyak, B. T. (1964). Some methods of speeding up the convergence of iteration methods. USSR Computational Mathematics and Mathematical Physics, 4(5), 1–17. doi.org/10.1016/0041-5553(64)90137-5
- Kingma, D. P., & Ba, J. (2015). Adam: a method for stochastic optimization. 3rd International Conference on Learning Representations (ICLR), San Diego. arxiv.org/abs/1412.6980