Gradient descent
On this page
Walk down a hill in thick fog. You cannot see the valley, but you can feel which way the ground slopes under your feet, so you take a step downhill and feel again. Gradient descent is that method used on a model: measure how wrong the model is, find which way the error slopes, and change the model one small step in the downhill direction.
See it
A ball takes steps down a curve toward the lowest point. With a step size of 0.10 it needs 26 steps to arrive. You make the step size 12 times larger, 1.20. What happens?
The curve is the loss for every value of w. The ball knows only the slope where it stands. The small dots are the places it has been. Moving the slider puts the ball back at the start, so you can compare step sizes from the same point.
The idea
A model fitted to data has numbers inside it that can be tuned. Call one of them w. The loss is one number that says how wrong the model is on its data: large when the predictions are far from the measurements, zero when they match. Training means finding the w with the lowest loss.
The gradient is the slope of the loss at the present w. A positive gradient means the loss rises as w rises, so w must go down. The learning rate is the size of the step.
the minus sign sends w downhill, against the slope
The step is the learning rate times the gradient, so the steps get shorter as the curve flattens near the bottom. The step sizes at which this curve stops settling (1.00 and above) belong to this curve only. The lesson on how models learn finds a limit of about 0.03 for its data. A real model has thousands of such numbers and may have more than one valley.
One worked number
The loss of a model is (w − 3)2, and its gradient is 2 × (w − 3). The model starts at w = 0 with a learning rate of 0.10. Where is w after one step, and what happened to the loss?
Show the worked solution, 3 steps
- Find the gradient at the start
Gradient = 2 × (0 − 3) = −6. It is negative, so the loss falls as w rises.
- Take the step
wnew = 0 − 0.10 × (−6) = 0 + 0.60 = 0.60.
- Compare the loss
Before: (0 − 3)2 = 9. After: (0.60 − 3)2 = 5.76. The loss fell. In the figure, press the button for one step and read the same two values.
The mistake to avoid
A second trap: a loss that has almost stopped falling does not prove the model is at the bottom. With a very small learning rate, the loss also changes very little on each step, far from the bottom.
Check yourself
The loss is (w − 3)2 and its gradient is 2 × (w − 3). The model is at w = 5 and the learning rate is 0.25. Where is w after one step?
A training run prints the loss after each step: 9.0, 17.6, 34.6, 67.8. What should you change first?
With a learning rate of 0.01, the loss falls from 9.00 to 8.64 on the first step and keeps falling by a little less on each step. After 20 steps a colleague says: the loss hardly moves now, so training is finished. Is that right?
Where you meet it
- A model of the energy use of a buildingThe numbers that link weather and occupancy to kWh are found by gradient descent on past meter readings.
- A vibration monitor on a motorThe model that knows the normal vibration was trained this way, on recordings of the healthy machine.
- A controller that corrects an errorA correction in proportion to the error is the same idea, and too large a gain makes the loop swing.
- The loss is one number for how wrong the model is. Training looks for the lowest loss.
- New w = old w − learning rate × gradient, repeated many times.
- Too small a learning rate crawls. Too large a learning rate makes the loss grow.
- The method feels only the slope where it stands. It does not know where the bottom is.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.