Concept · Chapter 2: The Math Toolkit
Loss Functions
A loss function turns 'how wrong is the model?' into a single number, so that learning becomes the problem of making that number small.
The problem
'Make the model better' isn't something a computer can act on. We need a precise, differentiable measure of error.
The solution
Define a function of the model's predictions and the true answers that is zero (or minimal) when predictions are right and grows as they get worse — then minimize its average over the data.
The consequence
Every learning problem becomes an optimization problem; choosing the loss is choosing what the model will care about.
Intuition
A teacher grading homework gives one number per student. A loss function grades a model's prediction: 0 for perfect, larger for worse. Average it over the training set and you get a single score for the whole model — the thing training tries to push down.
Tiny numeric example: mean squared error
Predictions vs targets
A model predicts house prices (in [3, 3]2 - 3 = -14 - 3 = 1\big((-1)^2 + 1^2\big)/2 = 1$. Squaring makes both errors count and punishes large mistakes more.
The equations
Where it appears in AI
- Regression (predicting a number): MSE or relatives.
- Classification and next-token prediction: cross-entropy, which scores the probability given to the correct answer.
- Post-training (Chapter 10): losses built from human preferences, such as reward-model and DPO losses.
What to remember
- Loss = a number measuring how wrong a prediction is; training minimizes its average over the data.
- Regression: mean squared error, the average of (prediction − target)².
- Classification and language modeling: cross-entropy.
- The loss must be differentiable for gradient-based training.
- You get what you optimize — a poorly chosen loss yields a model good at the wrong thing.
Watch
3Blue1Brown
Gradient descent, how neural networks learn | Deep Learning Chapter 2
Connects the abstract idea of minimizing a function to how a real network learns to recognise digits.