ECE571 / Week 5 · Support vector machines

Week 5 · Support vector machines

Support vector machines

Many lines separate two classes. The SVM picks the one with the widest gap to the nearest points, which turns out to be a clean optimization problem with a single best answer.

~35 min read 10 practice questions + 2 problems Reading: Géron Ch. 5 (incl. "Under the Hood"), Appendix C
The big picture

The perceptron stops at any line that separates the data. The support vector machine asks for the best one: the line whose distance to the closest training points (the margin) is as large as possible. With a scaling trick that becomes "minimize " subject to linear constraints, a convex problem with one global solution. Only the few points touching the margin, the support vectors, decide the answer. For data that isn't separable, a soft margin lets some points break the rules at a price , and the kernel trick finds curved boundaries by quietly working in a higher-dimensional space. The professor calls SVMs the go-to classifier before deep learning, and the first thing to try if you don't need a neural network.

After this you can
Section 1

Which line? The margin

Lecture 02:48–09:13Slides 2–5Géron Ch. 5, "Linear SVM Classification"

When data is linearly separable, infinitely many lines separate it, and the perceptron returns whichever it hits first. The professor's argument for picking a specific one is robustness. Real measurements are noisy. Picture each training point as a ball of uncertainty. How far could a point drift before it lands on the wrong side? Exactly its distance to the line. The line is only as safe as its closest point, so we want to maximize the minimum distance.

separates · margin 0.05 separates · margin 0.10 SVM · margin 1.18
Same 12 points, three separating lines. Each shaded street extends from the line to the nearest point on either side. All three lines classify every training point correctly, but the two on the left pass so close to a point that their streets are barely visible (margins 0.05 and 0.10). The SVM's street (right, solved exactly) is over ten times wider. The three circled points touch its edges: these are the support vectors.

Why a wide margin generalizes (slide 5)

Requiring a wide margin rules out most lines, so the model is more "constrained". Slide 5 says this lowers the VC dimension, a measure of how many patterns a model family can fit. Fewer possible fits means less room to fit noise, which means less overfitting. It's the same story as week 2.

Section 2

Distance from a point to a hyperplane

Lecture 09:13–16:52Slides 6–10

SVM notation keeps the bias separate: the hyperplane is with . (Last week we folded into ; here it must stay out, because the margin depends on alone.)

  1. is perpendicular to the hyperplane. Take two points on it. Both satisfy ; subtracting gives . So is orthogonal to every direction lying in the plane. It's the plane's normal vector.
  2. Project onto the normal. The distance from to the plane is the length of along the unit normal , for any on the plane: using .
wᵀx + b = 0 x′ x₀ w |wᵀx₀ + b| / ‖w‖
The distance is the projection of onto the unit normal. It doesn't matter which point on the line you start from; the projection is the same.
Worked example

Line , point . Then , , and . Distance .

Heads up: sign error on slide 13

Slide 13's blue box writes . That should be , as on slide 10 and in the derivation above.

Section 3

Hard-margin SVM

Lecture 16:52–18:31, 20:29–35:33Slides 11–17Géron Ch. 5, "Training Objective", "Quadratic Programming"

"Hard margin" means every training point must be classified correctly, so this assumes the data is linearly separable. With labels , "correct" is one inequality per point:

When a point is correctly classified, , so the absolute value in the distance can be dropped:

A max of a min looks hard. The professor calls the next step "a very important trick". Scaling by any gives the same hyperplane, because has the same solutions. So we're free to pick the scale, and we pick it so the closest point has

Maximizing is the same as minimizing , which is the same as minimizing (smooth, and the tidies the derivative):

This is a quadratic program (QP): a quadratic objective with linear inequality constraints. It's convex, so any solver finds the global optimum, not a local one. The professor highlights this as "another beauty of SVM". Off-the-shelf QP solvers exist in Python and MATLAB (slide 17).

Worked example: two points

Negative point , positive point . By symmetry the best line is the perpendicular bisector, so . The constraints:

: .   : .

Adding them gives , so . Minimizing means taking , and then .

Result: , , margin . That's exactly half the distance between the points, , as it should be.

Terminology: what "margin" means

In this course (and on the slides), the margin is the distance from the line to the nearest point: . Géron and many other sources talk about the whole "street", from one edge to the other: . Same optimization, different number. On an exam, say which one you mean.

