1. Both predictions are wrong — but equally wrong?
Think of a number-guessing game. Someone chooses an integer between 1 and 100. You guess 50 and hear “too low”; you guess 88 and hear “too high”. Each answer tells you which way to move, so the search becomes more focused.
Lesson 9 described training as repeatedly adjusting a network's parameters. But a network may have thousands of adjustable values and may output a whole probability distribution. What feedback tells it how to improve?
We give it a loss function: a rule that compresses how a prediction compares with the target into one numerical objective. Differentiating that objective with respect to the parameters gives a direction for updates. The optimiser and learning rate determine how to take those updates; the loss value itself is not an update step.
Start with a simpler task: predicting a continuous value. Suppose a house actually costs €300,000. Prediction A is €299,000; prediction B is €100,000. Both are wrong, but B is much further away. A useful objective must express that difference, rather than only returning “wrong”.
For easier arithmetic, measure prices in thousands of euros: the target is , and the predictions are and .
2. Square the error: mean squared error
Square each prediction error, then average over examples. This is mean squared error (MSE):
The hat marks a prediction; is the observed target. For the two house predictions:
An error 200 times as large receives a squared penalty 40,000 times as large. These numbers use our chosen price units: scaling the targets changes the numerical loss.
The gradient supplies the feedback
For one example, hold the true value fixed and differentiate the squared error with respect to the prediction:
- If the prediction is too high, this derivative is positive. A small move towards a lower prediction reduces the loss.
- If it is too low, the derivative is negative. A small move towards a higher prediction reduces the loss.
- The further the prediction is from the target, the larger the magnitude of this derivative.
For the averaged loss, each prediction's derivative has the extra factor :
The network does not usually adjust predictions directly. It adjusts parameters , so the derivative must continue through the network by the chain rule:
MSE is a common objective for regression: predicting quantities such as prices, temperatures or demand. Other regression losses are useful too; squared error can be sensitive to outliers.
3. Squared error for classification: a weak signal
Return to our fruit classifier. In a binary problem, let mean apple and let be the predicted probability of apple. Compare:
The confidently wrong prediction receives roughly 98 times the loss. But training uses gradients, not just the size of the loss.
Differentiate with respect to the probability:
At , this is ; at , it is . As the probability of the correct answer approaches zero, the magnitude approaches two rather than growing without bound.
Follow the derivative through Sigmoid
That probability derivative is only part of the picture. Our binary classifier produces a raw score, or logit, , and then applies Sigmoid:
Combining the derivatives gives:
For a true apple, , and a confidently wrong prediction with close to zero, the factor is very small. The gradient with respect to the score therefore becomes small precisely when the prediction badly needs correcting.
For example:
This is a reason cross-entropy is commonly paired with Sigmoid for binary classification. MSE can be used on probabilities, but the resulting optimisation behaviour differs.
4. Logarithmic loss: cross-entropy
Let be the probability assigned to the correct class. For a single example with a known class label, cross-entropy is:
We use the natural logarithm here. Lesson 6 used base-two logarithms to measure information in bits; changing the base scales the objective by a constant.
For a correct class probability of , the loss is approximately . At , it is approximately ; at , approximately . A tenfold reduction in probability adds to the loss. The growth is logarithmic, not exponential.
Probability gradients and score gradients are different
With respect to correct-class probability:
Its magnitude is 100 at and 1,000 at . However, this does not mean the network's parameter gradients automatically become infinitely large.
For binary targets, the complete loss is:
For the same Sigmoid score , applying the chain rule simplifies the score derivative to:
When and is close to zero, this derivative stays close to , rather than disappearing through the extra Sigmoid factor found in squared error. That is the useful comparison: cross-entropy preserves a corrective signal at the score even for a confident mistake. Parameter gradients still depend on the rest of the network.
Try it on the fruit machine
Choose whether the example really is an apple, then adjust the four features. Watch how the loss changes as the machine assigns more or less probability to that label.
Predicted probability of apple:
Probability of the true label:
Cross-entropy loss:
If the true label is apple, . If it is not apple, . Changing the true label leaves the model's prediction unchanged but changes the loss: an objective compares a prediction with a target, rather than judging a prediction in isolation.
The feature controls use the same illustrative, fixed classifier as Lesson 7. No parameters are being trained in this experiment.
More than two classes
For mutually exclusive classes, with predicted probabilities and target distribution :
For a one-hot target, the correct class has and the other entries are zero. Only the correct-class term remains:
Average this loss across the batch to obtain the training objective. If targets are soft distributions instead of one-hot labels, more than one term contributes.
Cross-entropy is a common classification objective. Softmax, in the next lesson, turns arbitrary scores into a distribution over mutually exclusive classes. Binary classification often uses Sigmoid instead. Practical implementations commonly compute the loss directly from logits using numerically stable expressions rather than separately taking the logarithm of rounded probabilities.
5. What you have discovered
A loss function makes prediction quality a numerical objective that an optimiser can work with.
- MSE averages squared errors and is common for continuous-value regression.
- Gradients describe how the objective changes. Parameter updates depend on the chain rule, the optimiser and the learning rate.
- Sigmoid plus MSE can produce a small score gradient for a confidently wrong prediction.
- Cross-entropy uses negative log probability. It rewards assigning probability to the true label and penalises confident mistakes.
- For Sigmoid plus binary cross-entropy, the score gradient simplifies to .
- The objective must fit the task, targets and outputs. No loss guarantees good generalisation or successful training on its own.
We have now connected the prediction to an objective. Next: where do class probabilities come from when the network outputs arbitrary scores?