Loss Functions
A loss function turns "how wrong is the model?" into a single number, so training just means making that number small. Since the data are fixed, the loss depends only on the weights, and you can picture it as a landscape over the weights whose lowest point is the best model.
What the symbols mean
- Number of examples
How many training examples the loss averages over.
- Prediction
What the model says for example i.
- Target
The right answer for example i.
What the symbols mean
- Predicted probability
The model's probability that example i is a yes (label 1).
- Label
1 for yes, 0 for no. It works as a switch that keeps only one of the two terms.
Explanation
A loss function is simply a score for how wrong a model is: feed it the model's predictions and the right answers, and out comes one number, where smaller is better. (The name comes from decision theory: it's what you lose by guessing wrong.) Mean squared error, , looks busy, but it's three small moves: take each error (prediction minus truth), square it (so misses in either direction count, and big misses count a lot), then average over the examples. Training a network sounds mysterious, but it's nothing more than adjusting the weights to make this one number small. The rest of this unit is about how to do that adjusting; this lesson is about what we're making small.
Mean squared error, and why squares. A regression model predicts a number: a house price, a temperature, tomorrow's demand. The error on example is . You can't just average the errors, because a miss of and a miss of would cancel to a perfect-looking . Squaring fixes the signs, and it does two more useful things. It punishes big misses much more than small ones: errors of and cost and , so the bigger miss is nine times as costly, not three. And it's smooth: has the derivative everywhere, with no corner, which gradient descent in the next lesson relies on. Absolute error has a sharp corner at .
There's a nice connection to statistics hiding here. Suppose a lazy model ignores its input and predicts the same number for everyone. Which gives the smallest MSE? Set the derivative to zero:
The best constant is the mean, and its MSE, , is the variance of the targets. So any model worth keeping has to beat the variance. Later, in Maximum Likelihood, you'll see that minimizing MSE is exactly the right thing to do when the noise in the data is normal.
Binary cross-entropy, for yes/no questions. A classifier answers a yes/no question (spam or not, defective or not) with a probability that the answer is yes, using the sigmoid from Activation Functions. The label is for yes and for no. Look at one example's term, . The and the act like switches. When , the second term is multiplied by and vanishes, leaving . When , the first term vanishes, leaving . Either way, the loss is of the probability the model gave to the right answer.
| probability given to the right answer | 0.99 | 0.9 | 0.5 | 0.1 | 0.01 |
|---|---|---|---|---|---|
| loss | 0.010 | 0.105 | 0.693 | 2.303 | 4.605 |
Notice how lopsided that is. Being right and confident costs almost nothing, a shrug () costs , and being confident and wrong costs a lot, growing without bound as the probability heads to . Why a log at all? The probabilities of independent examples multiply, and the log turns that product into a sum we can average; the full story is in Maximum Likelihood. The log is the natural log. Another base would only rescale the loss by a constant, which doesn't change which weights are best.
Why not just use MSE on the probabilities? Because the sigmoid flattens out. When is badly wrong (say for a true yes), is tiny, and the MSE gradient carries a factor of , so it barely pushes. Cross-entropy's cancels that flattening exactly: its derivative with respect to is simply , so the more wrong the answer, the harder the push. We'll derive that in Softmax and Logits.
The loss is a function of the weights. Here's the shift in point of view that makes training possible. Once the data are collected, every and is just a fixed number. The only things that can change are the weights, so the loss is really a function of the weights. Take the model and the four points , , and . Then
an ordinary parabola in , and the best sits at its lowest point. Add a bias, , and the loss becomes a function of two variables: a surface over the plane, just like the ones in Functions of Several Variables. A real network has millions of weights, so its loss is a "surface" over a space with millions of dimensions. You can't draw that, but everything you know about surfaces still applies: there are valleys and slopes, and there's a gradient pointing uphill. For a linear model with MSE the surface is always a bowl; for a deep network it's bumpier, with many valleys.
A common mistake. It's tempting to think of the loss as a function of the inputs , because that's how reads. During training it's the other way around: the inputs and labels are constants and the weights are the variables. That's why we take and never . We're asking what happens to the loss if we nudge a weight while the data hold still.
See it
Worked examples
A model is fit to the points , , and . Write the mean squared error as a function of , and find the that makes it smallest.
- 1
Write each prediction. The model multiplies by , so the four predictions are , , and .
A spam filter gives three emails these probabilities of being spam: for an email that is spam (), for one that isn't () and for one that is (). Find the binary cross-entropy.
- 1
For each email, find the probability the model gave to the right answer. When that's ; when it's . That's what the switches and in the formula do.
Practice
Type your answer and check it. Solve them all to complete the lesson.
A model predicts , and for three examples whose targets are , and . What is the mean squared error?
A classifier gives probability of yes to an example whose label is yes (). What is its binary cross-entropy loss? (Natural log.)
Three examples have labels and predicted probabilities of yes . Find the average binary cross-entropy.
During training, the loss of the model is treated as a function of which quantities?
A model ignores its input and predicts the same number for every example. The targets are , and . Which minimizes the mean squared error?
A model predicts house prices in thousands of dollars. On three houses it's off by , and thousand. Engineers report the root mean squared error, , because it's in the same units as the price. Find the RMSE in thousands of dollars.
Fit (no bias) to the points , and by minimizing the mean squared error. What is the best ?
A tumor classifier gives a malignant case probability of being malignant, and a different malignant case probability . How many times bigger is the first case's cross-entropy loss than the second's?
The hardest one here. Get it, and you've got this lesson.
Fit to the points , and by minimizing the mean squared error . Set both partial derivatives to zero and solve. Enter , and the minimum MSE.
Where it's used
Every trained model you've used was shaped by a loss like these. House-price and demand-forecasting models minimize MSE and usually report its square root, RMSE, because it's in the original units. Spam filters, medical screening models and fraud detectors minimize binary cross-entropy. Large language models are trained by minimizing the cross-entropy of the next word, averaged over trillions of words, and the "loss curve" engineers watch for weeks during training is exactly this one number, slowly falling.