Section 4

Support vectors

Lecture 07:10–09:13, 35:33–36:50Slide 18

The support vectors are the training points on the edges of the street, where the constraint holds with equality:

They "support" the separator: remove any other point and the solution doesn't change at all. Move a support vector and the line moves. In the figure above, 3 of 12 points decide everything.

Why the professor calls this the beauty of SVMs

It's automatic data selection. With a million training points and a simple class boundary, the trained SVM might depend on only a few hundred of them. It's also a contrast with week 4: k-NN keeps every training point to make predictions, while the SVM keeps just the support vectors.

Section 5

Soft margin and the C parameter

Lecture 18:31–20:21, 36:50–37:50, 40:18–49:04Slides 19–23Géron Ch. 5, "Soft Margin Classification"

Real data is rarely perfectly separable. One mislabeled point makes the hard-margin problem impossible, or forces a terrible narrow street. The fix is to give each point a slack variable , the amount by which it's allowed to violate the margin, and to charge for it:

point is on or outside its edge of the street (no violation)
inside the street but still on the correct side
misclassified
hyperparameter: how much each unit of violation costs

The professor names the two parts. depends only on the model, not the data, so it's the regularizer (week 11 is about these). depends on the data, so it's the loss. It's still a QP.

C = 0.05 margin 1.83 · 15 support vectors C = 100 margin 1.23 · 9 support vectors
The same overlapping data, solved with two values of C. Small C makes violations cheap: the street widens and many points sit inside it (all of them are support vectors). Large C makes violations expensive: the street narrows to squeeze points out. Both get 93% training accuracy here; the difference shows up on new data, which is why C is chosen by cross-validation.
Large CSmall C
Violationsexpensive → fewcheap → many
Marginnarrowwide
Limit: the hard-margin SVM: only matters, so
Riskoverfitting (chases noisy points)underfitting
Untangling the lecture (45:15–49:04)

This part of the lecture gets tangled ("bigger C, you allow smaller penalty..."). The clean version is the table above, which matches slide 23: large C → less tolerant of violations → smaller margin. C is a hyperparameter; pick it by cross-validation (week 2).

Section 6

The hinge-loss view

Lecture 32:42–33:20Slide 2 ("surrogate loss")Géron Ch. 5, "Online SVMs"

This isn't spelled out on the slides, but it connects the SVM to everything from week 3. At the optimum, each slack is as small as its constraint allows: . Substituting that in removes the constraints:

The term , with , is the hinge loss.

−2−10123 0123 t = y · f(x) (positive = correct side) hinge logistic 0–1
Three ways to score a prediction. The 0–1 loss (just "wrong or not") is what we care about, but it's flat, so gradients are useless. That was the perceptron's problem in week 3. Hinge loss and logistic loss are convex stand-ins ("surrogate losses", slide 2). Hinge loss is exactly zero once a point is safely past the margin (t ≥ 1), which is why those points drop out and only support vectors matter.

Because this version has no constraints, you can also train it with (sub)gradient descent. The professor mentions this at 32:48, and it's how Géron's SGDClassifier(loss="hinge") works.

Section 7

The dual problem

Lecture 44:04–44:52, 49:12–50:47Slide 24Géron Ch. 5, "The Dual Problem"; Appendix C
Flagged in lecture · 44:30 and 50:19: reading homework

"This is somehow like homework, the reading homework for you to understand so-called kernel SVM and the dual formula for the SVM. Please read the textbook." This section and the next are that reading, condensed.

Every constrained optimization problem (the primal) has a partner, the dual, built with one Lagrange multiplier per constraint. For the SVM the two have the same solution, so you can solve either. Setting the Lagrangian's derivatives to zero (Géron, Appendix C) gives two facts:

Substituting back gives the dual, which is again a QP, now in the 's:

(For the hard margin, drop the upper bound: .) A new point is classified by

  • Support vectors again: only for support vectors. Every other point has and vanishes from the sum. (This is "complementary slackness": either a constraint is tight or its multiplier is zero.)
  • Only dot products appear. The data shows up in training and prediction only through . That one fact is what makes the kernel trick work.
  • The dual is also faster when there are fewer training points than features (Géron).
Check with the two-point example

forces . Then , and matching gives for both points. Both are support vectors, as they must be.

