Search

Search papers and lessons

Hinge Loss

Level 3 · 12 min read

Hinge loss charges nothing for a correct prediction that clears a safety margin and charges a straight-line penalty for mistakes and near misses, which is why it trains support vector machines.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Losses & regularization
  • Linear algebra
  • Optimization
  • ReadReady to readNot yet

TL;DR

A linear classifier computes a raw score ss for each input, and the sign of ss picks the class. Hinge loss turns that score and the true label into a penalty. The penalty is zero when the score is on the correct side of the boundary by at least 1, and it grows in a straight line as the score moves toward the wrong side. It is the loss behind the support vector machine, and it pushes the boundary away from the nearest training points.

Builds on: Loss Function, Linear Classifier, Convex Function · Leads to: Support Vector Machine

Where it's used

Text classification with linear SVMs. Before deep networks took over, linear support vector machines trained with hinge loss were a standard tool for sorting documents into topics and for filtering spam. Libraries such as LIBLINEAR train them on sparse word-count vectors with millions of features, because each training step only touches the features a document actually uses.

Face embeddings. FaceNet (2015) learned a vector for each face image so that photos of the same person sit close together. Its triplet loss is a hinge in disguise: it asks that the distance to a matching photo be smaller than the distance to a non-matching photo by at least a fixed margin, and it charges a linear penalty when that fails.

How it works

Use the classifier from the Linear Classifier lesson: the score is s=2x−4s = 2x - 4, and a positive score means the class is +1+1. Each training example has a true label yy, which is +1+1 or −1-1. The hinge loss needs one new quantity: the product ysy s.

  1. Take the first labelled example: x=3x = 3, true label y=+1y = +1.
  2. Compute the score: s=2⋅3−4=2s = 2 \cdot 3 - 4 = 2. Check: the score is positive, so the classifier says +1+1, which is right.
  3. Compute the product ys=1⋅2=2y s = 1 \cdot 2 = 2. A positive product means the score is on the correct side. Its size says how far from the boundary the point sits.
  4. Apply the hinge: the loss is max⁡(0,  1−2)=max⁡(0,−1)=0\max(0,\; 1 - 2) = \max(0, -1) = 0. The point clears the margin of 1, so it costs nothing.
  5. Take the second example: x=2.2x = 2.2, y=+1y = +1. The score is s=2⋅2.2−4=0.4s = 2 \cdot 2.2 - 4 = 0.4. Check: 4.4−4=0.44.4 - 4 = 0.4.
  6. The product is ys=0.4y s = 0.4. It is on the correct side, but only just, because 0.40.4 is less than 1. The hinge gives max⁡(0,  1−0.4)=0.6\max(0,\; 1 - 0.4) = 0.6. The point is right but too close to the boundary, so it pays a small price.
  7. Take the third example: x=1x = 1, y=+1y = +1. The score is s=−2s = -2, which is on the wrong side. The product is ys=−2y s = -2, and the loss is max⁡(0,  1−(−2))=3\max(0,\; 1 - (-2)) = 3.
  8. Flip the label of the first example to y=−1y = -1 and keep x=3x = 3. The score is still 22, but now ys=−2y s = -2 and the loss is 33, the same as the wrong-side case above.
  9. Look at the pattern. For a positive label, the loss is zero once the score reaches 1 and rises as the score falls below 1. The graph below shows this.
−3−2−101230123score shinge loss
  • loss when y = +1
For a positive label the loss is flat at zero past s = 1 and climbs in a straight line below it. The three marked points are steps 4, 6 and 7.
flowchart LR
  A["Input x"] --> B["Score s = w dot x + b"]
  B --> C["Multiply by label y"]
  C --> D{"y s at least 1?"}
  D -- "yes" --> E["Loss 0"]
  D -- "no" --> F["Loss 1 minus y s"]

The math

Definition: Hinge loss

