1 km32 km33 km84 km6guessguessguessguessw × kmw2errorerrorerrorerrorp=0.90.11p=0.12.30error²error²error²error²mean of err²loss?1w = 22total = 03for x, y in data:4 guess = w * x5 err = y - guess6 total += err ** 27loss = total / len(data)w = 1w = 2w = 38.52.511.5
Rows: real times · guesses · errors · squared errors (one column per delivery)Teal block: the model, guess = w × km · gold block: the lossChips: numbers moving from row to row · bars: the loss of each modelFour rows of cells, back to front: the real delivery times, the model's guesses, the errors and the squared errors. On the right, the model holds its one number w, and the loss block holds the result: one number for how wrong the guesses are. Plain errors cancel to 0; squared errors cannot. Their mean, 2.5, is the loss. The bars compare three values of w, and the training loop nudges w until the loss is as low as it can go for this data. The four deliveries are made up, small numbers chosen so the arithmetic is easy to follow. Real models learn from thousands of examples and many numbers, not one.

Measuring mistakes

  1. A model makes guesses, and some are wrong. To improve it, we first need one number that says how wrong it is overall. That number is the loss, also called the cost. Smaller loss means smaller mistakes.

Building the loss

Using the loss

What happens

;; step 1/10 · mistakes
model    makes a guess for each example
loss     one number: how wrong the guesses are, overall

loss big    -> the guesses are far off
loss small  -> the guesses are close
loss 0      -> every guess is exactly right

The four deliveries are made up, small numbers chosen so the arithmetic is easy to follow. Real models learn from thousands of examples and many numbers, not one. The mean squared error here is computed exactly as written: add the squared errors, divide by how many there are. Some textbooks (such as Stanford CS229) put ½ in front instead; that changes the number, not which model is best.