Lecture 6: Support Vector Machines
Stanford CS229 lecture 6 notes: Support Vector Machines.
- cs229
- machine-learning
- course-notes
On this page
Why SVMs?#
We want a classifier that not only separates the data, but separates it with the widest possible margin. For a given dataset there are infinitely many lines that separate the classes. Some of them pass very close to a point, so a tiny bit of noise would flip the prediction. We prefer the line that stays as far away as possible from the points.
Notation#
For SVMs, we use labels (not 0 and 1), and write the classifier as:
Here is the intercept, and are the weights.
Margins#
Functional margin of a point:
If this is positive, the prediction is correct. The larger it is, the more confident we are.
Problem: you can scale and by 2 and the functional margin doubles, without the boundary actually changing. So it isn't a real measure of distance.
Geometric margin: the actual Euclidean distance from the point to the boundary:
This doesn't change if you rescale and .
The margin of the whole training set is the smallest margin among all points.
The Optimal Margin Classifier#
Goal: maximize the geometric margin. Because we can rescale freely, we can pick the scale so that the functional margin is exactly 1. Then maximizing is the same as minimizing :
This is a convex quadratic program. We can solve it with off-the-shelf software.
o | margin | x
o |<-------->| x
o o | / / / | x x
| / / / / | x
boundaryLagrange Duality#
To get to the really nice version of this, we need Lagrange multipliers. For the problem
we form the Lagrangian , with .
The key condition is complementary slackness (KKT): at the optimum, . So either the constraint is exactly tight (), or .
For the SVM, the constraints are tight only for the points sitting right on the margin. These points are called the support vectors. All other points have , which means they don't matter at all. You could delete them and get the same boundary.
Solving for and in the Lagrangian gives:
The weight vector is a linear combination of just the support vectors.
The Dual Problem#
Plugging back in, we get:
The important thing: the data only shows up as inner products . And to make predictions:
which also only depends on inner products, and only needs the support vectors since the rest have .
This is what makes the next lecture (kernels) possible.