Skip to content
BIASED

Support vector machines

Among all separating hyperplanes, the widest band: hard and soft margin, hinge loss plus L2, what the dual says about each training point, the role of C, and why a wide margin generalizes.

Which separating line

The topic Classification metrics taught us to judge a trained classifier. It does not say which classifier to train. When the classes in the training set are separable, infinitely many hyperplanes have zero training error, and every metric on the training set calls them perfect. The logistic loss sends the weights to infinity on such data (see Logistic regression). The hinge loss drops to zero once every margin is at least 11 and stops telling the solutions apart. The support vector machine (SVM) answers with geometry: take the hyperplane that stays as far as possible from the nearest training points.

Classes in this topic are y∈{−1,+1}y \in \{-1, +1\}. Recall two quantities from Linear classifiers and surrogate losses. The distance from a point xx to the hyperplane ⟨w,x⟩+b=0\langle w, x \rangle + b = 0 is

dist⁡(x)=∣⟨w,x⟩+b∣∥w∥,\operatorname{dist}(x) = \frac{\lvert \langle w, x \rangle + b \rvert}{\lVert w \rVert},

and the margin of an object is Mi=yi(⟨w,xi⟩+b)M_i = y_i(\langle w, x_i \rangle + b). It is positive when the object is classified correctly. For a hyperplane that classifies every point correctly, ∣⟨w,xi⟩+b∣=Mi\lvert \langle w, x_i \rangle + b \rvert = M_i. So the margin of the hyperplane, its distance to the nearest training point, is

γ=min⁡iMi∥w∥.\gamma = \min_i \frac{M_i}{\lVert w \rVert}.

The running example comes from the topic Duality of the course Mathematics for ML. The negative points are A=(0,0)A = (0, 0) and B=(2,0)B = (2, 0), the positive ones C=(1,2)C = (1, 2) and D=(3,3)D = (3, 3). The line x2=0.2x_2 = 0.2 separates them without errors, but passes 0.20.2 from AA and BB. The line x2=1x_2 = 1 makes no errors either, and its nearest points, AA, BB and CC, are at distance 11. A new negative object just above AA lands on the wrong side of the first line and on the right side of the second.

The SVM idea is to maximize γ\gamma. The intuition: a decision that does not change when every training point is nudged a little is less tuned to the accidents of the sample. The formal argument comes in the last block.

The hard margin

Multiply ww and bb by the same positive number. The classifier does not change, and every MiM_i and ∥w∥\lVert w \rVert are multiplied by that number, so γ\gamma stays the same. The scale can therefore be fixed for free. The SVM fixes it so that the nearest points have Mi=1M_i = 1. Then every point satisfies Mi≥1M_i \ge 1, and the margin is γ=1/∥w∥\gamma = 1/\lVert w \rVert.

Between the hyperplanes ⟨w,x⟩+b=−1\langle w, x \rangle + b = -1 and ⟨w,x⟩+b=+1\langle w, x \rangle + b = +1 lies a band of width 2/∥w∥2/\lVert w \rVert with no training points inside. Maximizing the margin means minimizing ∥w∥\lVert w \rVert. Squaring it and adding a factor 12\tfrac12 makes the objective smooth:

min⁡w,b 12∥w∥2s.t.yi(⟨w,xi⟩+b)≥1,i=1,…,n.\min_{w, b}\ \tfrac12 \lVert w \rVert^2 \qquad \text{s.t.} \qquad y_i(\langle w, x_i \rangle + b) \ge 1, \quad i = 1, \dots, n.

This is the hard-margin SVM. The objective is strictly convex in ww and the constraints are linear. So on separable data the optimal ww is unique, and the problem is a quadratic program that standard solvers handle.

