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 and 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:
L is a table of losses with one row per and one column per (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 at 20.4 and change only :
The loss falls, bottoms out near , 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 , each of which is a straight-line function of and , so the loss is a quadratic in and (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:
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, and , 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 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.
The rings are ellipses, all centred on the bottom (×). They're long and tilted: you can trade against 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:
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
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.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 4: Numerical Computation: minimising a function; why a quadratic like this one has a single minimum (4.3)
- matplotlib: contour: drawing contour lines of a grid of values
- NumPy: numpy.unravel_index: turning argmin’s flat position into a row and column