Gradient descent
About this lesson
Training a model means finding the weights that make the loss small. Gradient descent does it one step at a time: measure the slope of the loss where you stand, then step a little way downhill, and repeat. Watch a one-weight model learn from two examples, see why its steps shrink near the bottom, and see what a learning rate that is too small or too big does.
One knob to tune
The model guesses y from x using one number, its weight w: guess = w × x. Two training examples: x = 1 should give 2, and x = 2 should give 4. Training must find a good w. We start at w = 0.
model guess = w * x
data x = 1 -> y = 2
x = 2 -> y = 4
start w = 0 (guesses 0 and 0: both wrong)
goal find the w with the smallest lossRead the lesson step by step
One knob to tune
The model guesses y from x using one number, its weight w: guess = w × x. Two training examples: x = 1 should give 2, and x = 2 should give 4. Training must find a good w. We start at w = 0.
model guess = w * x data x = 1 -> y = 2 x = 2 -> y = 4 start w = 0 (guesses 0 and 0: both wrong) goal find the w with the smallest lossThe loss for every w
Loss is the average squared error. At w = 0 both guesses are 0, so the errors are 2 and 4: loss = (4 + 16) ÷ 2 = 10. The valley shows the loss at every w; lowest at w = 2.
loss(w) = average of (guess - y) squared w = 0: errors 0-2 = -2 and 0-4 = -4 squares 4 and 16, average 10 w = 1: loss 2.5 w = 2: loss 0 (the bottom) w = 4: loss 10 (too big: guesses 4 and 8)Feel the slope
The computer cannot see the whole valley. It can only measure the slope where it stands, called the gradient. At w = 0 the slope is −10: nudge w up by 0.01 and the loss drops by about 0.1. Downhill is to the right.
slope(w) = average of 2 * (guess - y) * x w = 0: (2*(-2)*1 + 2*(-4)*2) / 2 = -10 check: loss(0.01) = 9.90025 (10 - 0.09975) negative slope -> loss falls as w grows -> downhill is to the rightStep downhill
Gradient descent steps against the slope: new w = w − learning rate × slope. With a learning rate of 0.1: 0 − 0.1 × (−10) = 1. The loss drops from 10 to 2.5.
w = w - lr * g = 0 - 0.1 * (-10) = 1 loss(1) = 2.5 (was 10)Flatter ground, smaller step
At w = 1 the slope is only −5, so the same rule takes a smaller step: 1 − 0.1 × (−5) = 1.5, and the loss falls to 0.625. Near the bottom the ground is flatter, so the steps shrink by themselves.
w = 1: slope = -5 w = 1 - 0.1 * (-5) = 1.5 loss(1.5) = 0.625 step sizes so far: 1, then 0.5Repeat: the training loop
That is the whole loop: measure the slope, step, repeat. Two more steps reach w = 1.75, then 1.875, and the loss falls to 0.16, then 0.04. The slope shrinks toward 0 at w = 2, which fits both examples exactly.
step w loss slope used 1 1 2.5 -10 2 1.5 0.625 -5 3 1.75 0.15625 -2.5 4 1.875 0.0390625 -1.25 each step halves the distance to w = 2The learning rate
The 0.1 is the learning rate: how big a step to take for each unit of slope. People choose it before training. Start again at w = 0 with 0.01 instead: four steps only reach w ≈ 0.37. Too small means slow.
lr = 0.01, start at w = 0 step 1 w = 0.1 loss = 9.025 step 2 w = 0.195 loss = 8.145 step 3 w = 0.28525 loss = 7.351 step 4 w = 0.370988 loss = 6.634 with lr = 0.1, four steps reached 1.875Too big a step
Now try 0.4. From w = 0 the step overshoots: 0 − 0.4 × (−10) = 4, the far wall, where the loss is still 10. The slope there is +10, so it jumps straight back. It bounces for ever; anything bigger climbs out.
lr = 0.4, start at w = 0 w = 0 - 0.4 * (-10) = 4 loss 10 w = 4 - 0.4 * (10) = 0 loss 10 w: 0 -> 4 -> 0 -> 4 -> 0 ... never settles lr above 0.4: each jump lands higher (diverges)Same idea, many knobs
Real models have millions or billions of weights, so the valley has that many directions. Backpropagation finds the slope for every weight at once, and each step usually uses a small random batch of examples: stochastic gradient descent.
one weight slope = one number many weights gradient = one slope per weight every step w = w - lr * slope, for every weight backpropagation computes all the slopes at once SGD each step uses a random mini-batch
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01At the current weight the slope of the loss is −10. Which way should gradient descent move w?
A negative slope means the loss goes down as w goes up, so downhill is toward bigger w. The rule w − learning rate × slope does this automatically: subtracting a negative number adds.
02w = 1, the slope is −5 and the learning rate is 0.1. What is the new w?
new w = 1 − 0.1 × (−5) = 1 + 0.5 = 1.5.
03With the same learning rate, why do the steps get smaller as the ball nears the bottom of the valley?
Each step is learning rate × slope. Near the lowest point the valley is flatter, so the slope is closer to 0 and the step is smaller, even though the learning rate never changed.
04In the lesson, a learning rate of 0.4 made w jump 0 → 4 → 0 → 4. What does that show?
A step that is too big jumps over the valley to the other wall. Here it lands at the same height every time, so it never settles; any bigger rate would land higher each time and diverge.
05A real neural network has millions of weights. What changes in gradient descent?
The gradient is one slope per weight. Backpropagation computes all of them at once, and every weight takes its own small step downhill. Usually each step uses a small random batch of examples (stochastic gradient descent).
Glossary
- Weight (parameter)
- A number inside a model that training is allowed to change. Here the model is guess = w × x, with one weight, w.
- Loss
- One number that says how wrong the model's guesses are on the training data. Here it is the mean (average) squared error.
- Gradient (slope)
- How fast the loss changes when a weight changes a little. Its sign says which way is uphill; with many weights there is one slope per weight.
- Gradient descent
- A way to train a model: repeatedly change every weight by minus the learning rate times its slope, so the loss goes down a little each step.
- Learning rate
- The number that sets how big each step is (0.1 in the lesson). People choose it before training: too small is slow, too big overshoots.
- Training loop
- The code that repeats the steps: compute the slope, update the weights, and go round again.
- Backpropagation
- The method that computes the slope of the loss for every weight of a neural network in one backward pass.
- Stochastic gradient descent (SGD)
- Gradient descent where each step measures the slope on a small random batch of examples instead of all of them, so steps are cheaper.
Notes and sources
- For this data the loss is 2.5 × (w − 2)² and its slope is 5 × (w − 2), so with a learning rate of 0.1 each step halves the distance to w = 2. Real losses are rarely this tidy.
- A real loss surface can have many dips. Gradient descent only walks downhill from where it starts, so it can stop in a dip that is not the lowest one.
- The Python program in the lesson is real: run it and it prints w and the loss after each of the four steps (1 1.0 2.5, then 2 1.5 0.625, 3 1.75 0.15625, 4 1.875 0.0390625).
- For this valley any learning rate above 0.4 makes every jump land higher than the last. The safe range depends on how steep the loss is, which is why people try a few rates.
- Stanford CS229 Machine Learning, lecture notes (A. Ng, T. Ma): linear regression, least squares and the gradient descent (LMS) update
- Stanford CS231n, Optimization: Stochastic Gradient Descent (following the gradient, step size, mini-batches)
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), section 4.3: Gradient-Based Optimization
- Goodfellow, Bengio & Courville, Deep Learning, chapter 8: Optimization for Training Deep Models (minibatch SGD)
- Google Machine Learning Crash Course: Linear regression, gradient descent
- Google Machine Learning Crash Course: Hyperparameters (learning rate too small or too large)
- C. Lemaréchal, Cauchy and the Gradient Method, Documenta Mathematica, extra volume ISMP (2012)
- Rumelhart, Hinton & Williams, Learning representations by back-propagating errors, Nature 323 (1986)
- Robbins & Monro, A Stochastic Approximation Method, Annals of Mathematical Statistics 22 (1951)
- Python Library Reference: built-in function sum
Quiz Unlocks after the last step.