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

07Hinge loss: the margin view

7.1Margins and the 0-1 loss

For two classes with labels \tilde y\in\{-1,+1\} and a real-valued score f(x), the classifier predicts the sign of f(x). The margin of an example is

m=\tilde y\,f(x).

A positive margin means a correct prediction, a negative one a mistake, and a large positive margin means correct with room to spare. The error count is the 0-1 loss \mathbb 1[m\le0], a step that gradient descent cannot use. Every classification loss in this tutorial is a function of the margin that serves as a smooth or convex stand-in for that step (Murphy, 2022, §4.3.2):

\underbrace{\max(0,\,1-m)}_{\text{hinge}},\qquad\underbrace{\log(1+e^{-m})}_{\text{log loss}},\qquad\underbrace{e^{-m}}_{\text{exponential}},\qquad\underbrace{(1-m)^2}_{\text{squared}}. \tag{15}

The log loss here is the binary cross-entropy (7) written with \tilde y\in\{-1,+1\} and p=\sigma(f): -\log\sigma(m)=\log(1+e^{-m}). The exponential loss is the loss of AdaBoost (Murphy, 2022, §18.5). Figure 5 plots all of them.

Classification losses as functions of the margin m=\tilde y f(x). Hinge loss is exactly zero for m\ge1; log loss (in base 2, so that it passes through 1 at m=0) and exponential loss never reach zero; squared loss also punishes margins larger than 1. In the style of Murphy (2022, Fig. 4.2); computed for this tutorial.
Figure 5. Classification losses as functions of the margin m=\tilde y f(x). Hinge loss is exactly zero for m\ge1; log loss (in base 2, so that it passes through 1 at m=0) and exponential loss never reach zero; squared loss also punishes margins larger than 1. In the style of Murphy (2022, Fig. 4.2); computed for this tutorial.

7.2Hinge loss and the support vector machine

The hinge loss \max(0,1-m) is zero once an example is on the correct side with margin at least 1, and grows linearly as the margin shrinks or turns negative. The soft-margin support vector machine (Cortes and Vapnik, 1995) minimizes total hinge loss plus a penalty on the weights (Murphy, 2022, §17.3):

\min_{w,b}\;\sum_{n=1}^N\max\bigl(0,\;1-\tilde y_n(w^\top x_n+b)\bigr)+\lambda\|w\|^2 . \tag{16}

The penalty keeps \|w\| small, which is the same as keeping the geometric margin 1/\|w\| wide. (The usual form \tfrac12\|w\|^2+C\sum_n\xi_n with slack variables \xi_n is equivalent, with \lambda=1/(2C).)

The subgradient of the hinge term with respect to w is -\tilde y_nx_n if m_n<1 and 0 if m_n>1. So examples that are safely correct contribute nothing at all; only the examples on or inside the margin, the support vectors, determine the solution. Delete any other training example and the classifier does not change.

Worked example 4 (A spam filter). A filter gives five spam emails (\tilde y=+1) the scores 2.6, 1.8, 1.25, 0.6 and -0.3, and five normal emails (\tilde y=-1) the scores -2.4, -1.6, -1.1, -0.5 and 0.4. The margins are 2.6, 1.8, 1.25, 0.6, -0.3 and 2.4, 1.6, 1.1, 0.5, -0.4. Hinge losses: 0, 0, 0, 0.4, 1.3 and 0, 0, 0, 0.5, 1.4, a total of 3.6. Only four emails, two inside the margin and two misclassified, have any loss, and only they move the filter. Under log loss every email contributes; even the six safe ones still cost between 0.072 and 0.287.

7.3What each loss estimates

Let \eta(x)=P(\tilde y=+1\mid x). What score f(x) minimizes the expected loss at x? For the hinge loss, on -1\le f\le1 the expected loss is

\eta\,(1-f)+(1-\eta)(1+f)=1+(1-2\eta)\,f,

a straight line in f, minimized at f=+1 if \eta>\tfrac12 and at f=-1 if \eta<\tfrac12 (outside [-1,1] the loss only grows). For the log loss, the derivative of \eta\log(1+e^{-f})+(1-\eta)\log(1+e^{f}) is \sigma(f)-\eta, zero when \sigma(f)=\eta. Together with the corresponding results for the other losses (Hastie et al., 2009, §10.5, Table 12.1):

f^\star_{\text{hinge}}=\operatorname{sign}(2\eta-1),\qquad f^\star_{\text{log}}=\log\frac{\eta}{1-\eta},\qquad f^\star_{\text{exp}}=\frac12\log\frac{\eta}{1-\eta},\qquad f^\star_{\text{sq}}=2\eta-1 . \tag{17}

All four have the sign of 2\eta-1, so all four lead to the best possible classifier in the limit of infinite data. But only the losses whose minimizer is a one-to-one function of \eta let us read back a probability. Log loss estimates the log-odds, so \sigma(f) is a probability; hinge loss estimates only the side of the boundary. That is why a support vector machine’s scores are not probabilities, and why they are calibrated afterwards (for example by Platt scaling, a logistic regression on the scores) when probabilities are needed (Murphy, 2022, §17.3).

Pause and think. Hinge loss and log loss look almost the same in Figure 5. Name two practical differences.

Show the answer

(1) Hinge loss is exactly zero for margins above 1, so the solution depends only on the support vectors (a sparse solution); log loss keeps pulling on every example, a little. (2) Log loss estimates the log-odds, so it gives probabilities; hinge loss gives only a decision. Log loss is also smooth, hinge loss has a corner at m=1.