Gradient descent explained

Almost every machine learning model you have heard of, from a straight-line fit to a large language model, was trained by the same simple idea: measure how wrong the model is, work out which way to change its parameters to make it a little less wrong, take a small step that way, and repeat. That idea is gradient descent. This page explains how it works, why it sometimes struggles, and how momentum, Nesterov, RMSProp and Adam improve on it.

A narrow loss valley drawn as blue contour bands, with the coloured paths of gradient descent zigzagging across it, momentum sweeping through and overshooting, and Adam curving in, from a white start point to the minimum
A narrow loss valley drawn as blue contour bands, with the coloured paths of gradient descent zigzagging across it, momentum sweeping through and overshooting, and Adam curving in, from a white start point to the minimum

The idea: walk downhill

Picture the loss of a model as a landscape: every point is a choice of parameters, and the height is how badly the model does with them. The aim is the lowest point. You cannot see the whole landscape, but at any point you can feel the slope under your feet: the gradient ∇f, the vector of partial derivatives, which points in the direction in which the loss rises fastest and whose length says how steep it is. Gradient descent steps the other way, x ← x − η∇f(x), and repeats. Augustin-Louis Cauchy described the method in 1847 for solving systems of equations; today it trains neural networks with billions of parameters, where the gradient is computed by backpropagation.

The learning rate and the stability limit

The learning rate η sets the step size, and it is the most important setting. Near a minimum the surface looks like a bowl whose steepest direction has curvature λ, the largest eigenvalue of the Hessian matrix of second derivatives. Along that direction each step multiplies the distance to the minimum by 1 − ηλ. For η below 1 / λ the iterates slide straight in; between 1 / λ and 2 / λ they overshoot and bounce from side to side while still closing in; above 2 / λ every bounce lands farther up the opposite wall and the iterates diverge. Practitioners therefore try rates spread over powers of ten, use schedules that start larger and shrink, or warm up from a small rate.

Narrow valleys and the zigzag

When the curvature differs greatly between directions, gradient descent has a problem. The learning rate must stay below 2 / λ for the steepest direction, but then progress along the flattest direction, with curvature μ, shrinks by only a factor 1 − ημ per step, and the number of steps grows with the condition number κ = λ / μ. Meanwhile the gradient points mostly across the valley, so the path zigzags between its walls. Rosenbrock's function, introduced by Howard Rosenbrock in 1960 as a test for optimisers, is the classic case: its minimum at (1, 1) lies at the end of a long, curved, nearly flat valley that gradient descent finds quickly and then follows painfully slowly.

Momentum and Nesterov acceleration

Boris Polyak's heavy ball method (1964) gives the walker inertia: a velocity v ← βv − η∇f(x), and a step x ← x + v. Gradients that keep pointing the same way, along the valley, add up to a speed of about η / (1 − β) times the slope; gradients that alternate, across the valley, cancel. With a well chosen β the number of steps grows only with √κ instead of κ. Yurii Nesterov's accelerated gradient (1983) evaluates the gradient at the look-ahead point x + βv, where the velocity is about to carry it, so it corrects course before overshooting; for smooth convex functions it achieves the best possible rate for any method that only uses gradients. Momentum's inertia can also roll the iterate over small bumps, which is why on Rastrigin's surface it sometimes reaches the global minimum when plain gradient descent stops in the nearest dip.

Adaptive steps: AdaGrad, RMSProp and Adam

Instead of one learning rate for all directions, adaptive methods scale each coordinate by the size of its own recent gradients. AdaGrad (Duchi, Hazan and Singer, 2011) divides by the square root of the sum of all past squared gradients, which suits sparse features but makes the steps shrink for ever. RMSProp, suggested by Geoffrey Hinton in a 2012 course, uses a running average with decay ρ instead, so the step stays about α long: s ← ρs + (1 − ρ)g², x ← x − αg / (√s + ε). Adam, by Diederik Kingma and Jimmy Ba (2014), combines this with momentum: a running mean m of the gradients with β₁ and a running mean v of their squares with β₂, both divided by 1 − βᵗ to undo their start at zero, and the step −α m̂ / (√v̂ + ε). Because the step size hardly depends on how steep the surface is, adaptive methods handle badly scaled problems and flat saddle regions well, but with a constant α they keep jittering around a minimum, and in deep learning they sometimes find solutions that generalise slightly worse than tuned momentum.

Saddle points and local minima

Wherever the gradient is zero, gradient descent stops, and not every such point is a minimum. At a saddle point the surface curves up in some directions and down in others, like a mountain pass. Gradient descent started near the ridge leaves it only slowly, because the gradient there is tiny, and the loss sits on a plateau for many steps before it drops again. In the very high-dimensional loss surfaces of neural networks, saddle points vastly outnumber poor local minima, which is one reason momentum, adaptive steps and the noise of stochastic gradients help so much. A local minimum is a true dip that is not the lowest one: Himmelblau's function has four minima of equal depth, so the minimum reached depends on the start and the path, and Rastrigin's function is an egg box with a local minimum near every point of an integer grid. No method that only follows the local slope is guaranteed to find the global minimum of such a function; restarts, global methods such as simulated annealing or genetic algorithms, or better models are needed.

Gradient descent in machine learning

A model's loss is usually an average over its training examples, and computing the exact gradient means going through all of them. Stochastic gradient descent instead uses the gradient of a small random mini-batch, a noisy but unbiased estimate that is far cheaper, so the model takes many more steps in the same time; the noise even helps it escape saddle points and sharp minima. Training a network therefore combines everything on this page: backpropagation to compute the gradient, a mini-batch to estimate it, momentum or Adam to choose the step, and a learning rate schedule with warm-up and decay. The app uses exact gradients on two-dimensional surfaces so that every path can be seen, but the behaviour it shows, zigzags, overshoots, divergence, plateaus and traps, is what engineers watch for in the loss curves of real training runs.

Поширені запитання

Why go against the gradient?

The gradient points in the direction of steepest ascent, the way the function increases fastest. For a small enough step the change in the loss is about the step times the gradient, so stepping against the gradient lowers the loss by the most for a step of that length. That is why the method is also called steepest descent.

How do I choose the learning rate?

Try a few values spread over powers of ten, such as 0.1, 0.01 and 0.001, and keep the largest one whose loss still falls smoothly; just below the rate where training becomes unstable is usually best. Schedules that lower the rate during training, and a short warm-up at the start, make the choice less critical. For a quadratic bowl the limit is exactly 2 / λ, with λ the steepest curvature.

What is the difference between gradient descent and stochastic gradient descent?

Gradient descent uses the exact gradient of the whole loss at every step. Stochastic gradient descent estimates it from one example or a small mini-batch, so each step is cheaper but noisy. In machine learning almost everyone uses the stochastic version, usually with momentum or Adam, because it makes progress on huge datasets long before a single exact gradient could be computed.

Does gradient descent always find the minimum?

On a convex function with a small enough learning rate it converges to the global minimum. On other functions it converges to some point where the gradient is zero, usually a local minimum, sometimes a saddle point it cannot leave, and with too large a learning rate it may not converge at all. Which minimum it finds depends on the start, which is why the app lets you drag the start point.

Готові спробувати?

Відкрийте PictorX у браузері — без встановлення і без кредитної картки.

Open the gradient descent app