Gradient descent, step by step with real numbers
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.
- Step 1. Slope = 2 × (0 − 3) = −6. New w = 0 − 0.1 × (−6) = 0.6.
- Step 2. Slope = 2 × (0.6 − 3) = −4.8. New w = 0.6 + 0.48 = 1.08.
- Step 3. Slope = −3.84. New w = 1.464.
- Steps 4 and 5. w = 1.771, then 2.017.
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.
- Learning rate 1.0. The error is multiplied by 1 − 2 = −1 each step. Starting at 0, w jumps to 6, then back to 0, then 6 again, forever. It never settles.
- Learning rate 1.1. The factor is −1.2, so the error grows by 20% each step while flipping sides: w goes 0, 6.6, −1.32, 8.18, −3.22. The loss climbs instead of falling. This is divergence, and in a real training run it usually shows up as the loss shooting up or turning into "NaN" (not a number).
- Learning rate 0.001. The factor is 0.998, so the error shrinks by only 0.2% a step. It works, but it takes thousands of steps to get close.
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:
- for w: (2 ÷ 3) × sum of (error × x) = (2 ÷ 3) × (−3 − 10 − 21) = (2 ÷ 3) × (−34) ≈ −22.67
- for b: (2 ÷ 3) × sum of errors = (2 ÷ 3) × (−15) = −10
Both slopes are negative, so both weights should go up. With a learning rate of 0.1:
- new w = 0 − 0.1 × (−22.67) ≈ 2.267
- new b = 0 − 0.1 × (−10) = 1.0
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:
- Batch: the whole dataset per step. Exact but slow, and it needs all the data in memory.
- Stochastic (SGD): one example per step. Very cheap per step, but noisy: the path zigzags.
- Mini-batch: a small group of examples per step. The survey notes that common sizes range between 50 and 256, though they vary by application.
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
- Unscaled features. If one input ranges from 0 to 1 and another from 0 to 100,000, the loss surface becomes a long, narrow valley. One learning rate cannot suit both directions, so training zigzags. Scaling features to similar ranges fixes much of this.
- Reading one noisy loss value. With mini-batches, the loss jumps around from step to step. Judge progress on a smoothed curve or on a validation set.
- Blaming the model when the learning rate is wrong. If the loss explodes, lower the learning rate before changing anything else. If it barely moves, try raising it.
- Expecting the global minimum. Neural network losses are not bowls. Gradient descent finds a good low point, not necessarily the lowest one, and in practice that is usually enough.
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.
See it move. Learn the ideas behind this post with animated lessons:
- Gradient descent
- The learning rate
- Derivatives and slopes
- Loss functions
- Linear regression
- Optimizers: SGD, momentum and Adam