Module 5

Support vector machines and kernels

Many lines can separate two groups of points. The support vector machine picks the one that stays as far as possible from both, and a single substitution, the kernel trick, lets the same method draw curves.

Why it matters

The SVM is the best understood classifier there is: its training problem is convex, so there is one best answer and no luck involved in finding it, and that answer has a geometric meaning you can see. It also introduces two ideas that outlive it. The hinge loss shows that what you choose to penalise decides which data points matter. And the kernel trick, comparing inputs through a similarity function instead of through explicit features, is the first appearance of an idea that returns when attention compares tokens by inner products in module 9.

The idea

The margin

Label the two classes $y = +1$ and $y = -1$, and classify with the sign of $f(x) = w^{\top} x + b$. The distance from a point $x$ to the boundary $f = 0$ is $|f(x)| / \lVert w \rVert$. Scaling $w$ and $b$ together does not move the boundary, so fix the scale by asking that the closest points satisfy $y\, f(x) = 1$. Those closest points then lie on the two lines $f = \pm 1$, and the gap between the lines, the margin, has width

$$\frac{2}{\lVert w \rVert} .$$

The widest margin is the smallest $\lVert w \rVert$. If the classes can be separated, the maximum-margin classifier solves

$$\min_{w,\, b}\; \tfrac{1}{2} \lVert w \rVert^2 \quad \text{subject to} \quad y_i\, f(x_i) \ge 1 \;\text{ for every } i .$$

The hinge loss

Real data overlap, so let points break the rule and charge them for it. The soft-margin SVM minimises

$$J(w, b) = \tfrac{1}{2} \lVert w \rVert^2 + C \sum_{i} \max\!\big(0,\; 1 - y_i\, f(x_i)\big) .$$

The second term is the hinge loss. A point on the correct side and outside the margin has $y_i f(x_i) \ge 1$ and costs nothing. Inside the margin or on the wrong side, the cost grows linearly. The constant $C$ sets the exchange rate between a wide margin and violations of it.

Support vectors

The hinge is flat wherever it is zero, so points outside the margin contribute nothing to the gradient. Differentiating $J$ where it is differentiable (and choosing a value at the kink, which makes this a subgradient):

$$\frac{\partial J}{\partial w} = w - C \!\!\sum_{i \,:\, y_i f(x_i) \lt 1}\!\! y_i\, x_i , \qquad \frac{\partial J}{\partial b} = - C \!\!\sum_{i \,:\, y_i f(x_i) \lt 1}\!\! y_i .$$

At the minimum these must vanish, so $w$ is a weighted sum of just the points on or inside the margin: the support vectors. Every other point could be moved, or deleted, without changing the classifier, as long as it stays outside the margin. Compare logistic regression, whose loss is never exactly zero and whose answer depends on every point.

The kernel trick

More generally, the best $w$ can always be written as a combination of the training points, $w = \sum_j \alpha_j x_j$. Substituting,

$$f(x) = \sum_j \alpha_j\, x_j^{\top} x + b, \qquad \lVert w \rVert^2 = \sum_{i,j} \alpha_i \alpha_j\, x_i^{\top} x_j .$$

The data now appear only inside inner products. So replace every $x_i^{\top} x_j$ by a kernel $K(x_i, x_j)$, a function that equals an inner product $\phi(x_i)^{\top} \phi(x_j)$ in some other feature space, and you have trained a linear SVM in that space without ever computing $\phi$. Writing $K$ also for the matrix of kernel values between training points, the problem becomes

$$J(\alpha, b) = \tfrac{1}{2}\, \alpha^{\top} K \alpha + C \sum_i \max\!\big(0,\; 1 - y_i\, f(x_i)\big), \qquad f(x) = \sum_j \alpha_j\, K(x_j, x) + b .$$

The most used kernel is the Gaussian or RBF kernel,

$$K(x, x') = \exp\!\left( - \frac{\lVert x - x' \rVert^2}{2 \sigma^2} \right),$$

whose feature space is infinite-dimensional. Each training point contributes a bump of width $\sigma$ to $f$. A large $\sigma$ gives smooth, almost linear boundaries; a small $\sigma$ lets the boundary wrap tightly around individual points.

Training

$J$ is convex in $(\alpha, b)$, so any minimum is the global one, but it has kinks, so plain gradient descent needs one adjustment: use a subgradient and let the step size shrink, typically like $1/\sqrt{t}$. That is what the demo and the exercise do. Production SVM libraries instead solve an equivalent constrained problem (the dual) with specialised algorithms, which is faster for large datasets and gives the same answer.

Live demo: push the margin around

Drag points, watch the margin and its support vectors

Thick line: the boundary f = 0. Thin lines: the margin, f = ±1.

class +1class −1support vector

Circled points are support vectors: those with $y_i f(x_i) \le 1$, on or inside the margin. The classifier is trained by subgradient descent on $J(\alpha, b)$ and restarts from its current answer whenever you change something, so the picture takes a moment to settle.

Try this

  1. Drag a point that is not circled, keeping it well away from the thin margin lines. Nothing moves: the classifier does not depend on it. Now drag a circled point and the whole boundary follows.
  2. Drag one orange point deep into the blue cluster. With C at 1 the margin tolerates it. Raise C to 3 and the SVM bends its line to reduce the violation, at the price of a narrower margin.
  3. Press A ring. No straight line separates these, and the linear SVM does little better than guessing. Switch to RBF kernel: same algorithm, same code path, and the boundary is a closed curve.
  4. With the ring and the RBF kernel, slide σ down to 0.2. Nearly every point becomes a support vector and the boundary shrink-wraps the data, the kernel version of overfitting. Slide it up to 2.5 and the boundary becomes smooth and round.

Exercise

A kernel SVM by subgradient descent

Write the RBF kernel and the subgradient of the kernel SVM objective $J(\alpha, b)$. The training loop, the objective and a ring-shaped dataset are provided. The tests compare your kernel with the definition, check your subgradient against finite differences, and then train on 80 points: the result must classify at least 97% of 200 fresh points correctly.

Check yourself

You delete a training point that is not a support vector and retrain. What happens to the boundary?

Nothing. A point outside the margin has zero hinge loss and a zero coefficient in $w$, so the minimum of $J$ is unchanged. Only points on or inside the margin determine the solution.

What do very small and very large values of $C$ do?

Small $C$ makes violations cheap, so the SVM prefers a wide margin and tolerates points inside it. Large $C$ makes violations expensive, so the margin narrows to avoid them, approaching the hard-margin classifier when the data are separable.

Why can the RBF kernel be used although its feature map $\phi$ is infinite-dimensional?

Because $\phi$ is never computed. Training and prediction need only the inner products $\phi(x)^{\top} \phi(x')$, and the kernel gives each one directly as a single exponential of a distance.

Where this leads

Kernels build in a notion of similarity chosen by hand. The rest of the course goes back to networks that learn their own. Module 6 starts with images, where one more piece of built-in structure, sharing the same small filter across every position, makes all the difference.

Sources: B. E. Boser, I. M. Guyon and V. N. Vapnik, “A training algorithm for optimal margin classifiers”, COLT (1992); C. Cortes and V. Vapnik, “Support-vector networks”, Machine Learning 20 (1995); B. Schölkopf and A. J. Smola, Learning with Kernels, MIT Press (2002); S. Shalev-Shwartz, Y. Singer, N. Srebro and A. Cotter, “Pegasos: primal estimated sub-gradient solver for SVM”, Mathematical Programming 127 (2011); C. M. Bishop, Pattern Recognition and Machine Learning, Springer (2006), ch. 7.