Gradient LabSign up free

Blog › The Math

Gradient descent, step by step with real numbers

Gradient descent, step by step with real numbers
In shortGradient descent finds good model weights by repeatedly taking a small step downhill on the loss. This post works through it by hand: first on a one-weight bowl, then on a tiny linear regression with three data points. Along the way it shows why the learning rate decides whether training settles, crawls or blows up.

Imagine standing on a foggy hillside at night. You cannot see the valley, but you can feel which way the ground slopes under your feet. A sensible plan: take a small step downhill, feel the slope again, step again. Keep going and you will usually end up at the bottom.

That is gradient descent, the method behind training almost every neural network. The "hillside" is the loss, a number that measures how wrong a model's predictions are. The position on the hill is the model's weights, the numbers it learns. And the slope you feel is the gradient. This post works through the method by hand, with numbers you can check on a calculator.

The rule in one line

For a single weight w, the update is:

new w = w − learning rate × slope of the loss at w

The slope (the derivative) tells you how much the loss changes if you nudge w a little. If the slope is positive, increasing w makes the loss worse, so you decrease it. If the slope is negative, you increase it. Subtracting the slope always moves you downhill. With many weights, the slope becomes a list of slopes, one per weight, called the gradient, and the same rule applies to each weight at once.

The learning rate is a small positive number that sets the step size. The Deep Learning textbook by Goodfellow, Bengio and Courville writes the update in exactly this form and traces the method back to Cauchy in 1847.

A one-weight example you can do by hand

Take the simplest possible loss: a bowl with its lowest point at w = 3.

loss(w) = (w − 3)²

Its slope is 2 × (w − 3). At w = 0 the slope is −6: the loss drops if w goes up. Start at w = 0 with a learning rate of 0.1.

Notice the pattern. The distance to the answer goes 3, 2.4, 1.92, 1.536 and so on: it shrinks by 20% every step. That is not a coincidence. For this bowl, each step multiplies the error by (1 − 2 × learning rate), which is 0.8 here. The steps also get smaller on their own, because the slope gets gentler as you approach the bottom. Nobody has to tell the algorithm to slow down.

Reading is half of it. Gradient descent shows this step by step in about 3 minutes. Sign up free to track what you’ve learned.

When the learning rate is wrong

The same formula tells you what happens with other learning rates.

w = 3 (minimum) Learning rate 0.1: converges 0 → 0.6 → 1.08 → 1.46 → 1.77 → 2.02 w = 3 (minimum) Learning rate 1.1: diverges 0 → 6.6 → −1.32 → 8.18 …
The same bowl, two learning rates. With 0.1 each step moves downhill and the steps shrink. With 1.1 each step overshoots the bottom by more than the last, and the loss grows.

Real loss surfaces are not perfect bowls, and they have millions of weights, but the lesson carries over. A learning rate that is too large makes training unstable; one that is too small wastes compute. That is why the learning rate is usually the first setting people tune, and why schedules that change it during training are common.

A real model: linear regression with three points

Now a model with two weights. Suppose you have three data points that lie exactly on the line y = 2x + 1:

x y
1 3
2 5
3 7

The model predicts ŷ = w·x + b, where w is the slope and b the intercept. The loss is the mean squared error (MSE): the average of (prediction − target)² over the three points.

Start with w = 0 and b = 0. Every prediction is 0, so the errors (prediction minus target) are −3, −5 and −7. The loss is (9 + 25 + 49) ÷ 3 ≈ 27.67.

The gradient of MSE with respect to each weight is:

Both slopes are negative, so both weights should go up. With a learning rate of 0.1:

Check the new predictions: 3.27, 5.53 and 7.80. The errors are now 0.27, 0.53 and 0.80, and the loss has dropped from 27.67 to about 0.33 in a single step.

The next step overshoots slightly in w (2.267 → 2.018) and pulls b down a little (1.0 → 0.893), and the loss falls to about 0.005. From there progress slows, because the remaining error is spread awkwardly between w and b. After 100 steps the weights are about w = 2.004 and b = 0.991; after 500 steps they match the true line, 2 and 1, to four decimal places.

Two things to take from this. First, each weight gets its own slope, so gradient descent adjusts them in different amounts at the same time. Second, the biggest gains come early. Most of the remaining steps are spent polishing.

From three points to millions: batches

In the example, every step used all three data points to compute the gradient. That is batch gradient descent. With millions of examples, one exact gradient per step becomes far too slow.

Ruder's survey of gradient descent methods describes the three standard variants, which differ only in how much data feeds each gradient:

Mini-batches are the default in deep learning. They are cheap enough to take many steps, smooth enough to make steady progress, and they map well onto GPUs, which are built to process many examples in parallel. A little noise can even help, by shaking the weights out of shallow dips in the loss surface.

Reading is half of it. The learning rate shows this step by step in about 3 minutes. Sign up free to track what you’ve learned.

Common pitfalls

Once this loop is clear, the popular optimisers are easy to place. Momentum and Adam keep the same idea of stepping against the gradient; they just change how big each step is and in which direction, using the history of past gradients.

Questions people ask

What does the learning rate do in gradient descent?

It sets the size of each step. Multiply the gradient by the learning rate and subtract the result from the weights. Too small and training crawls; too large and each step overshoots the minimum, which can make the loss grow instead of shrink.

What is the difference between batch, stochastic and mini-batch gradient descent?

They differ in how much data is used to compute each gradient. Batch gradient descent uses the whole dataset, stochastic gradient descent uses one example, and mini-batch gradient descent uses a small group of examples, which is the usual choice in deep learning.

Why do we subtract the gradient instead of adding it?

The gradient points in the direction in which the loss rises fastest. Moving the opposite way lowers the loss, at least for a small enough step.

Sources

  1. Goodfellow, Bengio and Courville (2016), Deep Learning, Chapter 4: Numerical Computation
  2. Ruder (2016), An overview of gradient descent optimization algorithms