Bitpath

Lesson 2 · Unit 04

Linear regression

Fit a line by gradient descent and read its loss curve

12 exercisesabout 3 min

What you practise

  • Fit y = wx + b by 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.

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
 |*
 | *
 |  **
 |    ****
 |        ************
 +----------------------- step

9 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.

One email when Bitpath is ready to try. Nothing else. Privacy