How AI Measures Its Mistakes: Loss Functions
Loss functions are a model's pain threshold — without them, no learning.
After the forward pass (the forward computation through the network), the network has a prediction. But a prediction without a score is useless — the model cannot improve if it does not know how far off it landed.
The loss function closes this gap. It takes the model's output and the correct answer, and compresses their difference into one number. That number is the starting signal for everything that follows: gradient computation, weight updates, and ultimately, learning.
Backpropagation Algorithm
The birth of modern machine learning through an elegant training algorithm. In October 1986, David Rumelhart, Geoffrey Hinton, and Ronald Williams published the paper 'Learning representations by back-propagating errors' in Nature. This algorithm considerably changed the training of neural networks by providing an efficient method for adjusting weights in multi-layer networks. The procedure repeatedly adjusts the connection weights to minimize the difference between actual and desired output. The decisive innovation lay in the ability to train hidden layers that automatically recognize important features of the task. The mathematical foundations had already been derived earlier — by Paul Werbos (1974) and Seppo Linnainmaa (1970), among others — but it was this paper that made backpropagation widely known and convincingly demonstrated its effectiveness. Backpropagation became the workhorse of machine learning and today enables all modern deep learning applications.
Mean Squared Error: Measuring Distance in Regression
Definition
Loss Function
Analogy
Example
Definition
Why square? First: without squaring, positive and negative errors cancel each other out — an overestimate of +5 and an underestimate of -5 would average to zero error. Second: squaring amplifies large errors disproportionately — an error of 10 contributes 100 to the sum, while an error of 1 contributes only 1.
- 1. Compute ErrorsFor 5 houses: True prices (in units of €100,000) [3.0, 4.0, 5.0, 6.0, 7.0] vs. Predictions [2.5, 4.2, 5.0, 7.5, 6.0] → Errors: [-0.5, +0.2, 0.0, +1.5, -1.0]
- 2. Square Errors[-0.5, +0.2, 0.0, +1.5, -1.0] → [0.25, 0.04, 0.00, 2.25, 1.00]
- 3. Sum Squares0.25 + 0.04 + 0.00 + 2.25 + 1.00 = 3.54
- 4. Divide by nMSE = 3.54 / 5 = 0.708 — House 4 (error 1.5, squared 2.25) contributes almost 64% of the total error alone!
MSE forces the network to correct its worst predictions the most. The quadratic penalty makes large errors disproportionately expensive.
Caution
Misconception: Just Average the Raw Errors
Without squaring, errors of +1.5 and -1.0 would partially cancel out. The simple average of raw errors here is only 0.04 — suggesting a nearly perfect model, when in fact it is badly wrong on house 4. Squaring prevents this deception.
Cross-Entropy: The Price of Confident Mistakes
Definition
Cross-Entropy
Analogy
Example
Definition
Penalty difference: -log(0.99) = 0.01 vs. -log(0.01) = 4.61
460 ×
Concrete example: A spam classifier evaluates an email that is actually spam (true label = 1):
Spam Probability | Loss: -log(p) | Interpretation
0.99 | 0.01 | Correct and confident — tiny loss
0.80 | 0.22 | Correct but uncertain — small loss
0.50 | 0.69 | Coin flip — moderate loss
0.10 | 2.30 | Wrong and fairly confident — large loss
0.01 | 4.61 | Wrong and very confident — enormous lossFor problems with more than two classes, Categorical Cross-Entropy is used, typically combined with Softmax. The principle remains the same: confident wrong answers are punished disproportionately.
Interactive: How Differently Errors Are Penalised
Try it yourself: move the prediction for house 4 from the worked example and watch how the absolute and the squared error grow and what that does to the MSE of all five houses. Under “Classification” you set the spam probability of the spam email from the table: the more confidently the model is wrong, the more steeply cross-entropy climbs, while the squared error never goes above 1.
The Loss Landscape: Connecting Error to Optimisation
Task: Regression (continuous values). Penalises large errors quadratically. Smooth gradients everywhere. Ideal for: price prediction, temperature, scores.
Task: Classification (probabilities). Penalises confident errors logarithmically. Steep gradients at extremes. Ideal for: spam detection, image recognition, diagnosis.
Every possible combination of the network's weights produces a specific loss value. If you could plot all these values at once, you would see a vast mathematical landscape — a surface where horizontal dimensions represent weight values and the vertical dimension represents loss magnitude.
Imagine standing blindfolded on a mountainside. Your only sensory input is the slope under your feet (the gradient). You want to reach the valley floor (minimum loss). With each step, you feel which direction slopes downward and walk that way.
A smooth, bowl-shaped valley (a good loss landscape) makes this straightforward — every step brings you closer. A landscape with many small dips and ridges (a bad loss landscape) means you might walk into a shallow dip, feel flat ground in every direction, and stop — thinking you have arrived when the real valley is far away.
Caution
Warning: Loss = 0 Is Not a Victory
A training loss near zero only proves that the model fits the training data — perhaps because it has memorised the examples rather than learning generalisable patterns (overfitting). Whether it has really learned only shows in the loss on unseen data, the validation or test set: if that loss is clearly higher than the training loss, the model has memorised.
The loss function provides the altitude, the gradient provides the direction. In the next article, you will learn how gradient descent actually navigates this landscape.
When the output uses a function that squeezes values between 0 and 1 (the so-called sigmoid function), MSE creates flat gradient zones near 0 or 1. The model receives almost no learning signal despite being completely wrong. Cross-Entropy, on the other hand, produces steep gradients in exactly these zones — the model learns faster and more reliably. Using MSE for classification is like navigating a mountain with a nearly flat map — you can barely tell which direction is downhill.
Note
Key Takeaways
- A loss function converts prediction errors into a single number that can be differentiated with respect to the weights — this number is the only feedback the network receives about its performance.
- MSE squares errors, making large mistakes disproportionately expensive — ideal for regression. Cross-Entropy uses logarithms, making confident wrong answers catastrophically expensive — ideal for classification.
- The loss value defines a point in a vast mathematical landscape. Training means navigating this landscape toward its lowest valley — and the shape of that landscape depends heavily on which loss function you chose.
Check
Learning Goals
Can you explain this in your own words? Tick what you've got.
0 of 3 done
Check
Test Your Knowledge
What is the primary role of a loss function in a neural network?
Select one answer
1. What is the primary role of a loss function in a neural network?
2. Why does MSE square the difference between prediction and true value instead of using the absolute difference?
3. You are building a model to predict apartment rental prices (continuous values in EUR). Which loss function should you use?
4. A spam classifier assigns a probability of 0.10 to an email that is actually spam (true label = 1). Using Binary Cross-Entropy, approximate the loss for this single sample.
5. After 200 epochs, your model achieves a loss of 0.0001 on the training set but 2.8 on the validation set. A colleague celebrates the low training loss. What is the most likely problem?