Skip to content
GenAI Learn/The Math You Actually Need
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Loss Functions: Measuring Wrongness

8 min read

You'll learn to

  • -Understand a loss function as a single number quantifying how wrong a model's predictions are
  • -Write and implement mean squared error and binary cross-entropy
  • -Extend binary cross-entropy to categorical cross-entropy for more than two classes

Every training process needs a single number to answer one question: how wrong is the model right now? That number is called the loss (sometimes the cost), and training is, at its core, an attempt to make that number as small as possible.

Comparing Prediction to Reality

A loss function takes the model's prediction and the actual correct answer and outputs a number representing how far apart they are. Predict a house at $410,000 when it actually sold for $400,000, and a simple loss function might just report the $10,000 difference, or more commonly that difference squared, which penalizes big misses much more than small ones.

Mean Squared Error: The Default for Regression

Mean Squared Error
MSE = (1/n) Σᵢ₌₁ⁿ (ŷᵢ − yᵢ)²
Average the squared difference between each prediction ŷᵢ and true value yᵢ. Squaring makes every error positive and punishes large misses disproportionately. A miss of 10 contributes 100, while a miss of 2 contributes only 4.

Binary Cross-Entropy: The Default for Two-Class Problems

For classification, squared error is not the natural choice. You want to punish a model that was confidently wrong far more than one that was merely uncertain. Binary cross-entropy does exactly that.

Binary Cross-Entropy
BCE = −(1/n) Σᵢ [yᵢ log(ŷᵢ) + (1−yᵢ) log(1−ŷᵢ)]
For each example, only one of the two terms is active depending on whether the true label yᵢ is 0 or 1. Because log(x) heads toward negative infinity as x approaches 0, a confident wrong prediction gets punished extremely harshly.
MSE and binary cross-entropy, and why confidence matters

What About More Than Two Classes?

Real problems rarely stop at two classes. Is this photo a cat, a dog, or a bird? Binary cross-entropy generalizes cleanly into categorical cross-entropy, which compares a full predicted probability distribution across every class against a true distribution that puts all its weight on the one correct class.

Categorical Cross-Entropy
CCE = −Σᵢ yᵢ log(ŷᵢ)
yᵢ is 1 for the true class and 0 for every other class (a representation called one-hot encoding), so this sum collapses to a single term: the negative log of the predicted probability assigned to the correct class. Predict the right class with high confidence and the loss is small. Predict it with low confidence and the loss grows fast.

Categorical cross-entropy is almost always paired with a softmax output layer, which the next chapter covers in depth. Softmax turns a network's raw scores into a valid probability distribution across all the classes, and categorical cross-entropy is the loss that scores how good that distribution is.

Loss Is Not the Same as the Metric You Actually Care About

One subtlety worth remembering: the loss function used during training is not always the same thing as the business metric you actually care about. A model might be trained to minimize squared error on price predictions, while the business cares about the percentage of predictions within 5% of the true price. These can pull in slightly different directions, which is worth watching for.

Interview shorthand worth memorizing: MSE for regression, binary cross-entropy for two classes, categorical cross-entropy for more than two. You will rarely be wrong reaching for these as your default answer, as long as you can explain why the shape of each penalty fits its task.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

ScaleDojo Logo
Initializing ScaleDojo