Cross-check: Géron's Equation 5-6 is missing a constraint

Géron's dual (Eq. 5-6, and C-4 in Appendix C) lists only . His own derivation one step earlier (Eq. C-3) produces , and the dual needs it whenever there's a bias term . Without it, the problem is wrong. The version above includes it.

Section 8

The kernel trick

Lecture 37:50–40:08, 49:12–50:19Slides 20, 24Géron Ch. 5, "Nonlinear SVM Classification", "Kernelized SVMs"

Some data can't be split by any line. The slides' example is one class inside a circle and the other outside it. The fix is to map the data into a new space where it is linearly separable, and run a linear SVM there:

In 1-D: no single threshold works −303 ↓ map φ(x) = (x, x²) x² x² = 2.25
Adding the feature makes the data separable. In the new 2-D space a straight horizontal line splits the classes. Back in the original space, that line is the pair of thresholds . The slides' circle example works the same way with the feature .

The catch is that useful maps can produce a huge number of features. The kernel trick avoids ever computing them. Because the dual only needs dot products, replace every with a kernel that computes the dot product in the big space directly from the original vectors.

Worked example: a kernel is a shortcut

Take in 2-D. It equals for .

Check with , . Kernel: . Explicit: , , dot product . Same answer, without building .

KernelNotes
Linearthe plain linear SVM
Polynomialall feature products up to degree
Gaussian RBFthe lecture's example (38:00); an infinite-dimensional ; controls how local each point's influence is

The RBF kernel behaves a lot like a smarter k-NN: each support vector votes with a weight that fades with distance. Like C, the kernel's settings (, ) are hyperparameters to cross-validate.

Section 9

Using SVMs in practice

Lecture 00:12–01:19, 31:13–33:20Géron Ch. 5
  • Scale your features. The margin is a distance, so (as with k-NN) a feature measured in big units dominates. Géron shows this explicitly: standardize first.
  • Solvers: any QP solver for small problems (cvxopt in Python, quadprog in MATLAB); in practice scikit-learn's LinearSVC (linear, scales to big data) or SVC(kernel="rbf") (kernels; training time grows roughly between and in the number of samples, so it suits small to medium datasets).
  • Tune C (and for RBF) with cross-validation, usually on a log grid like 0.01, 0.1, 1, 10, 100.
  • No probabilities by default. An SVM outputs a side of the line, not a probability (unlike logistic regression).
  • Further reading the professor recommends: Boyd & Vandenberghe, Convex Optimization (already in the textbook folder) for QPs and duality (Ch. 4–5).
Section 10

Cheat sheet

Distance to hyperplane
Margin distance; after scaling (street width )
Hard margin s.t. ; convex QP
Support vectors (hard); ; only they matter
Soft margin s.t.
Clarge → narrow margin, few violations, overfit risk; = hard; pick by CV
Hinge loss; soft SVM = hinge
Dual, ,
Recover w; from any support vector
Kernel trickreplace by ; RBF
Section 11

Practice

Score: 0 / 0

Q1Among all lines that separate the training data, which one does a (hard-margin) SVM choose?

Q2Distance from to the line ?

Q3A trained hard-margin SVM has . What is the margin (line to nearest point)?

Q4You delete a training point that is far from the boundary (not a support vector) and retrain. The boundary:

Q5Increasing C in a soft-margin SVM generally:

Q6What happens as ?

Q7A point has . Its hinge loss is:

Q8In the dual solution, means point is:

Q9Why does the kernel trick need the dual form?

Q10Class A fills a disk; class B surrounds it. Best SVM approach?

Problem 1: Solve the hard-margin SVM for (0,0) labeled −1, (2,0) labeled +1 and (0,2) labeled +1. Give w, b, the margin, and the α's.

The data is symmetric in , so . Constraints:

(0,0): .   (2,0) and (0,2): .

We minimize , so we want as small as possible: is smallest with , giving .

, , margin . All three constraints hold with equality, so all three are support vectors.

α's: . And .

Checked with a QP solver (scikit-learn): w = (1, 1), b = −1, α = (1, 0.5, 0.5).

Problem 2: Show that for 2-D vectors equals with .

.

, which is the same three terms. ∎

The kernel costs one 2-D dot product and a square; the explicit map builds 3-D vectors. For degree in many dimensions, the gap becomes enormous.