Skip to content
You Start with one site
Amounts in Start with one site
Machine Learning · Concept

Gradient descent

One idea Beginner 5 min read
Found this useful?

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.

Very small stepsAlways downhill, and it takes all day.It crawls
Medium stepsEach step is shorter than the one before, because the slope gets flatter.It settles
Very large stepsEach step crosses the valley and lands higher on the other side.It jumps out

See it

Predict first

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?

See it: steps downhill on a loss curve

Steps taken
Value of w
Loss

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.

wnew = wold − learning rate × gradient

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 question

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
  1. Find the gradient at the start

    Gradient = 2 × (0 − 3) = −6. It is negative, so the loss falls as w rises.

  2. Take the step

    wnew = 0 − 0.10 × (−6) = 0 + 0.60 = 0.60.

  3. 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 larger learning rate is not faster learning. Up to a point, larger steps do arrive sooner. Past that point each step crosses the bottom and lands higher on the other side, and the loss grows. When a loss rises during training, the first thing to try is a smaller learning rate.

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

Question 1

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?

Question 2

A training run prints the loss after each step: 9.0, 17.6, 34.6, 67.8. What should you change first?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.