Back to Learning Area

How AI Measures Its Mistakes: Loss Functions

Loss functions are a model's pain threshold — without them, no learning.

  • Fundamentals
  • 11 min
  • Intermediate

Plain Language

This preview is in Plain Language. The whole portal is also available in Plain Language. You can change the language at the top. Or use the button below. Our Plain Language texts are AI-assisted and editorially reviewed. They follow the core principles of easy-to-read language, but they are not certified Easy Read: a review panel of people with learning difficulties does not test them.

Preview: How AI measures its errors

What is a loss function?

An AI makes predictions. Sometimes these are wrong. A loss function measures the error. Loss function means: a measure for the error. A small loss means good predictions.

Two important loss types

There are two important loss types. MSE is for predicting numbers. MSE means: mean squared error. Cross-entropy is for yes/no decisions. Cross-entropy means: wrong certainties cost a lot.

What is the loss for?

The network minimizes the loss while learning. A small loss means: the network is right. A loss of zero is not a good sign. The network has memorized it then.

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.

Mean Squared Error: Measuring Distance in Regression

Definition

Loss Function

Analogy

Imagine scoring archers by how far each arrow lands from the bullseye. Strategy A (absolute distance): 2 cm off scores 2, 10 cm off scores 10. Strategy B (squared distance): 2 cm off scores 4, but 10 cm off scores 100. Under Strategy B, one terrible shot dominates the total score. MSE uses Strategy B — the biggest mistakes get corrected the most.

Example

In practice, you sometimes do NOT want outlier sensitivity. Alternatives like Mean Absolute Error or Huber Loss exist for such cases — MSE's outlier amplification is a deliberate trade-off.
MSE = (1/n) · ∑(ypred − ytrue)²
Mean Squared Error (MSE)

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. 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. 2. Square Errors[-0.5, +0.2, 0.0, +1.5, -1.0] → [0.25, 0.04, 0.00, 2.25, 1.00]
  3. 3. Sum Squares0.25 + 0.04 + 0.00 + 2.25 + 1.00 = 3.54
  4. 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

Imagine a weather forecaster who publicly announces confidence levels. On a day that actually rains: "90% chance of rain" — barely affects reputation. "10% chance of rain" when it pours — embarrassing, noticeable damage. "1% chance of rain" during a storm — career-ending, catastrophic damage. The logarithm in Cross-Entropy works the same way: the penalty does not grow linearly, it accelerates.

Example

Real forecasters face social dynamics, not pure log-loss. Also, Cross-Entropy assumes the true labels are correct — mislabelled training data can cause the model to be unfairly penalised.
L = −(1/n) · ∑[y · log(p) + (1−y) · log(1−p)]
Binary Cross-Entropy

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 loss

For 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

Mean Squared Error (MSE)

Task: Regression (continuous values). Penalises large errors quadratically. Smooth gradients everywhere. Ideal for: price prediction, temperature, scores.

Cross-Entropy (CE)

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

Not completed

What is the primary role of a loss function in a neural network?

Select one answer

Answer Key: 1) B · 2) B · 3) C · 4) C · 5) B