For a label y∈{−1,+1}y \in \{-1, +1\} and a raw score ss, the hinge loss is

ℓ(y,s)=max⁡(0,  1−y s).\ell(y, s) = \max(0,\; 1 - y\, s).

For a linear classifier the score is s=w⋅x+bs = \mathbf{w} \cdot \mathbf{x} + b (a dot product plus a bias), from the linear classifier lesson, so the loss of one example is ℓ(y,w⋅x+b)\ell\big(y, \mathbf{w} \cdot \mathbf{x} + b\big).

Step 3 in symbols: the margin product

The product ysy s is positive exactly when the sign of the score matches the label. It is called the margin of the example. A margin of 11 or more means the point is correct and at least one unit beyond the boundary, measured in score units. Writing m=ysm = y s gives

ℓ=max⁡(0,  1−m).\ell = \max(0,\; 1 - m).

Step 4 and Step 6 in symbols: the two regions

The loss has two regions. When m≥1m \geq 1, the inner term 1−m1 - m is zero or negative, so the loss is 00. When m<1m \lt 1, the loss is 1−m1 - m, a straight line that falls as the margin grows. Taking the values from the walkthrough:

m=2⇒ℓ=0,m=0.4⇒ℓ=0.6,m=−2⇒ℓ=3.m = 2 \Rightarrow \ell = 0, \qquad m = 0.4 \Rightarrow \ell = 0.6, \qquad m = -2 \Rightarrow \ell = 3.

The loss is continuous. At m=1m = 1 both regions give 00. It is not smooth there, though: the slope changes from −1-1 to 00, a sharp corner.

Step 7 in symbols: the slope

Away from the corner, the derivative with respect to the score is

