What Learning Means
The cost function
Turning 'wrong' into a number to minimize
A model can't fix a mistake it can't measure. The cost function is the measurement: feed it the model's current settings and it hands back a single number - how wrong you are, all in. Big number, bad model. Small number, good model. Every training run in machine learning, from a two-parameter line to a trillion-parameter language model, is nothing more than the hunt for the settings that drive this one number as low as it will go.
The idea in one line
Mean squared error: the workhorse score
Take our line from last lesson, . For a data point the model predicts , and it misses by the error . The most common way to turn a whole dataset of misses into one score is mean squared error - square each miss, then average:
Mean squared error (MSE)
Two words that get used loosely but mean different things: the loss is the error on a single example, ; the cost is the average loss over all examples. Loss is per-point, cost is the whole-batch verdict - and depends only on the parameters and , since the data is fixed.
Steer the line, watch the score
Here are two views of the same two numbers. On the left, the line sitting over five data points, with each error drawn as a literal square - its area is that point's squared loss. On the right, the whole cost landscape: every pair is a spot on the map, and its height (shown as rings and shading) is the cost there. The glowing dot is you. Drag it across the landscape - or nudge the sliders - and feel the line and the squares respond in lockstep. Your job: steer to the bottom of the bowl, the center of the rings.
The fit line + squared errors
The cost surface contours over (w, b)
Steer the dot toward the center of the rings to shrink the cost.
Why square the error, and not just add it up?
Parameters are coordinates; cost is the height
The right-hand panel is the whole point. Stop thinking of and as knobs and start thinking of them as coordinates: the pair is an address on a map, and the cost is the altitude at that address. Plot the altitude everywhere and you get a surface - for mean squared error it's a bowl (convex, one lowest point), which is why the contour rings close neatly around a single ★. Notice the rings are stretched into a tilted valley: this dataset has -values that couple and , so the cheapest way to raise the slope is to lower the intercept. Real models have millions of coordinates instead of two, but the picture is identical - and gradient descent is simply the act of walking downhill on this surface until the altitude stops dropping.
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 6, 7, 10, 10])
def cost(w, b):
y_hat = w * x + b # the model's predictions
errors = y_hat - y # how far off each point is
return np.mean(errors ** 2) # square, then average = the cost
print(cost(1.5, 0.0)) # 7.55 - a so-so line
print(cost(2.0, 1.0)) # 0.80 - the best fit, bottom of the bowlCheck yourself
In MSE, if one example's error doubles, its contribution to the score becomes:
On the cost surface, a single point corresponds to:
Recall: how do loss and cost differ, and what is the MSE formula?
Pin down the per-point vs whole-batch distinction and the formula. Try to state it, then check.
Primary source
Ask your teacher