Professor Turing

Study guides

What is gradient descent?

Last updated: 2026-11-25

Gradient descent is a way to find the input that makes a function as small as possible, by repeatedly stepping a little way downhill. At the current value you work out the slope, then move in the opposite direction, by the slope times a small number called the learning rate. Repeat until the steps become tiny. In machine learning the function is the loss, a number for how wrong the model is, and the inputs are the model's weights.

Start learning No account needed

It can fit the line in linear regression, and in a neural network it uses the gradients that backpropagation computes.

1. The steps, in plain words

Pick a starting value for the weight. Compute the gradient, which for one weight is the slope of the loss at that point: positive means the loss rises as the weight rises, negative means it falls. Subtract the learning rate times the gradient from the weight. Because you subtract, a positive slope moves the weight down and a negative slope moves it up, so in both cases a small enough step moves you toward lower loss. Compute the loss and the gradient again at the new value and repeat. The gradient shrinks as you near a minimum, so the steps shrink with it, without any rule saying so.

If the idea of a loss is new, can you learn statistics on your own covers fitting a line to data, which is the problem the example in section 3 solves.

2. A worked example: one weight, one hump

Take the loss f(w) = (w − 3)2. It is lowest at w = 3, where it is 0, and we pretend not to know that. Its slope is 2(w − 3). Start at w = 0 with a learning rate of 0.1, so each update is w ← w − 0.1 × 2(w − 3).

StepwLossGradient
009−6
10.65.76−4.8
21.083.6864−3.84
31.4642.3593−3.072
41.77121.50995−2.4576
52.016960.9664−1.9661

You can check the first step by hand. The slope at 0 is 2 × (0 − 3) = −6, so w becomes 0 − 0.1 × (−6) = 0.6. The loss falls from 9 to 5.76. At 0.6 the slope is −4.8, so the next move is 0.48 and w becomes 1.08.

The table has a pattern. Each step keeps 0.8 of the gap to 3, because 1 − 0.1 × 2 = 0.8. After 5 steps the gap is 0.98304, which matches 3 − 2.01696. That also tells you how long it takes: w gets within 0.01 of 3 after 26 steps. The loss keeps falling and the steps keep shrinking, but at this learning rate the weight is still moving after the fifth step.

Output from the script: step 1: w = 0.60000, loss = 5.76000, gradient = -4.8000 and steps until w is within 0.01 of 3: 26. They come from a short script, scripts/what-is-gradient-descent-worked-example.py in the Professor Turing repository, that does the same arithmetic.

3. The learning rate: too small, about right, too large

The same problem with other learning rates, six steps from w = 0, shows why the setting matters.

Learning ratew after steps 1 to 6
0.010.060, 0.119, 0.176, 0.233, 0.288, 0.342
0.10.6, 1.08, 1.464, 1.7712, 2.01696, then on toward 3
0.53, 3, 3, 3, 3, 3
16, 0, 6, 0, 6, 0
1.16.6, −1.32, 8.184, −3.221, 10.465, −5.958

At 0.01 the weight is headed the right way but has covered little ground after six steps, at 0.342. At 0.5 it lands exactly on 3 in one step, which is a coincidence of this curve and not something you can count on in general. At 1 it jumps from 0 to 6 and back, and the loss does not improve. At 1.1 each step overshoots by more than the one before, because the gap is multiplied by 1 − 1.1 × 2 = −1.2 each time, so the miss grows from 3 to 3.6 to 4.32 and the loss gets worse. A learning rate that is too large does not just slow gradient descent down, it can make it diverge.

4. Fitting a line with it

Now a small fitting problem. The data are (1, 2), (2, 4) and (3, 6), and the model is y = w × x. For the loss, take half the sum of squared errors, so L(w) = ½ × ((w − 2)2 + (2w − 4)2 + (3w − 6)2). Its gradient is the sum of (w x − y) x over the three points, which works out to 14w − 28, because 1 + 4 + 9 = 14 and 1 × 2 + 2 × 4 + 3 × 6 = 28. The loss is lowest at w = 2, and we again pretend not to know that.

StepwLossGradient
0028−28
11.42.52−8.4
21.820.2268−2.52
31.9460.0204−0.756
41.98380.0018

The learning rate is 0.05. From w = 0 the first update is 0 − 0.05 × (−28) = 1.4. Each step keeps 0.3 of the gap to w = 2, since 1 − 0.05 × 14 = 0.3, and the loss falls by a factor of 11.1 each step, which is 1 / 0.3 squared. In what is linear regression the slope comes from a formula in one go; here we reached nearly the same weight by walking, which is the route you take when a model has too many weights for a formula.

5. A common misconception: it finds the lowest point in one go

It is an iterative search, not a formula. The table shows it getting closer by a fixed fraction each step, and it only stops because you decide when to stop. It also uses only the slope where it stands. Both curves above have a single dip, so a start combined with a small enough learning rate heads to the same bottom, but a loss with several dips can leave it in one that is not the lowest. Neither example here has more than one dip, so they do not show that case. In a neural network the loss is not a single clean hump, which is one reason the learning rate and starting point need care.

A second slip is to read the gradient as the step. The step is the gradient times the learning rate. At step 0 in section 2 the gradient is −6 and the step is 0.6.

6. Test yourself

Close this page and write the update rule from memory. Then take f(w) = w2, whose slope is 2w, start at w = 4 with a learning rate of 0.25, and work out w after three steps. Reopen the page and compare with the script, which prints 4, 2, 1, 0.5. If your second value is wrong, check the sign of the subtraction first. For the general method, see how to test yourself.

What the sources found, and their limits

Dive into Deep Learning, section on gradient descent. A course text that shows a learning rate that is too small moving slowly, a rate of 1.1 overshooting and diverging on a one-dip curve, and a curve with many local minima where the result depends on the learning rate. Limits: its examples use different functions from this page. Source: Dive into Deep Learning, Gradient Descent, read on 2026-11-24.

The curves, data and figures on this page are made up, and the arithmetic is our own.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start. It's an AI tutor, free to try.

Bring a topic you are studying, such as how the learning rate changes the steps. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through.

It cannot see handwriting or photos, so type your steps, and the explanation has to be yours. You must be 18 or older. Guest conversations are deleted 7 days after your last activity; with an account they are kept while the account is active.

FAQ

Does gradient descent find the lowest point? Not in general. It follows the slope downhill from where it starts. On the one-dip curve in our example a small enough learning rate heads to the bottom from either side, but loss curves with several dips can trap it in a dip that is not the lowest. The example here does not show that case.

How do I choose the learning rate? Try a value, watch the loss, and adjust. In our example 0.1 needs 26 steps to get within 0.01 of the bottom, 0.5 lands on it in one step, and 1.1 makes each step overshoot by more than the one before.

Start with what you're working on

Tell the tutor which topic you are studying and let it ask you the questions.

Start learning No account needed

See also: all study guides