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

09Interview questions and model answers

9 questions · click a question to see a brief answer
Q1What is the difference between mean squared error and mean absolute error? Which would you use with outliers?
Brief answer

Squared error grows quadratically, so its pull on the fit is proportional to the error and a single outlier can dominate; it estimates the mean. Absolute error grows linearly, every point pulls with the same strength, and it estimates the median. With outliers, use absolute error or Huber loss. In the example of this tutorial, one mistyped price tilts least squares from slope 2 to 3.27, while absolute error stays at 2.01 and Huber at 2.05.

Q2Why is mean squared error the natural loss for regression?
Brief answer

It is the negative log-likelihood under Gaussian noise: -\log\mathcal N(y\mid f(x),\sigma^2) equals (y-f(x))^2/(2\sigma^2) plus a constant. Its minimizer is the conditional mean, and for linear models it has the closed form \hat w=(X^\top X)^{-1}X^\top y.

Q3What is Huber loss, and what does \delta do?
Brief answer

\frac12r^2 for |r|\le\delta and \delta|r|-\frac12\delta^2 beyond: quadratic for small errors, linear for large ones, smooth at the junction. Its derivative is \operatorname{clip}(r,-\delta,\delta), so no example pulls harder than \delta. \delta is the error size above which a point is treated as an outlier, set from the noise level or by cross-validation.

Q4Derive the gradient of softmax cross-entropy with respect to the logits.
Brief answer

\ell=-\log p_c=-z_c+\log\sum_je^{z_j}. The derivative of the first term is -\mathbb 1[k=c], of the second e^{z_k}/\sum_je^{z_j}=p_k. So \partial\ell/\partial z_k=p_k-y_k, or p-y for a one-hot y. Example: logits (2,1,0.1), true class first: p=(0.659,0.242,0.099), loss 0.417, gradient (-0.341,0.242,0.099).

Q5Why not use mean squared error for classification?
Brief answer

With a sigmoid output, \partial(p-y)^2/\partial z=2(p-y)\,p(1-p); the factor p(1-p) vanishes when the model is confidently wrong. At z=-5 with true label 1 the gradient is -0.0132 against -0.993 for cross-entropy, about 75 times weaker. It is also non-convex in the logit. Cross-entropy is the maximum-likelihood loss and its gradient is simply p-y.

Q6What loss does a large language model minimize? What is perplexity?
Brief answer

Cross-entropy on the next token: the next word is one choice from the whole vocabulary, so the model outputs a softmax over all tokens and pays -\log p(w_t\mid w_{<t}) for the token that actually follows, summed over all positions. Perplexity is \exp of the average cross-entropy per token: an effective number of equally likely choices, 1 for a perfect model.

Q7Compare hinge loss and logistic loss. What are support vectors? Does an SVM output probabilities?
Brief answer

Both are convex surrogates of the 0-1 loss in terms of the margin m=\tilde yf(x). Hinge, \max(0,1-m), is exactly zero for m\ge1, so only examples with m\le1, the support vectors, affect the solution. Log loss, \log(1+e^{-m}), never reaches zero and estimates the log-odds, so it gives probabilities. Hinge loss’s minimizer is \operatorname{sign}(2\eta-1), so SVM scores are not probabilities; calibrate them (Platt scaling) if needed.

Q8Why not optimize accuracy directly?
Brief answer

Accuracy is a sum of 0-1 losses, a step function of the parameters: its gradient is zero almost everywhere and undefined at the steps, and minimizing it exactly is computationally hard. We minimize a convex or smooth surrogate (cross-entropy, hinge) whose minimizer gives the same decisions, and report accuracy.

Q9What is a proper scoring rule, and why does it matter?
Brief answer

A loss on predicted probabilities whose expected value is minimized by reporting the true probabilities. Cross-entropy is proper because \E_p[-\log q]=H(p)+D_{\mathrm{KL}}(p\|q), minimized at q=p; the Brier score is also proper. Training with a proper scoring rule encourages calibrated probabilities. With a true rain probability of 0.7, forecasting 0.7 costs 0.611 on average, overconfident 0.9 costs 0.765.