Chapter 07 · Optimization
Walking down a mountain blindfolded
Training a model means searching for the lowest point of a landscape with billions of dimensions, in thick fog, feeling only the slope underfoot. Gradient descent, invented by Cauchy in 1847 to compute orbits, is still the algorithm that trains every LLM.
In this chapter
In 1847 Augustin-Louis Cauchy presented to the Paris Academy of Sciences a three-page note entitled “General method for solving systems of simultaneous equations”. He was interested in astronomical calculations: determining the orbit of a celestial body from observations leads to systems of equations that cannot be solved exactly. His proposal was to turn the problem into minimizing a function that measures the error and to walk down it step by step, always in the direction of steepest slope. He had invented gradient descent.
The algorithm
Start from any parameters and repeat:
The gradient, which backpropagation computes cheaply, points in the direction of steepest ascent, so we step the opposite way. The number is the learning rate: the size of the step. It is the most important hyperparameter of any training run.
When does it work? Convexity and smoothness
Guaranteeing that the algorithm works takes two conditions on the landscape. A function is convex if the segment between any two points of its graph lies above it, that is, if it is bowl-shaped. It is -smooth if its gradient changes no faster than : .
If is convex, every local minimum is a global minimum. If it is also differentiable, is enough for to be a global minimum.
If is convex and -smooth and is used, then after steps
If it is also -strongly convex, convergence is geometric: the error is multiplied at each step by at most .
The quadratic case, where everything is visible
Take with symmetric and eigenvalues . Since , the algorithm does . In the eigenvector basis, each coordinate evolves on its own:
It converges if and only if for every , that is, if . If , that coordinate changes sign at every step (the zigzag). The slowest direction is that of , and with the best the error shrinks per step by a factor , where is the condition number. Narrow, elongated valleys (large ) are slow.
Neural networks are not convex: their loss landscape is full of local minima, plateaus and saddle points. No classical theorem guarantees that gradient descent finds a good minimum there, and yet it does, again and again. Some partial explanations: in high dimension almost all critical points are saddle points rather than minima (Dauphin et al., 2014), and in heavily overparameterized networks most local minima are almost as good as the global one. It is one of the big open questions of the field.
Inertia: momentum and acceleration
In an elongated valley the gradient almost always points at the walls rather than towards the bottom. In 1964 Boris Polyak proposed the heavy-ball method: accumulate a velocity that averages previous gradients,
The oscillations cancel out and progress in the right direction builds up. In 1983 Yurii Nesterov found a variant that reaches error instead of for smooth convex functions, and showed that this is the best possible:
No method that only uses gradients can guarantee, for every convex -smooth function (in high enough dimension), an error smaller than after steps. Nesterov's accelerated method achieves that order.
Stochastic gradient descent
The loss of an LLM is an average over trillions of training words: . Computing the exact gradient at every step would mean going through all the data. The alternative is to estimate it with a random mini-batch of a few examples:
The estimate is noisy but unbiased: on average it points the right way. Herbert Robbins and Sutton Monro proved in 1951 that this kind of procedure converges if the learning rate decreases at the right pace.
Under regularity conditions, stochastic approximation converges if the rates satisfy
for example with . The first condition ensures you can get anywhere, and the second that the noise eventually dies out.
That is why small batches are always used in practice: many cheap, noisy steps beat a few expensive, exact ones. The noise also seems to help escape “sharp” minima and find “flatter” ones, which tend to generalize better.
Adam: a learning rate for every parameter
The parameters of a network are not alike: some receive huge gradients and others tiny ones. In 2014 Diederik Kingma and Jimmy Ba proposed Adam, which combines momentum with a normalization by the recent magnitude of each gradient:
Here and are the averages corrected for their initial bias. Its variant with decoupled weight decay, AdamW (2017), is the optimizer practically every current LLM is trained with, almost always with a rate that rises during an initial warm-up and then decays, in the spirit of what Robbins and Monro asked for.
We now have a general procedure for fitting parameters. The next question is which family of functions to parameterize, and here the oldest idea in AI appears: imitating neurons.
References
- A.-L. Cauchy (1847). “Méthode générale pour la résolution des systèmes d'équations simultanées”. Comptes Rendus de l'Académie des Sciences, 25.
- H. Robbins and S. Monro (1951). “A Stochastic Approximation Method”. Annals of Mathematical Statistics, 22(3).
- B. T. Polyak (1964). “Some methods of speeding up the convergence of iteration methods”. USSR Computational Mathematics and Mathematical Physics, 4(5).
- Y. Nesterov (1983). “A method for solving the convex programming problem with convergence rate O(1/k²)”. Doklady Akademii Nauk SSSR, 269.
- Y. Dauphin et al. (2014). “Identifying and attacking the saddle point problem in high-dimensional non-convex optimization”. NeurIPS.
- D. P. Kingma and J. Ba (2015). “Adam: A Method for Stochastic Optimization”. ICLR.
- I. Loshchilov and F. Hutter (2019). “Decoupled Weight Decay Regularization”. ICLR.
- S. Bubeck (2015). “Convex Optimization: Algorithms and Complexity”. Foundations and Trends in Machine Learning, 8(3–4).