∂ℓ∂s={−yif ys<1,0if ys>1.\frac{\partial \ell}{\partial s} = \begin{cases} -y & \text{if } y s \lt 1, \\ 0 & \text{if } y s \gt 1. \end{cases}

Chaining this through s=w⋅x+bs = \mathbf{w} \cdot \mathbf{x} + b with the Chain Rule and the partial derivatives of the score gives ∇wℓ=−y x\nabla_{\mathbf{w}} \ell = -y\,\mathbf{x} for a misclassified or in-margin point, and 0\mathbf{0} otherwise. A point outside the margin does not move the weights at all.

The corner: subgradients

At ys=1y s = 1 there is no single derivative. The subgradient is the set of all slopes of lines that touch the graph from below at that point. Here it is the segment between 00 and −y-y. Gradient-based training picks any value in that segment; a common choice is 00.

The training objective

A linear SVM minimises the average hinge loss plus a penalty on the size of the weights, using L2 regularisation on the norm ∥w∥\lVert \mathbf{w} \rVert:

J(w,b)=λ2∥w∥2+1n∑i=1nmax⁡(0,  1−yi(w⋅xi+b)).J(\mathbf{w}, b) = \frac{\lambda}{2}\lVert \mathbf{w} \rVert^2 + \frac{1}{n} \sum_{i=1}^{n} \max\big(0,\; 1 - y_i(\mathbf{w} \cdot \mathbf{x}_i + b)\big).

The first term keeps w\mathbf{w} small, which keeps the boundary wide. The second term charges every point that is wrong or inside the margin. Because each term is a maximum of affine functions of (w,b)(\mathbf{w}, b), the whole objective is convex, so subgradient steps on it, as in Gradient Descent, approach the global minimum.

Worked examples

Example
1. Tiny: three scores with label +1

Use y=+1y = +1 and the loss max⁡(0,  1−s)\max(0,\; 1 - s).

  1. Score s=2s = 2. Then 1−2=−11 - 2 = -1, and max⁡(0,−1)=0\max(0, -1) = 0. The loss is 00.
  2. Score s=0.5s = 0.5. Then 1−0.5=0.51 - 0.5 = 0.5, and max⁡(0,0.5)=0.5\max(0, 0.5) = 0.5. The loss is 0.50.5.
  3. Score s=−1s = -1. Then 1−(−1)=21 - (-1) = 2, and max⁡(0,2)=2\max(0, 2) = 2. The loss is 22.
  4. Interpretation: the loss is 00 at and beyond the margin, and it grows as the score falls below 11, at a rate of one unit per unit of score.
Example
2. Standard: a mix of labels

Take three examples: (y,s)=(−1,0.3)(y, s) = (-1, 0.3), (−1,−2)(-1, -2) and (+1,1)(+1, 1).

  1. First example: ys=(−1)(0.3)=−0.3y s = (-1)(0.3) = -0.3. Then 1−(−0.3)=1.31 - (-0.3) = 1.3, so the loss is 1.31.3.
  2. Second example: ys=(−1)(−2)=2y s = (-1)(-2) = 2. Then 1−2=−11 - 2 = -1, so the loss is max⁡(0,−1)=0\max(0, -1) = 0.
  3. Third example: ys=(+1)(1)=1y s = (+1)(1) = 1. Then 1−1=01 - 1 = 0, so the loss is 00. It sits exactly on the margin.
  4. Average over the three: (1.3+0+0)/3≈0.433(1.3 + 0 + 0) / 3 \approx 0.433.
  5. Interpretation: the first example has the wrong sign, since its score is positive and its label is negative, so its loss is above 11. The other two cost nothing.
Example
3. Realistic: a spam filter with a margin

Reuse the spam filter from the Linear Classifier lesson: weights w=(0.5,1.0)\mathbf{w} = (0.5, 1.0) and bias b=−2b = -2, with features (count of "free", number of links). Label +1+1 means spam.

  1. Email A, spam (y=+1y = +1), with free =4= 4 and links =1= 1. The score is 0.5⋅4+1.0⋅1−2=10.5 \cdot 4 + 1.0 \cdot 1 - 2 = 1. Then ys=1y s = 1, and the loss is 00.
  2. Email B, spam (y=+1y = +1), with free =1= 1 and links =0= 0. The score is 0.5⋅1+0−2=−1.50.5 \cdot 1 + 0 - 2 = -1.5. Then ys=−1.5y s = -1.5, and the loss is 1−(−1.5)=2.51 - (-1.5) = 2.5.
  3. Email C, not spam (y=−1y = -1), with free =2= 2 and links =1= 1. The score is 1+1−2=01 + 1 - 2 = 0. Then ys=0y s = 0, and the loss is 11.
  4. Email D, not spam (y=−1y = -1), with free =0= 0 and links =0= 0. The score is −2-2. Then ys=2y s = 2, and the loss is max⁡(0,−1)=0\max(0, -1) = 0.
  5. Average over the four emails: (0+2.5+1+0)/4=0.875(0 + 2.5 + 1 + 0) / 4 = 0.875.
  6. Interpretation: email B is the costly one, a spam message the filter scored on the wrong side. Email C is on the boundary and pays the full margin cost. Email A is comfortably right, and email D is right with room to spare.

Try it: a fifth email, not spam (y=−1y = -1), has free =3= 3 and links =0= 0. What is its hinge loss? Answer: the score is 0.5⋅3+0−2=−0.50.5 \cdot 3 + 0 - 2 = -0.5. Then ys=(−1)(−0.5)=0.5y s = (-1)(-0.5) = 0.5, and the loss is 1−0.5=0.51 - 0.5 = 0.5.

Common mistakes and tradeoffs

It is tempting to think a correct prediction always costs nothing. In fact a correct score of 0.40.4 on a positive example still costs 0.60.6, because the hinge asks for a margin of 11, not merely the right sign. That demand is what makes an SVM choose a wide boundary rather than any boundary that separates the data.

It is tempting to think hinge loss simply counts mistakes. In fact counting mistakes, the zero-one loss that is 11 for an error and 00 otherwise, has zero slope almost everywhere, so gradient descent cannot learn from it. The hinge loss is never smaller than the zero-one loss at the same score, so it is a convex stand-in that still pushes the error count down.

It is tempting to use labels 00 and 11. In fact the formula 1−ys1 - y s assumes yy is −1-1 or +1+1. With 00 and 11 labels the product no longer means "correct side", and the loss gives nonsense.

It is tempting to think the kink at ys=1y s = 1 blocks training. In fact a subgradient exists there, and in practice the zero choice works well. Training does not stall on the corner.

When to use it. Use hinge loss for margin-based linear classifiers, and for any setting where you want the boundary to be determined by the points closest to it. Points far beyond the margin then have no influence on the solution.

When not to use it. Hinge loss gives scores, not probabilities. If you need calibrated probabilities, use cross-entropy with a logistic output. Hinge loss also grows only linearly, so a very wrong point pulls less hard than under a squared loss. The squared hinge, which squares the penalty, is a smooth alternative, but it grows faster and gives very wrong points more influence.

History

Vapnik and Chervonenkis described the optimal margin classifier in 1964, for data that can be split perfectly by a line. The problem was to choose, among all separating boundaries, the one with the widest gap to the nearest points.

In 1992 Boser, Guyon and Vapnik combined the margin idea with the kernel trick, which lets a linear method draw curved boundaries by working in a transformed space. This made the method practical on harder problems.

In 1995 Cortes and Vapnik introduced the soft-margin support vector network, which allows some points to fall inside the margin or on the wrong side, at a cost. That cost is the hinge loss. The name comes from the shape of its graph, which looks like the hinge of a door. Since then the hinge has spread to ranking, metric learning and other tasks where a margin is wanted.

Recap

  • A linear classifier produces a score s=w⋅x+bs = \mathbf{w} \cdot \mathbf{x} + b, and the label yy is −1-1 or +1+1.
  • The product ysy s measures how correct a prediction is and how far it sits from the boundary.
  • Hinge loss max⁡(0,1−ys)\max(0, 1 - y s) is zero beyond a margin of 1 and rises in a straight line inside it.
  • Adding an L2 penalty to the average hinge loss gives the support vector machine, which keeps the boundary as wide as the data allow.
  • Read next: Support Vector Machine for the full method, and Subgradient for the rule that makes training at the corner work.

Check yourself

QuestionRecall: which label values does hinge loss expect, and what does a loss of zero mean for one example?Show answer

Hinge loss expects labels y=−1y = -1 or y=+1y = +1. A loss of zero means ys≥1y s \geq 1: the score has the correct sign and is at least 1 unit beyond the boundary, so the example is outside the margin.

QuestionComputation: an example has label y=−1y = -1 and score s=−0.5s = -0.5. What is its hinge loss?Show answer

The product is ys=(−1)(−0.5)=0.5y s = (-1)(-0.5) = 0.5. The loss is max⁡(0,1−0.5)=0.5\max(0, 1 - 0.5) = 0.5. The score is on the correct side, but the example is inside the margin, so it still pays a cost.

QuestionWhy: a model with a correct score of s=0.8s = 0.8 on a positive example still has positive hinge loss. Why does the loss not stop at the sign?Show answer

The hinge asks for a margin of 1, not just a positive score. A score of 0.80.8 is correct but closer to the boundary than the required margin, so the loss is 1−0.8=0.21 - 0.8 = 0.2. Demanding a margin pushes the boundary away from the data, which is what makes the classifier generalise better than an arbitrary separating line.

QuestionApplied: after training an SVM with hinge loss, many training points have margins ysy s between 0 and 1. What does this tell you about those points?Show answer

They are correctly classified but inside the margin, so each one contributes a positive loss. In an SVM these are the points that shape the boundary, called support vectors. Points with a margin above 1 contribute nothing to the solution and could be removed without changing it.

Sources