Explain cross-entropy loss — binary vs categorical, and why it works for classification
Written by the mlprep team · Updated
Explain cross-entropy loss. Why do we use it for classification instead of MSE? Walk me through binary vs. categorical cross-entropy and how softmax fits in.
What cross-entropy measures#
Cross-entropy between a true distribution p and predicted distribution q is:
For classification, p is a one-hot distribution (1 at the correct class, 0 elsewhere) so almost all terms vanish. What remains is just -log(q_correct) — the log probability assigned to the true class. Minimizing cross-entropy = maximizing log-likelihood of the correct class.
Binary cross-entropy (BCE)#
For binary classification with a sigmoid output:
When y=1: loss = -log(ŷ). A confident correct prediction (ŷ≈1) gives near-zero loss; a wrong confident prediction (ŷ≈0) gives very high loss.
When y=0: loss = -log(1-ŷ). Symmetric penalization.
Categorical cross-entropy#
For multiclass with a softmax output:
Since y is one-hot, only the true class term contributes. The loss is the negative log probability assigned to the correct class. Softmax ensures all ŷ_c sum to 1 and are in (0,1).
Why not MSE for classification?#
MSE on sigmoid outputs: gradient = (ŷ - y) · ŷ(1-ŷ). Near 0 or 1, ŷ(1-ŷ) ≈ 0 — gradients vanish even when the prediction is badly wrong. Learning stalls.
Cross-entropy gradient for BCE: (ŷ - y). No saturation. When the model is confidently wrong, the gradient is near ±1 and learning proceeds at full speed. The log "cancels" the sigmoid's squashing.
Softmax + cross-entropy as a unit#
The softmax-cross-entropy combination is numerically computed together for stability. The gradient of the combined operation is simply:
This is remarkably clean — linear in the prediction error. It's one reason this combination dominates classification.
Log-likelihood connection#
Cross-entropy minimization is equivalent to maximum likelihood estimation under a categorical (or Bernoulli) distribution. You're finding model parameters that make the observed training data most probable. This gives cross-entropy a principled probabilistic foundation that MSE lacks for classification tasks.
Interview red flags#
- Unable to explain why MSE is a poor fit for classification beyond "cross-entropy is standard".
- Applying softmax in the model and again inside a loss that expects logits.
- Not knowing that cross-entropy is the negative log-likelihood of the correct class.
- Mixing up binary cross-entropy for multi-label problems with categorical cross-entropy for mutually exclusive classes.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Cross-entropy = -log(probability assigned to correct class). Use it for classification because: (1) the gradient doesn't vanish when the model is confidently wrong — unlike MSE on sigmoid; (2) it's equivalent to maximum likelihood under a categorical distribution; (3) softmax + cross-entropy has a clean gradient of (ŷ - y). BCE for binary tasks, categorical CE for multiclass.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How does label smoothing modify cross-entropy and why does it help generalization?
- What's the difference between cross-entropy and KL divergence? When are they equivalent?
- You have a 1000-class classification problem. How does the softmax denominator scale and what problems can arise?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions