Step
Leader
At the minimum
Start
Surface
Optimisers
Learning
Playback
View

More information about gradient descent

Optimisers racing downhill

Training a neural network means walking downhill on a loss surface, one gradient step at a time. Here the surface is a map you can see: drag the start point, and gradient descent, momentum, Nesterov, RMSProp and Adam race to the bottom side by side, each leaving its path on the contours while the chart logs its loss after every step. Change the learning rate and each optimiser's settings to see why some zigzag, some overshoot, some stall and some fly. Gradient descent finds the lowest point of a surface by always stepping downhill, against the gradient, the direction in which the loss rises fastest. The colours and contour lines show the loss f(x, y): dark blue valleys are low, pale peaks are high, and the white stars are the minima. Up to five optimisers race from the white start point; drag it anywhere, or click the map, and they race again. Gradient descent takes steps of η times the gradient. Momentum keeps a velocity that builds up along a valley and damps the zigzag across it; Nesterov looks ahead before it takes the gradient. RMSProp and Adam divide each step by a running average of the squared gradients, so their steps are about α long whatever the slope; Adam adds momentum and corrects the averages' start. The chart shows the loss above the minimum after every step on a logarithmic scale. Try a learning rate that is too large and watch gradient descent overshoot and diverge, the saddle point that stalls plain gradient descent, Himmelblau's four valleys, and Rastrigin's egg box of local minima, where momentum can roll through to the global minimum. Press Play, Step or Back; save the picture, record a video with a tone that falls with the loss, or open it in the photo editor with the pencil, where the map, the contours, each path, the start, the minima and the chart are layers of their own.

Як це працює

  1. Press Play: every optimiser that is ticked starts from the white point and steps downhill; the chart logs its loss above the minimum after each step. Step and Back go one step at a time.
  2. Drag the white start point, or click anywhere on the map, and the race starts again from there; while you drag, the whole paths follow the pointer.
  3. Change the learning rate η of gradient descent, momentum and Nesterov, or the rate α of RMSProp and Adam; push η past the limit and watch gradient descent overshoot and diverge.
  4. Pick another surface or example: the zigzag in a narrow bowl, the saddle point, Himmelblau's four valleys, or Rastrigin's local minima where only momentum rolls through.

Downhill, one step at a time

The gradient ∇f points in the direction in which the loss rises fastest, so a small step against it lowers the loss. Gradient descent repeats x ← x − η∇f(x), where the learning rate η sets the step. Too small and it crawls; too large and it overshoots the valley floor, bounces from side to side and, above 2 / λ for the steepest curvature λ, flies off. In a long narrow valley the gradient points mostly across the valley rather than along it, so plain gradient descent zigzags and makes slow progress towards the minimum: the problem is ill-conditioned, and the ratio of the largest to the smallest curvature, the condition number, says how badly.

Momentum and Nesterov

Momentum, Boris Polyak's heavy ball of 1964, keeps a velocity v ← βv − η∇f(x) and moves by it. Steps that agree along the valley add up, steps that alternate across it cancel, so the zigzag is damped and the descent speeds up by up to a factor 1 / (1 − β). Momentum also tolerates a larger learning rate, and its inertia can carry it over small bumps and out of shallow local minima. Yurii Nesterov's accelerated gradient (1983) takes the gradient at the point the velocity is about to carry it to, x + βv, so it brakes before it overshoots; on convex problems it provably converges faster than gradient descent.

RMSProp and Adam

Adaptive methods give every coordinate its own step size. RMSProp, proposed by Geoffrey Hinton in a 2012 lecture, keeps a running average of the squared gradients and divides each step by its square root, so steep directions take smaller steps and flat ones larger steps: steps are about α long whatever the slope. Adam, by Diederik Kingma and Jimmy Ba (2014), adds momentum on top: a running average of the gradients over a running average of their squares, both corrected for starting at zero. Adam with α = 0.001, β₁ = 0.9 and β₂ = 0.999 is the default optimiser of deep learning, though with a constant step it keeps hovering around a minimum rather than settling exactly on it.

Valleys, saddles and local minima

Rosenbrock's function (1960) hides its minimum at (1, 1) at the end of a long, curved, nearly flat valley that is easy to find and hard to follow. The saddle surface has a point where the gradient vanishes but which is a minimum in one direction and a maximum in the other; plain gradient descent creeps away from it slowly, while momentum and adaptive methods escape fast. In high-dimensional neural networks saddle points are far more common than bad local minima. Himmelblau's function has four equally deep minima, so where an optimiser ends depends on where it starts and how it moves, and Rastrigin's function is an egg box of local minima where gradient steps get stuck in the nearest dip.

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

What is gradient descent?

An iterative method for finding a minimum of a function: start somewhere, compute the gradient, the direction of steepest ascent, and take a step the other way, x ← x − η∇f(x). Repeated, the steps run downhill until the gradient is zero. It is how almost every neural network is trained, with the loss over the training data as the function and the network's weights as the coordinates.

What does the learning rate do?

It sets the size of each step, η times the gradient. A small learning rate is safe but slow; a large one is fast until it overshoots. On a surface whose steepest curvature is λ, gradient descent diverges once η is larger than 2 / λ: each step lands farther up the opposite wall. In the app, the Learning rate too large example shows gradient descent diverging while momentum, at the same rate, still converges.

Why does gradient descent zigzag?

In a narrow valley the walls are much steeper than the floor, so the gradient points mostly across the valley. A step big enough to make progress along the floor overshoots across it, and the next step comes back. Momentum averages the steps, so the back-and-forth cancels while the movement along the valley adds up; adaptive methods such as RMSProp and Adam scale each coordinate's step by its own gradient size.

Which optimiser is best?

None always wins. Adam is the usual default in deep learning because it needs little tuning; well-tuned stochastic gradient descent with momentum often generalises as well or better for image models. On the app's surfaces momentum is often fastest along Rosenbrock's valley, adaptive methods escape the saddle quickly, and on Rastrigin's local minima it depends on luck and inertia. The race shows the trade-offs; it does not crown a winner.

What are local minima and saddle points?

A local minimum is a dip lower than everything around it but not the lowest point overall; gradient descent stops there because the gradient is zero. A saddle point also has a zero gradient but curves up in some directions and down in others, like a mountain pass; steps away from it are tiny at first, so plain gradient descent lingers. Momentum, noise and adaptive step sizes help to get past both.

Can I use the picture?

Yes. Save gives a PNG at the size you choose, Save JSON writes the settings and every optimiser's path, loss and outcome, Record films the race with a tick whose pitch falls with the loss, and the pencil opens the picture in the PictorX photo editor with the heat map, the contours, each optimiser's path, the start, the minima and the chart on layers of their own. In the editor itself it is under Apps ▸ Science ▸ Computing ▸ Gradient Descent. Everything runs in your browser.

Gradient Descent: як записати відео?

Натисніть червону кнопку «Запис» на панелі відтворення: вона знімає те, що показує сцена. Натисніть її ще раз, щоб зупинити, і відео збережеться на вашому пристрої. Запис безкоштовний, потрібно лише увійти. Без підписки на відео в куті буде невеликий логотип PictorX. Кнопка поруч із «Запис» відкриває параметри відео: формат (як показано, квадрат, сторіз 9:16 або широкий 16:9), тривалість (доки ви не зупините, або 5, 10, 15 чи 30 секунд для соцмереж) і файл: MP4, WebM або анімований GIF. Для звуку виберіть власні звуки застосунку або одну з трьох музичних доріжок: спокійну, яскраву чи ритмічну.

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

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

Перейти до редактора