# MSE vs Cross-Entropy Loss

> Mean squared error or cross-entropy: which loss fits a number and which fits a probability, and why it matters, for your AI interview.

**In short:** Use mean squared error (MSE) when a model predicts a number, such as a house price. Use cross-entropy, also called log loss, when it predicts a probability, such as fraud or not, because it punishes confident wrong answers much harder.

AI and LLM interview question Q419, Classic ML and deep-learning basics. Intermediate, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/mse-vs-cross-entropy-loss

## Say it in 30 seconds

MSE is for predicting a number, like a house price. It averages the squared gaps between guesses and truth, so big misses hurt a lot. Cross-entropy is for predicting a probability, like fraud or not. It punishes a confident wrong answer much harder than squared error does. So I pick the loss from the output: numbers get MSE, probabilities get cross-entropy.

## Say it in 2 minutes

A loss function is the score a model gets for each wrong answer. Training changes the model so that score goes down. The score I choose decides which mistakes the model learns to fix first, so the first question is always what the model outputs.

If it outputs a number, like the price of a house, I use mean squared error. For each house I take the gap between the guess and the real price, square it, and average the squares. Squaring means one badly wrong mansion valuation counts far more than many small misses. That helps when big misses are costly, and it is a trap when one outlier, a very unusual value far from the rest, can drag the whole model.

If it outputs a probability, like fraud at 93%, I use cross-entropy, also called log loss. It takes the log of the probability the model gave to the true answer. If the model said 1% fraud on a payment that was fraud, the penalty is large, about 4.6 in my small made-up example. With a sigmoid output (the S-shaped step that turns a score into a probability), the squared-error push on that same miss is about fifty times weaker.

In PyTorch, a popular AI library for Python, CrossEntropyLoss takes raw scores called logits and applies the softmax step itself. So I pick the loss from the output, check the numbers on a few made-up cases, and ask what a miss costs the business.

## What are MSE and cross-entropy, in plain words?

A loss function is a score for how wrong a model's answer was. Training changes the model so that score goes down. The loss you pick decides which mistakes the model learns to fix first.

Mean squared error (MSE) is the average of the squared gaps between each guess and the true value. Cross-entropy, also called log loss, is for a model that gives a probability, such as the chance a card payment is fraud. It asks how much probability the model gave to the true answer.

Think of two claims adjusters at an insurer. One estimates the repair bill for a flood-hit flat, which is a number. The other decides whether a claim is fraud, which is a probability. Each needs its own way of being scored.

So a model that predicts a number gets MSE. A model that predicts a probability gets cross-entropy. A bank's card fraud model is the probability case.

- **Loss function:** The score for one wrong answer. Training pushes it down.
- **Label:** The true answer in your data, such as 1 for fraud and 0 for not fraud.
- **Probability:** A number from 0 to 1. 0.93 means the model is 93% sure.
- **Logit:** The raw score a model makes before it turns into a probability.

## What does MSE do, and where does it go wrong?

MSE takes each guess, subtracts the true value, squares the gap, and averages those squares. Squaring makes a big miss count far more than a small one. That is useful when big misses are costly.

Worked example, with made-up numbers. A valuation model for a mid-size city guesses 2.4 million for a mansion that sold for 1.5 million. The gap is 900,000, and its square is 810 billion. A flat that is 10,000 off adds only 100 million. So one mansion miss counts as much as 8,100 small flat misses, and training bends toward it.

> "MSE moves the model more toward the outliers, while MAE doesn't." (Google, machine learning crash course)
>
> "Choose MSE: If you want to heavily penalize large errors." (Google, machine learning crash course)

The result is in squared units, so a price error becomes dollars squared. Take the square root, called RMSE, to get back to dollars.

> **Tip:** Asked which loss for a price model? Say MSE, then name its weak spot: one huge miss can drag the whole model. Add that you would check the data for outliers first.

## What does cross-entropy do, and why is it for probabilities?

Cross-entropy scores a probability by taking the log of the chance the model gave to the true answer, then flipping the sign so the score is positive. If the true answer got a tiny chance, the log is very negative, so the score is large.

> "Log loss, aka logistic loss or cross-entropy loss." (scikit-learn documentation)
>
> "This is the loss function used in (multinomial) logistic regression and extensions of it such as neural networks" (scikit-learn documentation)

It is the loss used in multinomial logistic regression and in extensions of it, such as neural networks. In PyTorch, a popular AI library for Python, CrossEntropyLoss expects unnormalised logits, the raw scores before any probability step, and it applies the softmax step itself. Softmax rescales scores so they lie between 0 and 1 and add up to 1.

Here is a made-up fraud model at a bank. For a payment that really was fraud, it gives only a 1% chance of fraud. The code below scores that one miss and shows how hard each loss pushes the model. Here 'push' means how much one example changes the model's raw score during training. The demo assumes the model turns that raw score into a probability with a sigmoid, an S-shaped step. It uses only Python's standard library.

```python
# A made-up fraud model at a bank. Label 1 means the payment really was fraud.
# The model's probability of fraud is p. We check the loss and the push on each step.
import math

def mse(p, y):
    return (p - y) ** 2

def cross_entropy(p, y):
    return -(y * math.log(p) + (1 - y) * math.log(1 - p))

def push_mse(p, y):
    # MSE's push passes through the sigmoid's slope, p * (1 - p), so it shrinks when p is near 0 or 1
    return 2 * (p - y) * p * (1 - p)

def push_ce(p, y):
    return p - y

y = 1  # the payment really was fraud
for p in (0.01, 0.5, 0.9):
    print(f"model says {p:.0%} fraud, truth is fraud")
    print(f"  loss: MSE {mse(p, y):.3f}, cross-entropy {cross_entropy(p, y):.3f}")
    print(f"  push: MSE {push_mse(p, y):+.4f}, cross-entropy {push_ce(p, y):+.2f}")
```

