Derive cross-entropy loss from first principles. Why is it "the right" loss for classification and language modeling?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Start from maximum likelihood. A classifier (or LM) defines a conditional distribution p_θ(y|x). Given i.i.d. data, MLE maximises Π p_θ(yᵢ|xᵢ); taking logs (monotone, turns products into sums, numerically necessary) and negating gives the training objective: minimise negative log-likelihood = −Σ log p_θ(yᵢ|xᵢ). For a categorical output with one-hot targets, that is exactly cross-entropy: H(y, p) = −Σ_c y_c log p_c = −log p(correct class). Language modeling is this applied per token: next-token cross-entropy is MLE of the text distribution, and perplexity = exp(mean CE) - "how many equally likely tokens is the model choosing among."
Full worked answer
Start from maximum likelihood. A classifier (or LM) defines a conditional distribution p_θ(y|x). Given i.i.d. data, MLE maximises Π p_θ(yᵢ|xᵢ); taking logs (monotone, turns products into sums, numerically necessary) and negating gives the training objective: minimise negative log-likelihood = −Σ log p_θ(yᵢ|xᵢ). For a categorical output with one-hot targets, that is exactly cross-entropy: H(y, p) = −Σ_c y_c log p_c = −log p(correct class). Language modeling is this applied per token: next-token cross-entropy is MLE of the text distribution, and perplexity = exp(mean CE) - "how many equally likely tokens is the model choosing among."
The information-theoretic view: minimising CE against the empirical data distribution is equivalent to minimising KL(data ‖ model) - you're matching the model distribution to the data distribution, and the gap above the data's own entropy is exactly the KL term.
Why it's right mechanically, not just philosophically:
- Gradient quality. With softmax outputs, ∂CE/∂logits = p − y. Confidently wrong (p≈0 on the true class) → gradient near maximal; correct and confident → gradient near zero. No saturation on mistakes.
- Proper scoring rule: expected CE is minimised only by predicting the true conditional probabilities, so it incentivises honest uncertainty - the basis for LM calibration.
- It punishes confident errors unboundedly (−log p → ∞ as p→0), which is what you want when downstream decisions consume the probabilities.
The framing that lands in interviews: cross-entropy isn't one loss among many - it's what "do MLE on a categorical distribution" compiles to. MSE is the same statement for Gaussian noise; every standard loss is a likelihood in disguise.
What a good answer covers
- A classifier (or LM) defines a conditional distribution p_θ(y|x)
- Given i.i.d
- For a categorical output with one-hot targets, that is exactly cross-entropy
- Language modeling is this applied per token
- The information-theoretic view
Key terms: maximum likelihood · negative log-likelihood · perplexity = exp(mean CE) · KL(data ‖ model) · Gradient quality · p − y · Proper scoring rule
In the glossary: Cross-entropy · Calibration
Interviewer follow-ups to expect
Show that softmax+CE gives gradient p − y. What does label smoothing change in this picture?
- Gradient quality. With softmax outputs, ∂CE/∂logits = p − y. Confidently wrong (p≈0 on the true class) → gradient near maximal; correct and confident → gradient near zero. No saturation on mistakes.
- Proper scoring rule: expected CE is minimised only by predicting the true conditional probabilities, so it incentivises honest uncertainty - the basis for LM calibration.
- It punishes confident errors unboundedly (−log p → ∞ as p→0), which is what you want when downstream decisions consume the probabilities.
Why is perplexity exp of CE, and what does perplexity 8 mean concretely?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep