TL;DR
A linear classifier computes a raw score for each input, and the sign of 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 , and a positive score means the class is . Each training example has a true label , which is or . The hinge loss needs one new quantity: the product .
- Take the first labelled example: , true label .
- Compute the score: . Check: the score is positive, so the classifier says , which is right.
- Compute the product . A positive product means the score is on the correct side. Its size says how far from the boundary the point sits.
- Apply the hinge: the loss is . The point clears the margin of 1, so it costs nothing.
- Take the second example: , . The score is . Check: .
- The product is . It is on the correct side, but only just, because is less than 1. The hinge gives . The point is right but too close to the boundary, so it pays a small price.
- Take the third example: , . The score is , which is on the wrong side. The product is , and the loss is .
- Flip the label of the first example to and keep . The score is still , but now and the loss is , the same as the wrong-side case above.
- 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.
- loss when y = +1
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
For a label and a raw score , the hinge loss is
For a linear classifier the score is (a dot product plus a bias), from the linear classifier lesson, so the loss of one example is .
Step 3 in symbols: the margin product
The product is positive exactly when the sign of the score matches the label. It is called the margin of the example. A margin of or more means the point is correct and at least one unit beyond the boundary, measured in score units. Writing gives
Step 4 and Step 6 in symbols: the two regions
The loss has two regions. When , the inner term is zero or negative, so the loss is . When , the loss is , a straight line that falls as the margin grows. Taking the values from the walkthrough:
The loss is continuous. At both regions give . It is not smooth there, though: the slope changes from to , a sharp corner.
Step 7 in symbols: the slope
Away from the corner, the derivative with respect to the score is
Chaining this through with the Chain Rule and the partial derivatives of the score gives for a misclassified or in-margin point, and otherwise. A point outside the margin does not move the weights at all.
The corner: subgradients
At 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 and . Gradient-based training picks any value in that segment; a common choice is .
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 :
The first term keeps 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 , the whole objective is convex, so subgradient steps on it, as in Gradient Descent, approach the global minimum.
Worked examples
Use and the loss .
- Score . Then , and . The loss is .
- Score . Then , and . The loss is .
- Score . Then , and . The loss is .
- Interpretation: the loss is at and beyond the margin, and it grows as the score falls below , at a rate of one unit per unit of score.
Take three examples: , and .
- First example: . Then , so the loss is .
- Second example: . Then , so the loss is .
- Third example: . Then , so the loss is . It sits exactly on the margin.
- Average over the three: .
- Interpretation: the first example has the wrong sign, since its score is positive and its label is negative, so its loss is above . The other two cost nothing.
Reuse the spam filter from the Linear Classifier lesson: weights and bias , with features (count of "free", number of links). Label means spam.
- Email A, spam (), with free and links . The score is . Then , and the loss is .
- Email B, spam (), with free and links . The score is . Then , and the loss is .
- Email C, not spam (), with free and links . The score is . Then , and the loss is .
- Email D, not spam (), with free and links . The score is . Then , and the loss is .
- Average over the four emails: .
- 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 (), has free and links . What is its hinge loss? Answer: the score is . Then , and the loss is .
Common mistakes and tradeoffs
It is tempting to think a correct prediction always costs nothing. In fact a correct score of on a positive example still costs , because the hinge asks for a margin of , 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 for an error and 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 and . In fact the formula assumes is or . With and labels the product no longer means "correct side", and the loss gives nonsense.
It is tempting to think the kink at 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 , and the label is or .
- The product measures how correct a prediction is and how far it sits from the boundary.
- Hinge loss 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 or . A loss of zero means : 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 and score . What is its hinge loss?Show answer
The product is . The loss is . 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 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 is correct but closer to the boundary than the required margin, so the loss is . 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 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
- Hinge loss (Wikipedia)
- Support vector machine (Wikipedia)
- Cortes and Vapnik, "Support-Vector Networks", Machine Learning 20 (1995): doi:10.1007/BF00994018