ML Study Hub

Home / MLT / Week 10

Week 10 · Support vector machines

Official syllabus line: Support Vector Machines
About 100 minEnd Term material (see exam radar)BSCS2007 · Arun Rajkumar
Before you start: MLF week 9 (Lagrange multipliers, KKT, complementary slackness), MLT week 2 (kernels), MLT week 9 (linear classifiers).
Watch first: the animated story of this weekAbout 1-2 minutes of real 3D animation with a friendly voice. Pause any time.
Read the words of the video
  1. Imagine kids on a playground, boys on one side, girls on the other. You need to draw a line to separate them. Many lines could work! But which line is the very best? Think of this line as the middle of a road. We want the widest possible road that still keeps all the kids safely on their own sides. This idea is called a Support Vector Machine, aiming for the 'maximum margin'.
  2. This special road has two clear edges. Only the few kids standing right on these edges truly matter for defining the road! These important 'edge-standers' are called 'support vectors.' If you moved any other kid, the road would not change. The best separating line is found by making this road as wide as possible, or 'maximizing the margin'.
  3. Let's try a small example. Imagine two points on a number line: one at zero, labelled negative; another at two, labelled positive. The best separating line will be exactly in the middle, at one. The 'road' would have a width of two, with its edges at zero and two. Both original points become our support vectors, defining this perfect road.
  4. SVMs are super important for your exams! Remember, the margin is two divided by the weight vector's length. Support vectors are the points on the road's edge. A big 'C' means a narrow road, while a small 'C' means a wide road. And with cool tricks, SVMs can even find curved roads! Keep practicing, you've got this!

The one-minute story

Think of it like this

Two groups of kids stand on a playground, boys on the left, girls on the right, and you want to draw a line between them. Many lines work. Which one is best?

Think of the line as the centre of a road. A thin road might be squeezed against some child on either side; any wobble and that child is on the wrong side. The safest line is the one at the centre of the widest possible road that still has nobody standing on it. That is the support vector machine (SVM): maximum margin.

Here is the beautiful surprise. Only the kids standing right at the edges of the road matter. If you moved any child further back, or removed them, the road would not change. Those edge-standers are the support vectors, and they alone decide where the road goes. And thanks to the kernel trick from week 2, the same machine can draw curved roads.

By the end of this week you can:

  • state the hard-margin SVM problem and compute the margin;
  • explain the dual problem and the role of support vectors (via complementary slackness);
  • extend to the soft margin with slack variables and the parameter CC;
  • use the kernel trick to get non-linear boundaries and read hinge loss.

Words you need

Words you need
  • Margin - the width of the empty road between the two classes. A wider margin is more robust.
  • Support vectors - the training points that lie exactly on the edge of the road (or inside it, in the soft-margin case).
  • Hard margin - insist that nobody is inside the road: needs separable data.
  • Soft margin - allow a few points inside or across, at a price.
  • Slack variable ξi≥0\xi_i\ge0 - how far point ii violates the road.
  • CC - the price of violations. Large CC: strict. Small CC: relaxed.
  • Hinge loss - max⁡(0, 1−y f(x))\max(0,\,1-y\,f(\mathbf x)).

Idea 1: Maximising the margin

Labels yi∈{−1,+1}y_i\in\{-1,+1\} and a linear classifier f(x)=wTx+bf(\mathbf x)=\mathbf w^T\mathbf x+b. We scale w,b\mathbf w,b so that the closest points of each class satisfy ∣f(x)∣=1|f(\mathbf x)|=1. Then:

  • every training point obeys yi(wTxi+b)≥1y_i(\mathbf w^T\mathbf x_i+b)\ge1 (correct side, outside the road);
  • the two road edges are wTx+b=±1\mathbf w^T\mathbf x+b=\pm1, and the width of the road is
margin=2∥w∥\text{margin}=\frac2{\|\mathbf w\|}

Making the road as wide as possible means making ∥w∥\|\mathbf w\| as small as possible:

The idea

Hard-margin SVM (primal)

min⁡w,b 12∥w∥2subject toyi(wTxi+b)≥1  for all i\min_{\mathbf w,b}\ \frac12\|\mathbf w\|^2\qquad\text{subject to}\qquad y_i(\mathbf w^T\mathbf x_i+b)\ge1\ \text{ for all }i

This is a convex problem (quadratic objective, linear constraints) with a unique solution.

Try it yourself