In the example the negative points fill the segment ABAB on the x1x_1 axis, and the nearest positive point to it is CC, at distance 22. No line can be farther than half of that from both classes. The line x2=1x_2 = 1 achieves it. In the SVM scale this is w=(0,1)w = (0, 1) and b=−1b = -1: MA=MB=MC=1M_A = M_B = M_C = 1, MD=2M_D = 2 and γ=1\gamma = 1. In the topic Duality the same answer came out as the perpendicular bisector of the shortest segment between the convex hulls of the classes. Here we need the dual for something else: it tells what role each training point plays.

Soft margin: slack and the hinge loss

Add two points to the example: a negative E=(0,1)E = (0, 1) and a positive F=(1,1)F = (1, 1). The classes are still separable, but barely. The hard margin now gives w=(2,4)w = (2, 4) and b=−5b = -5, and the band width drops to 2/20≈0.4472/\sqrt{20} \approx 0.447, 4.54.5 times narrower than before. Two points dictate the whole solution. If they are noise, say a customer with a wrong label, we would rather give them up. And when the classes overlap, no ww satisfies all the constraints Mi≥1M_i \ge 1.

The soft-margin SVM lets each point violate its constraint by a slack ξi≥0\xi_i \ge 0 and pays CC per unit of slack:

min⁡w,b,ξ 12∥w∥2+C∑i=1nξis.t.yi(⟨w,xi⟩+b)≥1−ξi,ξi≥0.\min_{w, b, \xi}\ \tfrac12 \lVert w \rVert^2 + C \sum_{i=1}^n \xi_i \qquad \text{s.t.} \qquad y_i(\langle w, x_i \rangle + b) \ge 1 - \xi_i, \quad \xi_i \ge 0.

With ξi=0\xi_i = 0 the point lies on its class's edge of the band or farther from the hyperplane. With 0<ξi<10 < \xi_i < 1 it is inside the band but still on its own side. With ξi=1\xi_i = 1 it is on the hyperplane itself, and with ξi>1\xi_i > 1 on the wrong side. Every error adds at least 11 to ∑iξi\sum_i \xi_i, so the total slack bounds the number of training errors from above.

For fixed ww and bb the best slack is the smallest feasible one, ξi=max⁡(0,1−Mi)\xi_i = \max(0, 1 - M_i). Substitute it, and the constraints disappear:

min⁡w,b C∑i=1nmax⁡(0, 1−yi(⟨w,xi⟩+b))+12∥w∥2.\min_{w, b}\ C \sum_{i=1}^n \max\big(0,\ 1 - y_i(\langle w, x_i \rangle + b)\big) + \tfrac12 \lVert w \rVert^2.

The first term is the hinge loss, the second the ridge penalty of Regularization. So the SVM is not a new kind of model but a linear classifier trained with a particular surrogate loss and an L2 penalty. Dividing by CnCn brings it to the familiar form "mean loss plus λ∥w∥2\lambda \lVert w \rVert^2": large CC means weak regularization.

The topic Linear classifiers and surrogate losses promised to derive what the hinge loss estimates. Let p=P(y=+1∣x)p = P(y = +1 \mid x). The expected loss of a score ff at the point xx is pmax⁡(0,1−f)+(1−p)max⁡(0,1+f)p \max(0, 1 - f) + (1 - p)\max(0, 1 + f). On [−1,1][-1, 1] it is the straight line 1+(1−2p)f1 + (1 - 2p) f, and outside that interval it only grows. The minimum is at f∗=+1f^* = +1 when p>1/2p > 1/2 and at f∗=−1f^* = -1 when p<1/2p < 1/2. That is the sign of the Bayes classifier and nothing more. SVM scores are not probabilities; when probabilities are needed, they are fitted on held-out data with Platt scaling from Logistic regression.

On the six points with C=1C = 1 the solution is w=(0.4,1.2)w = (0.4, 1.2), b=−1.8b = -1.8, with a band width of ≈1.58\approx 1.58. The band is 3.53.5 times wider than the hard one. The price is two points: EE falls inside the band with ξE=0.4\xi_E = 0.4, and FF ends up on the wrong side with ξF=1.2\xi_F = 1.2.

The dual with a ceiling C

