S14 · Lesson 33 · Support vector machines
The maximum-margin classifier and its canonical form, the margin as one over the weight norm, support vectors and the dual problem with kernels, soft margins with slack variables and the hinge loss, and how the SVM's loss compares with logistic regression's
When two classes can be separated by a line, infinitely many lines do it. The perceptron stops at whichever it reaches first; the support vector machine picks the one with the widest gap to the nearest points on each side. That choice has three payoffs: it generalizes well, the answer depends only on the few points at the edge of the gap (the support vectors), and the problem is convex with a dual form that uses the data only through inner products, so lesson 32's kernels slot straight in. Before deep learning, kernel SVMs were the strongest general-purpose classifiers available.
- Four losses as functions of the margin , the target (±1) times the classifier output: the 0–1 loss counts errors, the hinge loss max(0, 1 − m) is the SVM's, the logistic loss (in bits) is logistic regression's, and the squared loss is least squares'.
- The hinge loss is exactly zero once a point is beyond the margin, m ≥ 1, so well-classified points stop mattering: they aren't support vectors.
- The squared loss also punishes points that are "too correct", which is why least squares makes a poor classifier.
Predict
A linear SVM has = (3, 4). How wide is the gap between its two margin lines, ?
Each margin line is = 1/5 = 0.2 from the decision boundary (lesson 15's distance formula with an output of ±1), so the gap is = 0.4. Maximizing the margin means minimizing .
Name it
The hard margin
Scale w and b so the closest points satisfy (the canonical form). The margin is then , and the SVM solves
a convex quadratic program with a unique solution.
The dual and support vectors
With a Lagrange multiplier per constraint, and predictions are
Only points on the margin get : these are the support vectors, and the rest of the data could be deleted without changing the classifier. The bias follows from any support vector: .
Soft margins
For overlapping classes, slack variables let points violate the margin at a price:
At the optimum , the hinge loss, and the multipliers are capped at . Large C fits the training data hard; small C widens the margin and tolerates errors, acting as regularization.
Worked example
The training set is (1, 1) with t = +1 and (−1, −1) with t = −1. Find the SVM's weights, margin and multipliers.
Show the solution
- Use the symmetry. The boundary passes through the origin perpendicular to (1, 1), so b = 0 and .
- Scale w. The canonical condition gives c = 0.5, so
- Margin. and the margin is 1.414, the distance from the origin to each point.
- Multipliers. With , each multiplier is 0.25.
Answer w = (0.5, 0.5) and b = 0, a margin of 1.414, and multipliers of 0.25 each.
Worked example
Find the hinge and logistic losses for margins 0.3, −0.5 and 2.
Show the solution
- Hinge loss. gives 0.7, 1.5 and 0.
- Logistic loss. gives 0.800, 1.405 and 0.183 bits.
- Compare. Both grow linearly for badly misclassified points, but only the hinge loss reaches zero, which is what makes the SVM's solution sparse.
Answer the hinge losses are 0.7, 1.5 and 0, and the logistic losses 0.800, 1.405 and 0.183 bits.
Worked example
An RBF-kernel SVM trained on 2000 examples keeps 150 support vectors, on 64-dimensional inputs. Bound its leave-one-out error, and count the work per prediction.
Show the solution
- Which examples can be errors. Leaving out an example that isn't a support vector leaves the solution unchanged, and that example was classified correctly, so only support vectors can become leave-one-out errors.
- Bound.
- Work per prediction. Each prediction evaluates the kernel against every support vector: subtract-square-add steps, plus 150 exponentials.
- Fewer support vectors. They mean both a tighter bound and faster predictions.
Answer the leave-one-out error is at most 7.5%, and each prediction takes about 9600 subtract-square-add steps plus 150 exponentials.
Check
A canonical linear SVM has w = (3, 2). What's its margin?
Check
A point with target 1 gets output −0.8. What's its hinge loss?
Check
An SVM is trained on two points: (1, 1) labeled +1 and (−1, −1) labeled −1. What's its margin?
Check
A kernel SVM has two support vectors: one with t = +1, multiplier 0.3 and kernel value 0.5 with a new input, and one with t = −1, multiplier 2 and kernel value 0.9. The bias is −0.3. What's the output?
Check
A linear SVM has w = (1, 0.5), and (2, 0.5) is a support vector with label 1. What's the bias b?
Check
A soft-margin SVM has = 20, C = 0.1 and total slack 8. What's its objective?
Check
A soft-margin SVM has = 1 and C = 0.1. Three training points have margins = 2.5, −0.2 and 1.2.
(a)
What is their total slack?
(b)
What is the objective?
Check
A canonical linear SVM has weights (a, b) and bias c; consider a point (x, y).
Step 1
What is the SVM's output there?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the point's signed distance from the boundary?
Step 3
How wide is the margin?
Explain why
Why does an SVM's solution depend only on a few training points?
The SVM's constraints only bind for points that sit on or inside the margin. A point comfortably on the correct side has zero hinge loss, so moving it slightly, or deleting it, doesn't change the objective, and at the optimum its Lagrange multiplier is zero. The weights are a combination of the training points weighted by their multipliers, so only the points with nonzero multipliers, the support vectors at the edge of the gap or violating it, determine the boundary. That sparsity makes predictions cheaper, since the kernel sum runs over support vectors only, and it's a direct consequence of the hinge loss being exactly flat beyond the margin.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.