Tilt and slide the line. The ringed points on the road's edges are the support vectors. The button finds the widest road.

Solved example: 1. A one-dimensional SVM

Question. One point x=0x=0 with y=−1y=-1 and one point x=2x=2 with y=+1y=+1. Find the maximum-margin classifier and its margin.

Solution. In 1-D, f(x)=wx+bf(x)=wx+b. We need −(w⋅0+b)≥1-(w\cdot0+b)\ge1, i.e. b≤−1b\le-1, and w⋅2+b≥1w\cdot2+b\ge1. To minimise 12w2\frac12w^2 take both constraints tight: b=−1b=-1 and 2w−1=1⇒w=12w-1=1\Rightarrow w=1. So f(x)=x−1f(x)=x-1, the boundary is at x=1x=1 (the midpoint, as you'd expect), and the margin is 2∣w∣=2\frac{2}{|w|}=\mathbf2, the distance between the points. Both points are support vectors.

Idea 2: The dual problem and support vectors

Using Lagrange multipliers αi≥0\alpha_i\ge0, one for each constraint (MLF week 9), the dual of the hard-margin SVM is

max⁡α ∑iαi−12∑i,jαiαjyiyj xiTxjs.t.αi≥0,  ∑iαiyi=0\max_{\boldsymbol\alpha}\ \sum_{i}\alpha_i-\frac12\sum_{i,j}\alpha_i\alpha_jy_iy_j\,\mathbf x_i^T\mathbf x_j\quad\text{s.t.}\quad\alpha_i\ge0,\ \ \sum_i\alpha_iy_i=0

and the weight vector is recovered as

w=∑iαiyixi\mathbf w=\sum_i\alpha_iy_i\mathbf x_i

KKT complementary slackness says αi[yi(wTxi+b)−1]=0\alpha_i\big[y_i(\mathbf w^T\mathbf x_i+b)-1\big]=0. So for any point strictly outside the road (yif(xi)>1y_if(\mathbf x_i)>1) we get αi=0\alpha_i=0: it plays no part in w\mathbf w. Only points on the road edges (yif(xi)=1y_if(\mathbf x_i)=1) can have αi>0\alpha_i>0. These are the support vectors.

Two consequences:

  1. The classifier depends on only a few points. Delete any non-support vector and nothing changes.
  2. The data enters the dual only through dot products xiTxj\mathbf x_i^T\mathbf x_j. Replace them by a kernel k(xi,xj)k(\mathbf x_i,\mathbf x_j) and you have a kernel SVM with a curved boundary, at no extra conceptual cost. Prediction is f(x)=∑iαiyi k(xi,x)+bf(\mathbf x)=\sum_i\alpha_iy_i\,k(\mathbf x_i,\mathbf x)+b, a sum over support vectors only.
Solved example: 2. Solving the dual for the 1-D example

Question. For the two points in Example 1 write and solve the dual.

Solution. The points: x1=0x_1=0 (y1=−1y_1=-1), x2=2x_2=2 (y2=+1y_2=+1). The constraint ∑αiyi=0\sum\alpha_iy_i=0 gives −α1+α2=0-\alpha_1+\alpha_2=0, so α1=α2=α\alpha_1=\alpha_2=\alpha. The double sum has only one non-zero term, i=j=2i=j=2: α2⋅(+1)(+1)⋅2⋅2=4α2\alpha^2\cdot(+1)(+1)\cdot2\cdot2=4\alpha^2 (every term involving x1=0x_1=0 vanishes). The dual objective is 2α−12(4α2)=2α−2α22\alpha-\frac12(4\alpha^2)=2\alpha-2\alpha^2. Its maximum: derivative 2−4α=0⇒α=0.52-4\alpha=0\Rightarrow\alpha=\mathbf{0.5}.

Then w=∑αiyixi=0.5(+1)(2)+0.5(−1)(0)=1w=\sum\alpha_iy_ix_i=0.5(+1)(2)+0.5(-1)(0)=1 ✓. And bb from a support vector: y2(w⋅2+b)=1⇒2+b=1⇒b=−1y_2(w\cdot2+b)=1\Rightarrow2+b=1\Rightarrow b=-1 ✓. It matches the primal.

Idea 3: The soft margin

Real data is rarely perfectly separable, or it is separable only by a skinny, fragile road. Allow violations with slack ξi≥0\xi_i\ge0:

The idea

Soft-margin SVM

min⁡w,b,ξ 12∥w∥2+C∑iξis.t.yi(wTxi+b)≥1−ξi,  ξi≥0\min_{\mathbf w,b,\boldsymbol\xi}\ \frac12\|\mathbf w\|^2+C\sum_i\xi_i\quad\text{s.t.}\quad y_i(\mathbf w^T\mathbf x_i+b)\ge1-\xi_i,\ \ \xi_i\ge0
  • ξi=0\xi_i=0: the point is correctly outside the road. 0<ξi<10<\xi_i<1: inside the road but on the right side. ξi>1\xi_i>1: on the wrong side (misclassified).
  • CC is the price of a violation. Large CC: violations are expensive, so the margin is narrow and few points violate it (low bias, risk of overfitting). Small CC: violations are cheap, so the margin is wide and many points may violate (more bias, underfitting risk).
  • In the dual the only change is the box constraint 0≤αi≤C0\le\alpha_i\le C. Support vectors are the points with αi>0\alpha_i>0: on the edge, inside the road, or misclassified.

Hinge loss view. Eliminating ξi\xi_i gives the unconstrained problem

min⁡ 12∥w∥2+C∑imax⁡(0,  1−yi(wTxi+b))\min\ \frac12\|\mathbf w\|^2+C\sum_i\max\big(0,\;1-y_i(\mathbf w^T\mathbf x_i+b)\big)

The hinge loss is 00 for points beyond the road edge and grows linearly as a point enters the road or crosses over. Compared with logistic loss (week 9), hinge loss goes exactly to zero for "safe" points, which is why those points drop out.

Idea 4: Non-linear SVMs

Use the kernel trick: the dual and the prediction only need k(xi,xj)k(\mathbf x_i,\mathbf x_j). With an RBF kernel the boundary can wrap around clusters. Choices: kernel type (linear, polynomial, RBF) and its parameters (degree, σ\sigma), plus CC, all chosen by cross-validation. A very small σ\sigma (very local RBF) lets the SVM memorise: overfits.

Your turn

Your turn: 1. Margin from w\mathbf w

A trained SVM has w=(3,4)\mathbf w=(3,4) and b=−2b=-2. What is the margin width? On which lines do the road edges lie?

Need a hint?

Margin =2/∥w∥=2/\|\mathbf w\|.

Show the full solution

∥w∥=5\|\mathbf w\|=5, so the margin is 25=0.4\frac25=\mathbf{0.4}. Edges: 3x1+4x2−2=+13x_1+4x_2-2=+1 and 3x1+4x2−2=−13x_1+4x_2-2=-1, i.e. 3x1+4x2=33x_1+4x_2=3 and =1=1.

Your turn: 2. Spot the support vectors

A hard-margin SVM gives, for five points, the values yif(xi)=1,  1.8,  1,  2.5,  3.2y_if(\mathbf x_i)=1,\;1.8,\;1,\;2.5,\;3.2. Which points can be support vectors?

Need a hint?

Support vectors lie exactly on the margin, yif=1y_if=1.

Show the full solution

Points 1 and 3. The others are strictly outside the road, so their αi=0\alpha_i=0.

Your turn: 3. Effect of CC

What happens to the margin width and to the number of training violations as CC increases?

Need a hint?

Large CC makes mistakes expensive.

Show the full solution

The margin gets narrower and the number of violations decreases (the model fits the training data more tightly and risks overfitting).

Your turn: 4. Hinge loss

For f(x)=0.4f(\mathbf x)=0.4 and y=+1y=+1, compute the hinge loss. And for f(x)=−0.5f(\mathbf x)=-0.5, y=+1y=+1? For f(x)=2f(\mathbf x)=2, y=+1y=+1?

Need a hint?

max⁡(0,1−yf)\max(0,1-yf).

Show the full solution

max⁡(0,0.6)=0.6\max(0,0.6)=\mathbf{0.6} (inside the road). max⁡(0,1+0.5)=1.5\max(0,1+0.5)=\mathbf{1.5} (wrong side). max⁡(0,1−2)=0\max(0,1-2)=\mathbf0 (safe).

Your turn: 5. True or false

(a) Removing a non-support vector changes the SVM solution. (b) The SVM dual depends on the data only through dot products. (c) The SVM objective is convex. (d) A larger CC always gives better test accuracy.

Need a hint?

Use complementary slackness.

Show the full solution

(a) False. (b) True. (c) True. (d) False, it can overfit.

Exam radar

Exam radar

In the MLT End Term, summaries mention Soft-Margin SVM in 36 of 49 papers, Hard-Margin SVM in 25, Support Vectors in 20, Dual Problem in 15, Support Vector Machines in 13, Kernel Trick in 16. SVMs are the single most mentioned topic in the End Term.

Typical items: margin =2/∥w∥=2/\|\mathbf w\|; "which points are support vectors?"; "what does a large CC do?"; "the dual constraint ∑αiyi=0\sum\alpha_iy_i=0"; "w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i"; hinge-loss values; the primal vs dual form; hard vs soft margin differences.

Watch out - real exam trap
  • Margin is 2∥w∥\frac{2}{\|\mathbf w\|}, not 1∥w∥\frac1{\|\mathbf w\|} (that is the distance from the boundary to one edge).
  • Large CC ⇒\Rightarrow narrow margin, small C⇒C\Rightarrow wide margin. Easy to flip.
  • Support vectors have αi>0\alpha_i>0; in the hard-margin case they lie exactly on yif=1y_if=1.
  • Hard margin needs linearly separable data; soft margin does not.
  • Don't forget the dual constraint ∑iαiyi=0\sum_i\alpha_iy_i=0.

Quick quiz

Quick quiz

The margin of a hard-margin SVM with weight vector w is...

  • ||w||
  • 1 / ||w||
  • 2 / ||w||
  • ||w|| squared
The two edges are at w.x + b = +1 and -1, so the gap is 2 / ||w||.

A support vector is a training point...

  • far from the boundary
  • on (or inside) the margin, with a non-zero dual weight
  • that is misclassified only
  • at the origin
Complementary slackness gives non-zero alpha only for points on the margin edge (or violating it in the soft case).

In the soft-margin SVM, increasing C...

  • widens the margin
  • penalises margin violations more, narrowing the margin
  • removes the slack variables
  • has no effect
A big C makes violations expensive.

Select all TRUE statements about the SVM. (select all that apply)

  • The solution depends only on the support vectors.
  • The dual involves only dot products between data points.
  • It needs the data to be linearly separable in the soft-margin case.
  • The primal problem is non-convex.
The soft margin handles non-separable data, and the problem is convex.

Recap

Remember this
  • Hard margin: min⁡12∥w∥2\min\frac12\|\mathbf w\|^2 s.t. yi(wTxi+b)≥1y_i(\mathbf w^T\mathbf x_i+b)\ge1. Margin =2/∥w∥=2/\|\mathbf w\|.
  • Dual: max⁡∑αi−12∑αiαjyiyjxiTxj\max\sum\alpha_i-\frac12\sum\alpha_i\alpha_jy_iy_j\mathbf x_i^T\mathbf x_j s.t. αi≥0\alpha_i\ge0, ∑αiyi=0\sum\alpha_iy_i=0; w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i. Complementary slackness ⇒\Rightarrow support vectors only.
  • Soft margin: add C∑ξiC\sum\xi_i; 0≤αi≤C0\le\alpha_i\le C; large CC = narrow margin, small CC = wide margin. Hinge loss max⁡(0,1−yf)\max(0,1-yf).
  • Kernel SVM: replace dot products with kk.
Formulas to keep
  • Primal: min⁡12∥w∥2+C∑ξi\min\frac12\|\mathbf w\|^2+C\sum\xi_i s.t. yi(wTxi+b)≥1−ξiy_i(\mathbf w^T\mathbf x_i+b)\ge1-\xi_i, ξi≥0\xi_i\ge0
  • Margin =2∥w∥=\frac{2}{\|\mathbf w\|}
  • Dual: max⁡∑αi−12∑ijαiαjyiyjk(xi,xj)\max\sum\alpha_i-\frac12\sum_{ij}\alpha_i\alpha_jy_iy_jk(\mathbf x_i,\mathbf x_j); 0≤αi≤C0\le\alpha_i\le C; ∑αiyi=0\sum\alpha_iy_i=0
  • w=∑αiyixi\mathbf w=\sum\alpha_iy_i\mathbf x_i; f(x)=∑αiyik(xi,x)+bf(\mathbf x)=\sum\alpha_iy_ik(\mathbf x_i,\mathbf x)+b
  • Hinge loss: max⁡(0,1−yf(x))\max(0,1-y f(\mathbf x))
Connected weeks (learn them together)

Practise with real exam questions

These come straight from past papers (with the real figures and the official answer key). Open 33 real questions on this week   All End Term papers