ContentsChapter 6 · Kernels and SVMs3 of 3

6.3

The kernel trick

In this chapter
  1. 6.1Maximum margin
  2. 6.2Soft margin and the dual
  3. 6.3The kernel trick
Before you readOn a square, the opposite corners have the same label. No line separates the two classes. Could one extra feature make that possible?
Watch: Support Vector Machines: The Polynomial Kernel · StatQuest with Josh Starmer

Label the corners of the square (±1,±1)(\pm1,\pm1) by the sign of x1x2x_1x_2 The two diagonals meet, so no line puts their endpoints on opposite sides. Add the feature z=x1x2z=x_1x_2 Now the positive points sit at z=1z=1 the negative ones at z=−1z=-1 and the plane z=0z=0 separates them.

The circles (+1+1 and squares (−1-1 alternate. Lift each point to height z=x1x2z=x_1x_2 to separate them.

A feature map ϕ(x)\phi(x) turns an input into new coordinates. A linear boundary there can be curved or split into pieces in the original coordinates. Here the plane z=0z=0 means x1x2=0x_1x_2=0 both original axes form the boundary.

The dual SVM needs dot products, not the feature coordinates themselves. A kernel returns that dot product directly:

K(x,u)=ϕ(x)Tϕ(u).K(x,u)=\phi(x)^T\phi(u).

Replace every training dot product with K(xi,xj)K(x_i,x_j) then score a new point with f(x)=∑iαiyiK(xi,x)+bf(x)=\sum_i\alpha_i y_i K(x_i,x)+b This is the kernel trick: work as if the features had been expanded, without building every coordinate.

For example, the degree-two polynomial kernel K(x,u)=(xTu)2K(x,u)=(x^Tu)^2 in two dimensions expands to

x12u12+2x1x2u1u2+x22u22.x_1^2 u_1^2 + 2x_1x_2u_1u_2 + x_2^2 u_2^2.

That is the dot product of feature vectors with coordinates x12x_1^2 2x1x2\sqrt{2}x_1x_2 and x22x_2^2 The middle coordinate does the separating in the square. Another common choice, the RBF kernel, is K(x,u)=exp⁡(−γ∥x−u∥2)K(x,u)=\exp(-\gamma\|x-u\|^2) with γ>0\gamma>0 Its value is 11 at identical points and drops towards 00 with distance. Larger γ\gamma makes that drop quicker; scale the input features so their units do not dominate the distance.

Recall
What does a kernel return, and why is that enough for the dual SVM?
Recall
How does increasing γ\gamma change an RBF kernel?
Exercise
For x=(1,2)Tx=(1,2)^T and u=(3,−1)Tu=(3,-1)^T evaluate K(x,u)=(xTu)2K(x,u)=(x^Tu)^2 without expanding either vector into new features.
Exercise
In the square example, use f(x)=x1x2f(x)=x_1x_2 Give the class labels for (2,−1)(2,-1) and (−2,−1)(-2,-1) in that order.