The topic Duality derived the dual of the hard margin, and Lagrange multipliers and KKT conditions gave the optimality conditions. The soft margin adds one detail. Give a multiplier αi≥0\alpha_i \ge 0 to each margin constraint and a multiplier μi≥0\mu_i \ge 0 to each constraint ξi≥0\xi_i \ge 0:

L=12∥w∥2+C∑iξi+∑iαi(1−ξi−yi(⟨w,xi⟩+b))−∑iμiξi.\mathcal L = \tfrac12 \lVert w \rVert^2 + C \sum_i \xi_i + \sum_i \alpha_i \big(1 - \xi_i - y_i(\langle w, x_i \rangle + b)\big) - \sum_i \mu_i \xi_i.

Minimizing over ww and bb gives what it gave before: w=∑iαiyixiw = \sum_i \alpha_i y_i x_i and ∑iαiyi=0\sum_i \alpha_i y_i = 0. The new part is the derivative in ξi\xi_i:

∂L∂ξi=C−αi−μi=0.\frac{\partial \mathcal L}{\partial \xi_i} = C - \alpha_i - \mu_i = 0.

The Lagrangian is linear in ξi\xi_i, so with a nonzero slope the dual function is −∞-\infty. Hence μi=C−αi\mu_i = C - \alpha_i, and together with μi≥0\mu_i \ge 0 this caps αi\alpha_i from above. After substitution the terms with ξi\xi_i and bb vanish, and the dual problem remains:

max⁡α ∑iαi−12∑i,jαiαjyiyj⟨xi,xj⟩s.t.0≤αi≤C,∑iαiyi=0.\max_\alpha\ \sum_i \alpha_i - \tfrac12 \sum_{i,j} \alpha_i \alpha_j y_i y_j \langle x_i, x_j \rangle \qquad \text{s.t.} \qquad 0 \le \alpha_i \le C, \quad \sum_i \alpha_i y_i = 0.

It is the hard-margin dual with a ceiling CC added. The constraints are affine and the problem is feasible: w=0w = 0, b=0b = 0, ξi=1\xi_i = 1. So, as in Duality, strong duality holds: the primal and dual optima coincide, and on the six points with C=1C = 1 both equal 2.42.4. The classifier is recovered from α\alpha:

f(x)=∑iαiyi⟨xi,x⟩+b.f(x) = \sum_i \alpha_i y_i \langle x_i, x \rangle + b.

In scikit-learn these numbers can be read off directly. The field support_ holds the indices of the points with αi>0\alpha_i > 0 (grouped by class), dual_coef_ the products yiαiy_i \alpha_i, and coef_ the vector ww:

import numpy as np
from sklearn.svm import SVC
 
X = np.array([[0, 0], [2, 0], [1, 2], [3, 3], [0, 1], [1, 1]])
y = np.array([-1, -1, 1, 1, -1, 1])
m = SVC(kernel="linear", C=1).fit(X, y)
print(m.support_)
print(m.dual_coef_.round(3))
print(m.coef_.round(3), m.intercept_.round(3))
[1 4 2 5]
[[-0.6 -1.   0.6  1. ]]
[[0.4 1.2]] [-1.8]

Three kinds of points

What does the dual say about each point? The answer comes from complementary slackness, one condition per constraint:

αi(Mi−1+ξi)=0,(C−αi) ξi=0.\alpha_i \big(M_i - 1 + \xi_i\big) = 0, \qquad (C - \alpha_i)\, \xi_i = 0.

