We get to classify in very high dimensional spaces for almost no additional cost
Support vector machines are also called kernel machines
Some Kernels
Kernel
K(a,b)
Linear
a⊤b
Polynomial
(γa⊤b+r)d
Gaussian RBF
exp(−γ∥a−b∥2)
Sigmoid
tanh(γa⊤b+r)
Choosing the kernel (and γ, r, d) is part of choosing the model
A 10th-degree polynomial kernel corresponds to a huge feature space (every product of up to 10 features), but computing K is still just a dot product and a power
The RBF Kernel: Hills and Valleys
h(x)=∑α(i)t(i)e−γ∥x−x(i)∥2+b
Each support vector adds a bump: up for blue, down for red. The boundary is sea level, h=0.
Large γ: narrow bumps, a boundary that hugs every point (overfitting)
Small γ: broad bumps, close to a tilted plane (underfitting)
Nice Things About SVMs
Good generalization because of margin maximization
Not many hyperparameters to pick
No learning rate, no hidden layer sizes
Just C, the kernel, and its parameters (γ, d, ...)
No problems with local minima
What about SVM regression? It's possible, but we won't talk about it
The catch: the double sum is over every pair of training points. Training cost grows at least quadratically with m, so 100 million points is not practical.
Interactive figures are drawn by svm_demos.js (pure canvas, no libraries).
Every SVM in them is really trained, in the browser, by a small SMO solver
that matches sklearn's SVC. Build with
marp --html svm.md
--html is required: the demos are inline <div>s plus a <script>.
Bullets written with "*" unfold one at a time, and on demo slides each
reveal also advances the animation, so the clicker drives everything.
Mouse interaction (dragging, sliders, buttons) is extra and only affects
the window you use it in. Every 3D figure can be dragged to rotate.
Notation follows the reading (Geron, Appendix C): labels t in {-1, +1},
superscript (i) for instance i, m instances, alpha for the Lagrange
multipliers, zeta for slack, and the gamma / r / d kernel parameters.
The previous LibreOffice version of this deck is svm.odp / svm.pdf.
svm_3d.py (plotly) is the prototype for the "flattest plane" demo, and
svm_demos.py is the old matplotlib click-to-add-points demo.
We've spent weeks deep in neural networks. Today we drift back toward
more traditional ML. There are lots of formulas today, because one of
the two cool tricks only makes sense once you've seen what is being
optimized. You won't be tested on the minutiae of the formulas.
Idea 1 of 2: maximum margin. The data are samples from some underlying
distribution; keeping the boundary as far from all of them as possible
makes it least likely we put it in the wrong place.
In 2-D, three points typically pin the boundary down.
On the last step you can drag points around: the SVM retrains live. Drag
a non-support vector around (nothing happens) and then a support vector.
This is the same w^T x + b we used for linear regression and logistic
regression: one set of weights is one plane.
Multiplying by t flips the inequality for the negative class, which is
why we use -1/+1 labels here instead of 0/1.
This is the board drawing from last year. Click 2 shows a steep line
that satisfies every constraint, but where it crosses zero (the boundary)
is much closer to the red point than to the blue one. Click 3 lays it
down: the slope shrinks until the line bumps into one point from each
class and can't go any flatter. At that point the two distances under the
axis are equal: that's the maximum margin, 1/w on each side.
Q from class: "why do we want it flat?" Only because flattest forces the
zero crossing to the midpoint. The w slider and dragging the line show
that any steeper line has slack in where it can sit.
This replaces the old "Visualizing the Boundary" and "Two Boundaries"
slides: those were this picture seen from straight above.
Any w is a plane, exactly like linear regression. The dashed lines are
where the plane crosses +1 and -1; the green line is where it crosses 0.
Watch the readout while it flattens: ||w|| goes down and the margin
1/||w|| goes up. The gray band on the floor is the margin.
In 1-D it took two points to pin the line; here it takes three.
Squaring gets rid of the square root in ||w||: same minimizer, but smooth
and quadratic, which matters on the next slide. The 1/2 just makes the
derivative cleaner.
A numerical optimization course in the math department would cover these.
Unlike all the neural network loss surfaces we've been looking at, this
one has a unique minimum and we're guaranteed to find it.
Huge C: violations are very expensive. C = infinity is the hard margin
SVM, which has no solution at all if the data aren't separable.
Small C: we barely care, and happily trade violations for a wide margin.
No plane puts the stray blue point on the blue side, so the hard margin
SVM has no solution. C = 1000: the solver accepts the one unavoidable
violation (slack about 2.2: the point is misclassified, so its slack is
more than 1) and otherwise keeps every point outside the margin (1.11).
C = 0.1: margin 1.60, eight support vectors, a few small bars appear.
C = 0.01: margin 3.64, lots of slack, 16 of 17 points are support vectors.
Ringed points are support vectors: everything on or inside the margin.
The slider is log10 C. The activity's (2, 6) point is the same situation:
C = infinity has no solution, a finite C trades slack for margin.
Everybody hates constrained optimization. Each constraint becomes an
extra term in the function we optimize, and its multiplier becomes one
more parameter to solve for.
(The old slide had a stray 1/2 in front of the sum.)
"Dual" problem: guaranteed to have the same answer, expressed differently.
The solver works on this one. Written the way the reading writes it, as a
minimization; some sources negate it and maximize.
This second observation is the whole reason we went through the dual.
We don't need the data itself to do the optimization, only the dot
product between every pair of points. Hold that thought.
Idea 2 of 2 starts here. Slack is for the occasional stray point.
Here the classes are clean; the true boundary is just not a line, and
paying slack on five points would be papering over the real problem.
The curve is an RBF SVM, which is where we end up today.
SVMs can only ever draw linear boundaries.
But nothing stops us from adding features computed from the ones we have,
moving the data to a higher dimensional space where it is more likely to
be linearly separable.
Every point sits on the bowl z = x1^2 + x2^2. The SVM is trained on the
3-D points; three support vectors again. The green ring is where the
plane cuts the bowl; its shadow on the floor is the decision boundary in
the original 2-D space. Any plane cuts this bowl in a circle, so with
this feature map the SVM can draw any circle (or a line).
Table discussion. Answer: the double sum is already quadratic in the
number of training points, and each of those m^2 dot products now costs
time proportional to the new dimensionality. Going from 2 to 10,000
dimensions makes every one of them thousands of times slower (and we
have to compute and store all the phi vectors).
Through the power of math and magic, a cheap function of the two
low-dimensional vectors gives exactly the number we'd get by projecting
into the big space and taking the dot product there. How to find these
functions is beyond this class (Mercer's theorem). The activity has a
6-D version of this example: (a^T b + 1)^2.
We never compute w in the big space (it might not even be finite), but we
never need it: h(x) is w^T phi(x) + b written with K.
1998: kernel methods were what deep learning is now. Reams of papers on
"kernelizing" every algorithm in sight.
Notation now matches the reading. The old slides wrote the RBF kernel
with sigma: gamma = 1 / (2 sigma^2). The reading has a nice discussion
of when to use which kernel.
The RBF kernel's feature space is infinite dimensional.
Step 0 is the usual 2-D picture: decision regions, the boundary in green,
margins dashed. Click 1 tips it up: the same picture is a landscape.
Compare with the linear SVM: there the landscape was a plane.
Each point sits on the surface at height h(x); support vectors are ringed.
gamma = 5: 26 of 32 points are support vectors, each with its own spike.
gamma = 0.05: almost a plane, and several points end up on the wrong side.
C = 10 throughout. Slider is log10 gamma.
SVMs are still useful, which is why we're covering them: small to medium
data sets, lots of features, little tuning.