Linear models · lesson 2 of 12
Parts in this lesson
Anp.linspace(40, 64, 241)evenly spaced values to try
Bnp.unravel_index(L.argmin(), L.shape)the row and column of the smallest value
Cax.contour(ws, bs, L, levels=[...])a contour plot

The loss landscape

Last updated

The loss gives every possible line a score. Draw that score for every line at once and you get a landscape over the two numbers w and b: a smooth bowl, with the best line at the bottom. Seeing training as finding the bottom of a bowl is the picture behind everything from here to the largest neural networks.

By the end of this lesson you can

  • Compute the loss for a grid of w and b values, and find the lowest point
  • Read a contour plot of a loss, and link a point on it to a line through the data
  • Explain why the squared-error loss for a line is a bowl with a single bottom

Loss for every (w, b)

Lesson 3.1 tried lines one at a time. A computer can try thousands. Take 241 slopes from 40 to 64 and 161 intercepts from −20 to 60, and work out the loss of every one of the 38,801 lines:

landscape.ipynb

L is a table of losses with one row per b and one column per w (lesson 0.3's 2-D arrays). The double list comprehension is slow for big grids, but clear: each entry is loss(w, b) for one line.

The bowl shape

Fix b at 20.4 and change only w:

landscape.ipynb

The loss falls, bottoms out near w=52, and rises again, faster the further you go: 2 pesos per kilogram too low costs 107 more; 4 too high costs 551 more. That's a parabola, and no accident. The loss is a sum of squares of wxi+b−yi, each of which is a straight-line function of w and b, so the loss is a quadratic in w and b (lesson 1.1).

Over both numbers at once, it's a bowl. A quadratic like this one, a sum of squares, has exactly one lowest point and no other dips to get stuck in. That's what makes linear regression easy, and many later models harder: their landscapes aren't bowls.

The bottom is the best line

The lowest point of the grid:

landscape.ipynb

L.argmin() finds the smallest value's position as if the table were one long row; np.unravel_index turns that into a row and column. The answer, w=51.8 and b=20.5, with a loss of 19.05, is within the grid's spacing of lesson 3.1's best line (51.84 and 20.40, loss 19.03). Losses on the grid go up to 10,994 at the corners.

Searching a grid works for two numbers, but it doesn't scale: with 10 numbers and 100 values each it would need 10010 losses. Lesson 3.3 finds the bottom by walking downhill instead.

Contours

A landscape over two numbers can be drawn from above as a contour plot, like a hiking map: each ring joins the points (here, the lines) with the same loss.

landscape.ipynb

The rings are ellipses, all centred on the bottom (×). They're long and tilted: you can trade w against b along the long direction, raising the price per kilogram and lowering the fee, without the loss changing much. Across the short direction the loss climbs steeply.

Every point on the map is a line through the data. Click the map and see which:

Bowl and line, linkedclick the map to choose a line
×3040506070-4004080w (pesos per kg)b (pesos)pricekg

w = 45, b = 50: price = 45 × kg + 50, with loss 412.5. The bottom, marked ×, is 19.03.

Points on the same ring are different lines that fit equally badly. Points near × are lines close to the best; points far out on the map are lines that miss the receipts by a lot.

Check what you learned

What does one point on the loss contour map stand for?
Predict, then check with the figure: you click two points on the same contour ring. What do their lines have in common?
Why does the squared-error loss for a line have only one lowest point?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning