MoonSay /Tutorials/Machine Learning Interview #7
Contents
  1. 01Introduction
  2. 02What a loss function does
  3. 03Squared error
  4. 04Absolute error and Huber loss
  5. 05Cross-entropy
  6. 06Why not squared error for classification?
  7. 07Hinge loss
  8. 08Choosing a loss
  9. 09Interview questions and model answers
  10. 10Summary
  11. References
Loss Functions/Chapter 08 of 10

08Choosing a loss

8.1A short guide

Table 2 collects the losses of this tutorial. The question to ask is the one from Section 2: what kind of target, what noise, and what should the model’s output mean?

LossFormulaEstimatesUse when
Squared(y-\hat y)^2meanregression with roughly Gaussian noise; closed form for linear models
Absolute|y-\hat y|medianoutliers, skewed targets, or when the metric is mean absolute error
Huber(6)between mean and medianregression with occasional outliers; smooth
Cross-entropy-\log p_{\text{true}}probabilitiesclassification, including next-word prediction in language models
Hinge\max(0,1-\tilde yf)the class (sign)large-margin classifiers (support vector machines); sparse solutions
Table 2. The classic losses at a glance.

Three rules of thumb cover most interview answers. First, write down the noise model and take its negative log-likelihood: that is the principled default (Gaussian: squared; Laplace: absolute; categorical: cross-entropy). Second, if a few bad labels must not dominate, bound the pull of each example (absolute or Huber). Third, for classification, train with cross-entropy when you need probabilities and hinge loss when you only need a decision with a wide margin; report the metric you actually care about.

8.2Common misconceptions

  • “Cross-entropy, log loss and negative log-likelihood are different losses.” For classification they are the same quantity: -\log of the probability given to the true class (Murphy, 2022, §5.1.6).
  • “Train on the metric you report.” Accuracy and other counting metrics have zero gradient almost everywhere. Train on a surrogate whose minimizer is good for the metric, and report the metric.
  • “Squared error is fine for classification, it has the right minimum.” The minimum is right but the gradient vanishes for confident mistakes and the loss is not convex in the logit (Section 6).
  • “A support vector machine outputs a probability.” Its score estimates only the sign of 2\eta-1 (17); probabilities need a separate calibration step.
  • “Absolute error is always better when there are outliers.” It is robust, but it estimates the median, not the mean, and on clean Gaussian data it is less efficient than squared error. If you need the mean, use Huber loss.
  • “A lower loss always means a better model.” Losses are comparable only on the same data with the same loss. A cross-entropy of 0.4 and a squared error of 0.1 cannot be compared, and a language model’s perplexity depends on its vocabulary.