Skip to main content

Support Vector Machines

Source: Unit 2 §11

SVM is one of the most sophisticated yet simple-to-implement algorithms in machine learning. It is largely thought of as a linear classifier, and it is far more than that.

Myth-busting

MythReality
"Only a linear classifier"No - it handles non-linearity too, via kernels
"Only single or binary classification"No - it can do multi-class classification
"Only a classifier"No - it can do regression as well (SVR)
"Computationally expensive"No - with the right C and γ it works like a charm
"Math heavy and provable"Not a myth. It is math-heavy and fundamentally provable
FactsLinearly separable, formally

A data set is linearly separable if a hyperplane can separate the classes. In 2-D that hyperplane is a line, in 3-D a plane. Separable points typically form a convex hull.

The key insight: maximum margin

Many hyperplanes can separate the same data, so the question is which one is best. The SVM answer: the one that is farthest from the closest points of both classes.

Many hyperplanes separate the data. Which is best?widest margin: 48separates, margin 24separates, margin 27● class +1○ class −1
All three lines classify this training set perfectly. Only one of them leaves room for the next point to arrive slightly out of place.
Exam cueThe definition to hold on to

SVM is a maximum-margin classifier, described in the course as "one of the most elegant ideas in computer science". The support vectors are the closest data points to the boundary, and they alone define the hyperplane.

The three hyperplanes

2 / ‖w‖wperpendicular to the streetH1: w·x + b = +1H0: w·x + b = 0H2: w·x + b = −1support vector - the only points that matter
The street is as wide as the closest points allow. Only those points - the support vectors - touch the gutters, and only they determine where H0 sits.
PlaneEquationMeaning
H0w·x + b = 0Decision boundary, the "median" of the street; separates maximally
H1w·x + b = +1Upper margin - the closest positive points lie on it
H2w·x + b = −1Lower margin - the closest negative points lie on it
FactsTwo geometric facts about the street
  • ww is perpendicular to the street, exactly as it was perpendicular to the perceptron's boundary.
  • The distance between H1 and H2 is 2d=(d++d)2d = (d^{+} + d^{-}), the street width, and the whole optimisation exists to make it as large as possible.

Building the decision rule

To classify an unknown point uu, project uu onto ww with a dot product wuw \cdot u. The farther uu sits on the positive side of ww, the more likely it is a positive sample.

classify u as + if wu+b0, else \text{classify } u \text{ as } + \text{ if } w \cdot u + b \ge 0, \text{ else } -

Without loss of generality the rule can be hardened, insisting that no training point sits inside the street:

wxi+b+1  for all positive sampleswxi+b1  for all negative samplesw \cdot x_i + b \ge +1 \ \text{ for all positive samples} \qquad w \cdot x_i + b \le -1 \ \text{ for all negative samples}

The mathematical convenience yiy_i

Introduce a label variable for every instance: yi=+1y_i = +1 for positive samples and yi=1y_i = -1 for negative ones. Multiplying each of the two constraints by its own yiy_i flips the negative one round, and the pair collapses into a single inequality:

yi(wxi+b)1for all iy_i (w \cdot x_i + b) \ge 1 \qquad \text{for all } i

Rearranged, yi(wxi+b)10y_i(w \cdot x_i + b) - 1 \ge 0. For points on the gutter - the support vectors - it is exactly zero:

yi(wxi+b)1=0(support vectors)y_i (w \cdot x_i + b) - 1 = 0 \qquad \text{(support vectors)}

The quantity to maximise

Take a point x+x^{+} on the upper margin and xx^{-} on the lower margin. The street width is the projection of (x+x)(x^{+} - x^{-}) onto the unit vector w/ww / \lVert w \rVert:

width=(x+x)ww=2w\text{width} = (x^{+} - x^{-}) \cdot \frac{w}{\lVert w \rVert} = \frac{2}{\lVert w \rVert}
StepsWhere the 2 comes from
  1. x+x^{+} lies on H1, so wx++b=1w \cdot x^{+} + b = 1, giving wx+=1bw \cdot x^{+} = 1 - b.
  2. xx^{-} lies on H2, so wx+b=1w \cdot x^{-} + b = -1, giving wx=1bw \cdot x^{-} = -1 - b.
  3. Subtract: w(x+x)=(1b)(1b)=2w \cdot (x^{+} - x^{-}) = (1 - b) - (-1 - b) = 2.
  4. Divide by w\lVert w \rVert to turn the projection into a distance: width=2/w\text{width} = 2 / \lVert w \rVert.

Primal form

Maximising 2/w2/\lVert w \rVert is the same as minimising w\lVert w \rVert, which is the same as minimising 12w2\frac{1}{2}\lVert w \rVert^2:

min 12w2subject toyi(wxi+b)1i\min \ \tfrac{1}{2}\lVert w \rVert^2 \qquad \text{subject to} \qquad y_i (w \cdot x_i + b) \ge 1 \quad \forall i

That is the primal form. The 12w2\frac{1}{2}\lVert w \rVert^2 shape is a mathematical convenience for differentiation - the same trick as squaring the error in gradient descent.

Constrained optimisation and the Lagrangian

The Lagrange-multiplier detour. To maximise f(x1,x2)f(x_1, x_2) subject to g(x1,x2)=cg(x_1, x_2) = c, walk along the constraint curve. The optimum is the point where f\nabla f and g\nabla g align; any further move along the constraint decreases ff.

StepsThe Lagrange algorithm, applied to SVM
  1. Construct the Lagrangian, introducing one Lagrange multiplier αi\alpha_i per constraint - which here means one α\alpha per training instance.
  2. Compute LL, set the partial derivatives to 0, and solve.
L=12w2iαi[yi(wxi+b)1],αi0L = \tfrac{1}{2}\lVert w \rVert^2 - \sum_i \alpha_i \left[ y_i (w \cdot x_i + b) - 1 \right], \qquad \alpha_i \ge 0

The dual form

It is easier to solve the dual than the primal: instead of minimising over ww and bb, we maximise over the dual variables α\alpha.

Take the partial derivatives of LL with respect to ww and bb and set both to zero:

w=iαiyixiandiαiyi=0w = \sum_i \alpha_i y_i x_i \qquad \text{and} \qquad \sum_i \alpha_i y_i = 0
Exam cueWhat the first of those two says

The weight vector ww is a linear combination of the training inputs xix_i, their labels yiy_i and their multipliers αi\alpha_i. The model is literally built out of the training data, not merely fitted to it.

Substituting both back into LL eliminates ww and bb and leaves the Wolfe dual:

max iαi12ijαiαjyiyj(xixj)\max \ \sum_i \alpha_i - \tfrac{1}{2} \sum_i \sum_j \alpha_i \alpha_j y_i y_j (x_i \cdot x_j) subject toαi0andiαiyi=0\text{subject to} \quad \alpha_i \ge 0 \quad \text{and} \quad \sum_i \alpha_i y_i = 0
GotchaThe observation the whole next page depends on

The dual depends on the data only through the dot product xixjx_i \cdot x_j. Nothing else about xx appears anywhere. That single fact is what makes the kernel trick possible.

KKT conditions and the support vectors

FactsWhy only a few points matter
  • The KKT slack condition implies that for every point except the support vectors, αi=0\alpha_i = 0.
  • Only the support vectors have αi>0\alpha_i > 0, so only they contribute anything to w=iαiyixiw = \sum_i \alpha_i y_i x_i.
  • That is why an SVM is defined by a handful of points: deleting any non-support vector from the training set changes nothing at all.