The second is μiξi=0\mu_i \xi_i = 0 after substituting μi=C−αi\mu_i = C - \alpha_i. Together they sort the training points into three groups.

  • αi=0\alpha_i = 0. Then μi=C>0\mu_i = C > 0, so ξi=0\xi_i = 0 and Mi≥1M_i \ge 1: the point is on the edge of the band or beyond it. It does not enter ww.
  • 0<αi<C0 < \alpha_i < C. Then μi>0\mu_i > 0 gives ξi=0\xi_i = 0, and αi>0\alpha_i > 0 gives Mi=1M_i = 1: the point lies exactly on an edge of the band. Such points give the bias. From yi(⟨w,xi⟩+b)=1y_i(\langle w, x_i \rangle + b) = 1 and yi2=1y_i^2 = 1 we get b=yi−⟨w,xi⟩b = y_i - \langle w, x_i \rangle; in practice this value is averaged over all such points.
  • αi=C\alpha_i = C. Then μi=0\mu_i = 0, the slack may be positive, and Mi=1−ξi≤1M_i = 1 - \xi_i \le 1: the point is on the edge, inside the band or on the wrong side.

The points with αi>0\alpha_i > 0 are called support vectors. Here are the six points with C=1C = 1:

point AA BB CC DD EE FF
αi\alpha_i 00 0.60.6 0.60.6 00 11 11
MiM_i 1.81.8 11 11 33 0.60.6 −0.2-0.2
ξi\xi_i 00 00 00 00 0.40.4 1.21.2

AA and DD are far beyond the edges of the band, and their multipliers are zero. BB and CC sit on the edges with α=0.6\alpha = 0.6. The point BB gives b=−1−0.8=−1.8b = -1 - 0.8 = -1.8, and CC gives 1−2.8=−1.81 - 2.8 = -1.8. EE and FF violate their constraints and hit the ceiling α=C\alpha = C.

The solution rests on the support vectors

The solution w=∑iαiyixiw = \sum_i \alpha_i y_i x_i is a weighted sum of the support vectors alone. This has three consequences.

The other points can be thrown away. Delete a point with αi=0\alpha_i = 0. The same ww, bb and remaining α\alpha still satisfy the KKT conditions, and for a convex problem that is enough. The solution does not change: without AA and DD we get the same w=(0.4,1.2)w = (0.4, 1.2) and b=−1.8b = -1.8. Deleting a support vector does change it. Without EE the answer becomes w=(0,1)w = (0, 1), b=−1b = -1: the line x2=1x_2 = 1 of the four-point example.

A leave-one-out bound. Leave-one-out is the kk-fold cross-validation of Validation and overfitting with k=nk = n. A point with αi=0\alpha_i = 0, when held out, is classified correctly: the model without it is the same and keeps it on the edge of the band or beyond. So the leave-one-out error is at most the fraction of support vectors. The fine print: the solver must return the same solution. And this bounds a procedure on samples of size n−1n - 1, not the risk of one final model.

The margin from the dual alone. With a hard margin, sum αi(Mi−1)=0\alpha_i(M_i - 1) = 0 over all points. This gives ∑iαiMi=∑iαi\sum_i \alpha_i M_i = \sum_i \alpha_i. The left side equals ⟨w,∑iαiyixi⟩+b∑iαiyi=∥w∥2\langle w, \sum_i \alpha_i y_i x_i \rangle + b \sum_i \alpha_i y_i = \lVert w \rVert^2, so

∑iαi=∥w∥2,γ=1∑iαi.\sum_i \alpha_i = \lVert w \rVert^2, \qquad \gamma = \frac{1}{\sqrt{\sum_i \alpha_i}}.

In the four-point example αA=αB=14\alpha_A = \alpha_B = \tfrac14, αC=12\alpha_C = \tfrac12, the sum is 11 and γ=1\gamma = 1.

Compare with logistic regression. There every training point enters the gradient with weight pi−yip_i - y_i, and none drops out. In the SVM each point's contribution to ww is capped by CC, and points beyond the band contribute nothing.

How C trades margin for errors

CC is the exchange rate between band width and violations. On the six points:

CC band width 2/∥w∥2/\lVert w \rVert support vectors of them with α=C\alpha = C ∑iξi\sum_i \xi_i training errors
0.10.1 4.064.06 66 44 3.133.13 11
11 1.581.58 44 22 1.601.60 11
1010 0.4470.447 33 11 00 00

