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 10 of 10

10Summary

  • A loss decides what the model cares about. Training minimizes the average loss (1), and the derivative \partial\ell/\partial\hat y is each example’s pull. Most losses are negative log-likelihoods, -\log p_\theta(y\mid x).
  • Squared error (y-\hat y)^2: pull \propto error, estimates the mean, Gaussian maximum likelihood, closed form \hat w=(X^\top X)^{-1}X^\top y. Errors 1,2,5 cost 1,4,25.
  • Absolute error |y-\hat y|: constant pull, estimates the median, Laplace maximum likelihood. Huber (6): pull \operatorname{clip}(r,-\delta,\delta). One bad label: slope 3.27 (squared) versus 2.01 (absolute) and 2.05 (Huber). Street: mean 1.22 million, median 300 thousand.
  • Cross-entropy -\log p_{\text{true}}: Bernoulli or categorical maximum likelihood with the softmax (9); gradient p-y; proper scoring rule. Logits (2,1,0.1): p=(0.659,0.242,0.099), loss 0.417.
  • Large language models: \mathcal L=-\sum_t\log p_\theta(w_t\mid w_{<t}), cross-entropy on every next token; perplexity is \exp of the average cross-entropy.
  • Not squared error for classification: gradient 2(p-y)p(1-p) vanishes for confident mistakes (75\times weaker at z=-5) and is non-convex for z<-\log2.
  • Hinge \max(0,1-\tilde yf): zero for margin \ge1, so only support vectors matter; SVM =\sum hinge +\lambda\|w\|^2. Minimizers: hinge \operatorname{sign}(2\eta-1), log loss \log\frac{\eta}{1-\eta}; only log loss gives probabilities.