Lesson 03
Gradient Descent
Linear regression on study hours and marks: a wrong prediction first, then one numeric update of the weight m and the intercept c.
What You Will Learn
Click a topic to jump to that section.
- The one algorithm
- A farmer and a well
- A prediction that is badly wrong
- How m and c change
- One update with real numbers
- What changed inside the model
- The learning cycle
- Five steps on the same student
The one algorithm
We will use only linear regression. No other algorithm on this page.
A teacher wants to predict marks from study hours. Historical rows:
| Study hours (X) | Actual marks (Y) |
|---|---|
| 1 | 30 |
| 2 | 40 |
| 3 | 50 |
| 4 | 60 |
| 5 | 70 |
The model is a straight line:
predicted marks = m × hours + c
| Symbol | Plain meaning |
|---|---|
| X | Study hours |
| Y | Actual marks |
| m | Weight. Marks added for each extra hour (the slope). |
| c | Intercept. The mark the line gives at 0 hours. |
Look at the table. Each extra hour adds about 10 marks, and the line does not start at 0: one hour is already 30, so the honest line is about m = 10 and c = 20 (10 × hours + 20). The model does not know that yet. Gradient descent is how it finds m and c.
A farmer and a well
A farmer is standing in his field. The drinking well is in the lowest spot. He cannot see the whole field. It is evening, and the ground only tells him what is under his feet.
He does three things, again and again:
- He feels which way the ground slopes down.
- He takes one small step that way. Not a jump.
- He stops and feels the ground again.
When the ground is flat, he is at the well. He stops.
That walk is gradient descent.
| In the field | In the marks example below |
|---|---|
| How high he is standing | How wrong the prediction is (the loss) |
| Where his feet are | The current numbers m and c |
| One small step | One update of m and c |
| The well | The smallest loss this line can reach |
Two easy mistakes:
- Steps too small. He does reach the well, but it takes the whole night. In the model, the loss falls very slowly.
- Steps too big. He strides past the well and starts climbing the other side. In the model, the loss can jump back up.
The rest of this page is the same idea with study hours and marks. The farmer is choosing the next step. The model is choosing the next m and c.
A prediction that is badly wrong
The model starts knowing nothing:
m = 0, c = 0
So the prediction is 0 × hours + 0 = 0 for every student.
Take the student who studied 2 hours. Actual marks = 40. Predicted marks = 0. The miss is 40 marks.
That is the whole problem. The line shape is fine. m and c are the wrong numbers.
How m and c change
Gradient descent asks: if I nudge m a little, does the loss go up or down? Same question for c. Those answers are the gradients.
new m = old m − α × (how loss changes with m)
new c = old c − α × (how loss changes with c)
| Symbol | Meaning |
|---|---|
| m | Weight (slope) |
| c | Intercept |
| L | Loss. Here, squared miss: (prediction − actual)² |
| α | Learning rate. How big one step is. We will use 0.01. |
| change in L when m moves | Written ∂L/∂m |
| change in L when c moves | Written ∂L/∂c |
For this squared loss and one row, the slopes are:
∂L/∂m = 2 × (prediction − actual) × hours
∂L/∂c = 2 × (prediction − actual)
You do not need to derive those yet. Use them as the “which way is downhill” reading, then watch the arithmetic.
One update with real numbers
One row only, so every number stays visible: hours = 2, actual = 40, m = 0, c = 0, α = 0.01.
Prediction = 0 × 2 + 0 = 0
∂L/∂m = 2 × (0 − 40) × 2 = −160
∂L/∂c = 2 × (0 − 40) = −80
m = 0 − (0.01 × −160) = 1.6
c = 0 − (0.01 × −80) = 0.8
The minus in front of α is the whole trick. The gradients are negative, so subtracting them increases m and c. The line starts to rise with hours.
Same step in a short notebook cell:
m = 0
c = 0
hours = 2
actual = 40
alpha = 0.01
prediction = m * hours + c
grad_m = 2 * (prediction - actual) * hours
grad_c = 2 * (prediction - actual)
m = m - alpha * grad_m
c = c - alpha * grad_c
print(m, c, m * hours + c)1.6 0.8 4.0What changed inside the model
| Before | After one update | |
|---|---|---|
| Weight m | 0 | 1.6 |
| Intercept c | 0 | 0.8 |
| Prediction for 2 hours | 0 | 1.6 × 2 + 0.8 = 4 |
| Actual marks | 40 | 40 |
The miss shrank from 40 marks to 36 marks. Still far from 40. The model is not finished. It will run the same update again, with the new m and c.
The learning cycle
- Start with m = 0, c = 0.
- Predict: m × hours + c.
- Measure the miss: prediction versus actual marks.
- Read the two slopes: ∂L/∂m and ∂L/∂c.
- Update: m = m − α × slope for m, and the same for c.
- Repeat until the loss is small enough, or you stop.
Five steps on the same student
Same row (2 hours, 40 marks) and the same α = 0.01. Each row below is the model after that update.
| Step | m | c | Prediction for 2 hours | Squared loss |
|---|---|---|---|---|
| start | 0 | 0 | 0 | 1600 |
| 1 | 1.60 | 0.80 | 4.0 | 1296 |
| 2 | 3.04 | 1.52 | 7.6 | 1050 |
| 3 | 4.34 | 2.17 | 10.8 | 850 |
| 4 | 5.50 | 2.75 | 13.8 | 689 |
| 5 | 6.55 | 3.28 | 16.4 | 558 |
Loss keeps falling. Prediction climbs 0 → 4 → 7.6 → … toward 40. It is still not 40 after five steps, and m is not 10 yet. That is normal: α is small, and this walk used one student. A full training pass repeats the same idea on every row in the table. The line that fits all five rows is still about m = 10 and c = 20.
The farmer keeps stepping downhill until he reaches the well. The model keeps stepping m and c until the prediction is less wrong.
Check your understanding
Gradient Descent Quiz
What does the loss function measure?
1 / 10