At C=10C = 10 we already have the hard-margin solution, and larger CC do not change it. Here FF has αF=C\alpha_F = C and ξF=0\xi_F = 0: the ceiling also allows a point exactly on the edge. On separable data the hard margin is the limit C→∞C \to \infty. On overlapping data the hard-margin problem has no feasible point at all, and a large CC only makes the unavoidable slack more expensive.

Real data show where this ends. Take the breast cancer data from scikit-learn: 569 tumours, 30 standardized features, 398 objects for training and 171 for testing.

CC support vectors of them with α=C\alpha = C band width train accuracy test accuracy
0.0010.001 195195 189189 6.386.38 0.9400.940 0.9300.930
0.010.01 9292 8383 3.013.01 0.9800.980 0.9590.959
0.10.1 4444 3232 1.461.46 0.9920.992 0.9590.959
11 2727 1111 0.690.69 0.9920.992 0.9530.953
1010 2525 33 0.320.32 0.9950.995 0.9530.953
100100 2525 11 0.090.09 1.0001.000 0.9300.930

With small CC almost every support vector sits at the ceiling: the band is wide and many points are inside it. As CC grows the band narrows, the training accuracy reaches 11, and the test accuracy falls back to 0.9300.930. Large CC is weak regularization, and the model overfits, as with a small ridge penalty.

So CC is chosen by cross-validation on a logarithmic grid, as in Validation and overfitting. The band width is measured in the units of the features, so the features are standardized first. Otherwise the feature with the largest scale dominates ∥w∥\lVert w \rVert.

How this is solved in practice. SVC in scikit-learn is built on LIBSVM. Because of the constraint ∑iαiyi=0\sum_i \alpha_i y_i = 0, at least two multipliers must change at once, and SMO (sequential minimal optimization) repeatedly solves the problem for a pair of α\alpha in closed form. On millions of objects a linear SVM is better trained in the primal: hinge loss plus L2, stochastic subgradient steps, as in Stochastic and adaptive optimizers.

Why a wide margin generalizes, and what next

Why should a wide margin generalize? The first generalization bound came from the topic Inequalities and concentration of the course Probability Theory. For KK models the gap between the sample error and the true error grows like ln⁡K/n\sqrt{\ln K / n}. There are infinitely many hyperplanes, and the classical replacement for ln⁡K\ln K grows with the dimension dd. In high dimension the mere fact that the sample could be separated says almost nothing.

The margin removes the dimension. Suppose ∥x∥≤ρ\lVert x \rVert \le \rho for every object, and the distribution is separable with margin γ\gamma by a hyperplane through the origin. Then with probability at least 1−δ1 - \delta over a training sample of size nn, the 0–1 risk of the hard-margin SVM is at most (Shalev-Shwartz and Ben-David, 2014, Theorem 15.4)

4(ρ/γ)2n+2ln⁡(2/δ)n.\sqrt{\frac{4(\rho/\gamma)^2}{n}} + \sqrt{\frac{2 \ln(2/\delta)}{n}}.

The dimension does not appear, only the ratio of the data radius to the margin. With ρ/γ=10\rho/\gamma = 10, n=10,000n = 10{,}000 and δ=0.05\delta = 0.05 the bound is 0.200+0.027=0.2270.200 + 0.027 = 0.227. It is crude, like every bound of its kind, but it holds in a million dimensions as well as in two.

To sum up. Among the separating hyperplanes the SVM picks the widest band. The soft margin is the hinge loss plus an L2 penalty, with CC setting their balance. The dual sorts the points into three groups, and the solution rests on the support vectors alone.

But for any CC the boundary is still a hyperplane in the original features. XOR, four points that no line separates, is beyond it. Two facts of this topic point the way out. The dual and the classifier touch the data only through inner products ⟨xi,xj⟩\langle x_i, x_j \rangle. And the margin bound does not depend on the dimension. So we could map xx into a space of huge dimension, separate the data there with a wide margin, and compute nothing but inner products. That is the programme of the next topic, Kernels.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.