Output:

```text
model says 1% fraud, truth is fraud
  loss: MSE 0.980, cross-entropy 4.605
  push: MSE -0.0196, cross-entropy -0.99
model says 50% fraud, truth is fraud
  loss: MSE 0.250, cross-entropy 0.693
  push: MSE -0.2500, cross-entropy -0.50
model says 90% fraud, truth is fraud
  loss: MSE 0.010, cross-entropy 0.105
  push: MSE -0.0180, cross-entropy -0.10
```

*Diagram, One confident miss, two pushes: The same confident miss on a fraud model, scored two ways. The squared gap gives a weak push and log loss gives a strong one. The push numbers come from the small made-up example above.*
1. The squared gap gives a small push on this miss.
2. Log loss gives a large push on the same miss.
3. The small push means slower learning from this miss.
4. The large push means faster learning from this miss.

The numbers show the gap. In the demo, MSE's push on this confident miss is about 50 times weaker than cross-entropy's. With a sigmoid output, the squared gap gives a small push when the model is very sure, so it learns slowly from its worst misses.

## Which loss should you pick, and what are the limits?

Pick by the output first, then ask what a miss costs. A number gets MSE and a probability gets cross-entropy. Training lowers whichever score you chose.

*Diagram, Pick the loss by the output: Start from what the model outputs. A number goes to MSE, a probability goes to cross-entropy, and training lowers the score you chose.*
1. A number gets the squared gap between guess and truth.
2. A probability gets the log of the chance given to the true answer.
3. Training lowers the MSE score.
4. Training lowers the cross-entropy score.

- **Use MSE: A number, like a price.** Averages the squared gaps. Big misses cost more, so outliers can pull the model.
- **Use cross-entropy: A probability, like fraud.** Scores the chance given to the true answer. A confident miss gets a big penalty.
- **Check first: Your labels.** Both losses trust the labels. A wrong fraud label can cost a lot, so clean them first.

Neither loss fixes a wrong target. If the business cares most about rare fraud, check the scores on the fraud cases, not only the average loss.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Pick the loss from the output: a number gets MSE, a probability gets cross-entropy.
- [ ] MSE is the average of squared gaps, and big misses weigh heavily.
- [ ] Cross-entropy is the log of the chance given to the true answer.
- [ ] Why it punishes confident wrong answers harder, with a number.
- [ ] PyTorch's CrossEntropyLoss expects raw logits, not probabilities.
- [ ] Clean labels matter, because the loss trusts them.

**Common traps**

- Trap: "MSE is always the safe default." Better: It is safe for numbers. For probabilities it pushes weakly on confident misses, the cases that hurt most.
- Trap: "Cross-entropy works on any output." Better: It needs probabilities, or raw logits in PyTorch. A price is the wrong fit.
- Trap: "Log loss and cross-entropy are two different losses." Better: Same idea here. Scikit-learn calls it log loss, aka cross-entropy.

## Can you spot the wrong pairing?

**Spot the wrong pairing:** Which of these pairs a loss with the wrong kind of output?

A. MSELoss on the predicted price of a flat
B. CrossEntropyLoss on the raw logits of a fraud model
C. CrossEntropyLoss on the predicted price of a flat
D. MSELoss on the predicted delivery time in minutes

**Answer:** C. A price is a number, so it wants MSE. Cross-entropy is for classes and probabilities, so pairing it with a price is the mistake. Raw logits are fine, because PyTorch's loss expects them.

**Quick check:** A bank's model outputs the chance that a card payment is fraud. Which loss fits best?

A. Mean squared error on the 0 or 1 label, since it is simple
B. Cross-entropy on the predicted probability, since the output is a probability
C. Mean squared error on a predicted price, since prices are numbers
D. A higher learning rate

**Answer:** B. The output is a probability, so cross-entropy scores it directly. MSE can run on it too, but it pushes weakly on confident misses, the cases a fraud team cares about most.

## Can you answer these from memory?

**Flash cards**

- **What is MSE?** The average of the squared gaps between each guess and the true value.
- **What is cross-entropy?** Also called log loss: minus the log of the probability given to the true answer.
- **Which loss for a house price?** MSE, because the output is a number.
- **Which loss for a fraud probability?** Cross-entropy, because the output is a probability.
- **Why does MSE struggle with outliers?** Squaring makes one huge miss count far more than many small ones.
- **What does PyTorch's CrossEntropyLoss take?** Raw logits. It applies the softmax step itself.

## Likely follow-up questions

- Q415 What is gradient descent? https://designervenkat.online/interview/ai/what-is-gradient-descent
- Q418 What is backpropagation? https://designervenkat.online/interview/ai/what-is-backpropagation
- Q417 What does the learning rate do? (full answer coming)
- Q420 Precision, recall and F1? https://designervenkat.online/interview/ai/precision-recall-and-f1
- Q414 L1, L2 and dropout regularisation? https://designervenkat.online/interview/ai/l1-l2-and-dropout-regularisation
- Q416 SGD vs Adam? (full answer coming)

## Learn it in depth

- [Loss functions: measuring a mistake](https://designervenkat.online/learn/machine-learning/loss-functions)
- [Log loss: how sure was the model?](https://designervenkat.online/learn/machine-learning/log-loss)
