S1433 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 m=ty, 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 (1−m)2 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, 𝐰T𝐱+b=±1?

Name it

The hard margin

Scale w and b so the closest points satisfy tn(𝐰T𝝓(𝐱n)+b)=1 (the canonical form). The margin is then 1/‖𝐰‖, and the SVM solves

min𝐰,b⁡12‖𝐰‖2subject totn(𝐰T𝝓(𝐱n)+b)≥1

a convex quadratic program with a unique solution.

The dual and support vectors

With a Lagrange multiplier an≥0 per constraint, 𝐰=∑nantn𝝓(𝐱n) and predictions are

y(𝐱)=∑nantnk(𝐱,𝐱n)+b

Only points on the margin get an>0: 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: b=ts−∑nantnk(𝐱s,𝐱n).

Soft margins

For overlapping classes, slack variables ξn≥0 let points violate the margin at a price:

min⁡12‖𝐰‖2+C∑nξn,tnyn≥1−ξn

At the optimum ξn=max⁡(0,1−tnyn), the hinge loss, and the multipliers are capped at 0≤an≤C. 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
  1. Use the symmetry. The boundary passes through the origin perpendicular to (1, 1), so b = 0 and 𝐰=c(1,1).
  2. Scale w. The canonical condition 𝐰T(1,1)=1 gives c = 0.5, so 𝐰=(0.5,0.5)
  3. Margin. ‖𝐰‖=0.707 and the margin is 1.414, the distance from the origin to each point.
  4. Multipliers. 𝐰=a1(1,1)+a2(−1)(−1,−1)=(a1+a2)(1,1) With a1=a2, 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
  1. Hinge loss. max⁡(0,1−m) gives 0.7, 1.5 and 0.
  2. Logistic loss. log2⁡(1+e−m) gives 0.800, 1.405 and 0.183 bits.
  3. 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
  1. 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.
  2. Bound. error≤150/2000=7.5%
  3. Work per prediction. Each prediction evaluates the kernel against every support vector: 150×64=9600 subtract-square-add steps, plus 150 exponentials.
  4. 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 ‖𝐰‖2 = 20, C = 0.1 and total slack 8. What's its objective?

Check

A soft-margin SVM has ‖𝐰‖2 = 1 and C = 0.1. Three training points have margins tnyn = 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°.

Explain why

Why does an SVM's solution depend only on a few training points?

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.