5.3 Support Vector Machines

DATA 442/642 · Week 5 · PDF · Slides

Margins, the dual, and the kernel trick.

These notes walk through the lecture slides. The original deck is unchanged; use (slides) on the course hub if you want that PDF.

The figures follow Deisenroth, Faisal, and Ong, Mathematics for Machine Learning, Theodoridis, and Géron, Hands-On Machine Learning.

1. The main idea

Think of open and filled circles as houses in two villages. A road between them should be as wide as possible and should demolish as few houses as possible. No sensible engineer picks the dash-dotted path in the figure.

A classifier should sit between the dense regions of the two classes, in a sparse belt, leaving the largest possible margin. That is the usual requirement for generalization: small error on points that were not in the training set.

Maximum-margin road between two villages


2. Linearly separable classes

If the classes are linearly separable, infinitely many hyperplanes classify the training set with zero error. From that family one can always keep those that satisfy

yn(θ⊤xn+θ0)≥1,n=1,…,N.

The SVM picks the one with smallest norm:

minimizeθ∈ℝl&‖θ‖22subject to&yn(θ⊤xn+θ0)≥1,n=1,…,N.

‖θ‖ is tied directly to the margin.


3. Primal, dual, and support vectors

The constrained problem is the primal. It has a dual. In general the dual only lower-bounds the primal; under convexity (and regularity of the constraints) the values match. SVM meets those conditions, so you may solve either.

The Lagrangian is

ℒ(θ,θ0,λ)=12‖θ‖2−∑n=1Nλn(yn(θ⊤xn+θ0)−1).

KKT stationarity gives

∇θℒ=0⇒θ^=∑n=1Nλnynxn.

and

∇θ0ℒ=0⇒∑n=1Nλnyn=0,λn(yn(θ⊤xn+θ0)−1)=0,λn≥0.

Plug that expansion into ℒ and solve the dual with a quadratic program:

minimizeλ&12∑n=1N∑m=1Nλnλmynymxn⊤xm−∑n=1Nλnsubject to&λn≥0,∑n=1Nλnyn=0.

With a dual solution λ^,

θ^=∑n=1Nsλnynxn,

where Ns is the number of nonzero multipliers. Only points that meet the inequality as an equality, yn(θ⊤xn+θ0)=1, have λn≠0. Those points are the support vectors. Recover θ^0 from any such equality.


4. Notes

  • The SVM solution is unique: the cost is strictly convex.
  • The dual is usually faster than the primal.
  • The dual is what makes the kernel trick possible. In an RKHS the predictor is
y^(x)=∑n=1Nsλnynκ(x,xn)+θ0.

5. Non-separable classes

When the classes overlap, allow slack:

minimizeθ∈ℝl&‖θ‖22+C∑n=1Nξnsubject to&yn(θ⊤xn+θ0)≥1−ξn,ξn≥0.

A margin error is yn(θ⊤xn+θ0)<1, i.e. ξn>0. If ξn=0 that point does not add to the cost. The optimizer tries to drive as many ξn as possible to zero.

The user parameter C trades the two terms. Large C: a small margin, fewer margin errors. Small C: the opposite.


6. Choice of hyper-parameters

C is almost always chosen by cross-validation on held-out data.

The other choice is the kernel. Different kernels, different performance. A Gaussian kernel needs enough training points to fill the input space, because κ(x,xn) matches x to the stored point xn. Training-set size matters.

Kernel Formula Typical use
Linear κ(x,x′)=x⊤x′ Linearly separable data, or many features relative to N
Polynomial κ(x,x′)=(x⊤x′+c)d Polynomial-shaped boundaries (e.g. some image tasks)
Gaussian RBF κ(x,x′)=exp(−‖x−x′‖2/(2σ2)) Complex nonlinear relations
Sigmoid κ(x,x′)=tanh(αx⊤x′+c) Periodic or sigmoid-like relations (e.g. some time series)

7. Practice

  1. Where do the support vectors appear in the SVM dual?

  2. What does a large C do to the margin and to margin errors?

  3. Why does the dual make the kernel trick possible?