Lesson 2 · Unit 04
Linear regression
Fit a line by gradient descent and read its loss curve
What you practise
- Fit
y = wx + bby gradient descent in NumPy, the vectorized form, reading a loss curve, overfitting in one picture
Try a question from this lesson
Pick one
Hours studied and test score: (1, 52), (2, 54), (3, 56), (4, 58). Which line y = w·x + b passes through all four points?
Hint
How much does the score rise per extra hour? Then check one point to find the rest.
Correct answer
y = 2x + 50
Why
Each extra hour adds 2 points, so w = 2. At x = 1 the score is 52, so b = 50. Linear regression is finding w and b when the points do not line up so neatly.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Two knobs, two slopes
The model is a line: w is its slope, b is where it crosses x = 0. Fitting it means finding the w and b with the smallest mean squared error.
Two parameters, so the gradient has two entries. The chain rule gives both: the error, times how much the prediction reacts to that parameter.
pred = w·x + b
loss = mean((pred - y)²)
∂loss/∂w = mean(2 · (pred - y) · x)
∂loss/∂b = mean(2 · (pred - y))Concept
Reading a loss curve
Plot the loss after every step and you get the loss curve. It is the first thing to look at when training anything.
A healthy curve drops fast, then flattens: the model has converged. A curve that climbs or bounces means the learning rate is too high. A curve that barely moves means it is too low.
loss
|*
| *
| **
| ****
| ************
+----------------------- step9 more exercises in the app
- 3× type the answer
- 3× multiple choice
- 1× fill the blanks
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.