Foundations02 of 21· 7 min read

Loss Function — Explained Simply

What a Loss Function Actually Is

Most explanations say "the loss function measures how wrong your model is." That is true but it does not tell you anything useful. Here is the real explanation.

You Are a Teacher Marking Exam Papers

You have 30 students. They all just wrote a maths exam. Your job is to mark their papers and give each one a score that honestly reflects how well they understood the material.

But here is the thing — there are two very different ways you could mark:

Marking Style 1 — The Lenient Teacher A student writes 42 when the answer is 40. You say "close enough, full marks." A student writes 95 when the answer is 40. You say "well they tried, half marks."

Marking Style 2 — The Honest Teacher A student writes 42 when the answer is 40. Small penalty — they were close. A student writes 95 when the answer is 40. Huge penalty — they were wildly wrong.

The honest teacher's marking system is a loss function. It gives a small number when predictions are close to correct and a large number when predictions are far off. The model uses that number to understand how much it needs to improve.

The Three Things a Loss Function Must Do

1. Return ZERO when predictions are perfect
2. Return a SMALL number when predictions are close
3. Return a LARGE number when predictions are far off

That is the entire job description of a loss function. Any formula that satisfies those three rules can be a loss function.

The Simplest Loss Function — Mean Squared Error

Imagine your network is predicting lemonade sales:

Day 1:  Predicted 45,  Actual 48  → error = 3
Day 2:  Predicted 20,  Actual 30  → error = 10
Day 3:  Predicted 52,  Actual 50  → error = 2
Day 4:  Predicted 10,  Actual 40  → error = 30

You could just average those errors — but positive and negative errors would cancel each other out. So instead you square each error first — which makes all errors positive and punishes big mistakes more than small ones:

Day 1:  error = 3   → squared = 9
Day 2:  error = 10  → squared = 100
Day 3:  error = 2   → squared = 4
Day 4:  error = 30  → squared = 900

Then take the average:

MSE = (9 + 100 + 4 + 900) / 4 = 253.25

That number — 253.25 — is your loss. After thousands of training steps the loss gets closer and closer to zero.

import torch
import torch.nn as nn

predictions = torch.tensor([45.0, 20.0, 52.0, 10.0])
actuals     = torch.tensor([48.0, 30.0, 50.0, 40.0])

loss_fn = nn.MSELoss()
loss    = loss_fn(predictions, actuals)
print(loss)   # tensor(253.2500)

The Problem With MSE for Classification

MSE works beautifully when you are predicting numbers — lemonade sales, house prices, temperature tomorrow.

But what if you are predicting a category — is this review positive or negative? Is this email spam or not?

Imagine a student taking a true/false exam. The correct answer is True.

  • Student A writes True — completely correct
  • Student B writes False — completely wrong

MSE would say Student B's error is 1 — just one unit off. But that is misleading. In classification there is no "almost right." Either you said the right class or you did not.

You need a loss function that thinks differently about classification mistakes. That function is called Cross-Entropy Loss.

Cross-Entropy Loss — The Loss Function for Classification

Instead of measuring distance from the right answer, cross-entropy measures confidence.

The model does not just predict a class — it predicts a probability for each class:

Review: "This product is amazing, I love it!"

Model output:
  Positive: 0.85   ← 85% confident it is positive
  Negative: 0.10   ← 10% confident it is negative
  Neutral:  0.05   ← 5% confident it is neutral

The true label is Positive. Cross-entropy loss asks: how confident were you in the correct answer?

  • If you said 0.85 for the correct class → small loss — confident and right
  • If you said 0.10 for the correct class → large loss — unconfident about right answer
  • If you said 0.01 for the correct class → huge loss — almost certain it was wrong
predictions = torch.tensor([[0.85, 0.10, 0.05]])  # Model's probabilities
true_label  = torch.tensor([0])                    # 0 = Positive class

loss_fn = nn.CrossEntropyLoss()
loss    = loss_fn(predictions, true_label)
print(loss)   # Small number — model was confident and correct

The Penalty Table

Confidence in correct answer → Loss

0.99  (99% sure — and correct) → loss = 0.01   tiny penalty
0.80  (80% sure — and correct) → loss = 0.22   small penalty
0.50  (50/50 — uncertain)      → loss = 0.69   medium penalty
0.20  (20% sure — and correct) → loss = 1.61   large penalty
0.01  (1% sure — but correct)  → loss = 4.61   huge penalty

Being confidently wrong is the worst possible outcome. Being uncertain costs less. Being confidently right is rewarded with near-zero loss.

The Two Loss Functions You Will Use Most

# ── For regression — predicting numbers ───────────────────────────
nn.MSELoss()
# Used when: predicting house prices, sales numbers, temperatures
# Measures: average squared distance between prediction and reality

# ── For classification — predicting categories ────────────────────
nn.CrossEntropyLoss()
# Used when: sentiment analysis, spam detection, image classification
# Measures: how confident the model was about the correct class

# Your Nigerian Pidgin sentiment paper will use CrossEntropyLoss
# because you are predicting: Positive / Negative / Neutral
loss_fn = nn.CrossEntropyLoss()

What Happens to the Loss During Training

Epoch 1:    Loss = 1.842   ← terrible predictions
Epoch 100:  Loss = 0.923   ← getting better
Epoch 500:  Loss = 0.412   ← reasonable
Epoch 1000: Loss = 0.187   ← good
Epoch 5000: Loss = 0.043   ← excellent

                ↓ loss
    1.8 |█
        |█
    1.2 |█
        |██
    0.6 |  ████
        |      ████████
    0.0 |              ████████████████
        └─────────────────────────────→ epochs

A healthy loss curve goes down smoothly. Problems look like this:

Loss going UP        → learning rate too high, something is broken
Loss completely flat → learning rate too low or wrong architecture
Loss bouncing wildly → learning rate too high or data problems
Loss great on train,
high on val          → overfitting

The Real Words Mapped to the Story

In the StoryReal Technical Term
The teacher marking papersLoss function
The exam scoreLoss value
Small penalty for close answersLow loss
Huge penalty for wrong answersHigh loss
Average of squared errorsMean Squared Error (MSE)
Measuring confidence in correct classCross-Entropy Loss
Loss going down over timeModel is learning
Loss curveTraining progress chart
Loss stops going downConvergence
Loss great on train, bad on valOverfitting

The One Thing to Remember

The loss function is the only thing your neural network actually cares about. It does not care about being accurate. It does not care about being useful. It only tries to make the loss number smaller. Your entire job as an ML engineer is to choose a loss function that, when minimised, produces a model that is genuinely useful.

Choosing the wrong loss function is like hiring that lenient teacher — the score looks good but nobody actually learned anything.

← All Articles