Lab: train a model yourself

Put the whole section together. Build a dataset, train linear regression with gradient descent, debug the learning rate, then take the section quiz.

Lab · 30 min

In this lesson you will

  • Train a linear regression model end to end with gradient descent
  • Diagnose training from the path on the contour map and the loss curve
  • Explain how the data, the starting point and α affect the result
  • Check your understanding of the whole section with the section quiz

This is where it all comes together. Below is a complete, if tiny, machine learning system: a dataset, a model fw,b(x)=wx+bf_{w,b}(x) = wx + b, a cost J(w,b)J(w,b) and an optimiser, gradient descent. Everything a large model does, this does too, just with two parameters instead of billions.

How the lab works

  • Left, the data and the model. Pick a preset dataset, or tap empty space to add a house and tap a point to remove it (the buttons do the same from the keyboard). The blue line is the current model.
  • Middle, the contour map of J(w,b)J(w, b). The trail shows every step gradient descent has taken. Click the map to choose a different starting point.
  • Right, the loss curve. The cost after every iteration.
  • Controls. Set the start values and α\alpha, then Step one iteration at a time or Run. Reset goes back to the start. Show the answer draws the exact best line (computed directly with a formula) so you can check what gradient descent found.

Loading the interactive graphic…

This lab is a complete learning system: data, a line, a cost, and gradient descent.

Challenges

The lab ticks these off as you complete them:

  1. Train to J<0.1J < 0.1 on the House prices preset. Start from w=0w = 0, b=0b = 0 with α=0.1\alpha = 0.1 and press Run. Compare your line with Show the answer.
  2. Find a learning rate that diverges. Raise α\alpha a little at a time. What does the path on the contour map look like just before it blows up, and just after?
  3. Converge in under 100 iterations. You’ll need a well-chosen α\alpha, and maybe a better starting point.
  4. See the effect of an outlier. Load the One outlier preset and train. One badly priced house drags the whole line toward it, because squaring makes its large error count a lot. Remove it and train again.
  5. Fit your own data. Add at least three houses of your own and train a model on them.

Questions to think about

  • Starting points: does gradient descent always reach the same line, wherever you start? (For this bowl-shaped cost, it should. Why?)
  • Why does the loss curve drop steeply at first and then flatten?
  • On the Flat — no trend preset, what value does ww end up near, and what does that mean about how price depends on size in that data?

Section 1 quiz

You’ve finished the first section. This quiz covers everything from what machine learning is to choosing a learning rate; each answer comes with an explanation.

Self-check · 10 questions

Section 1 quiz: regression and gradient descent

1. Which problem is the best candidate for machine learning?
2. A hospital has thousands of scans, each labelled by doctors as healthy or not. A model learns to label new scans. This is…
3. Which of these is an unsupervised learning task?
4. For fw,b(x)=1.5x+1f_{w,b}(x) = 1.5x + 1, what is y^\hat{y} when x=2x = 2?
5. Two training examples: (1,2)(1, 2) and (2,2)(2, 2). For the model w=1,b=0w = 1, b = 0, what is J(w,b)J(w,b)?
6. A point on the contour map of J(w,b)J(w,b) lies on the smallest ring around the centre. What does that tell you about its line?
7. What does the learning rate α\alpha control in gradient descent?
8. At the minimum of JJ, what is the slope ∂J∂w\frac{\partial J}{\partial w}, and what does gradient descent do there?
9. You train with α=1.0\alpha = 1.0 and the cost goes 5, 20, 80, 320… What should you do first?
10. Why is batch gradient descent called batch?