vrishank

← Blog

Lecture 7: Kernels

Stanford CS229 lecture 7 notes: Kernels.

· 2 min read

  • cs229
  • machine-learning
  • course-notes
On this page

The motivation#

Linear classifiers can only draw straight boundaries. What if the data isn't linearly separable?

One idea: map the inputs to a bigger feature space where it is separable. For example, from xx to ϕ(x)=[x,x2,x3]\phi(x) = [x, x^2, x^3]. A linear boundary in the new space is a curved boundary in the original.

The issue is that if ϕ(x)\phi(x) has a million (or infinitely many) dimensions, computing it is a nightmare.

The kernel trick#

We saw that the SVM only ever needs inner products ⟨x(i),x(j)⟩\langle x^{(i)}, x^{(j)} \rangle. So, replace them with ⟨ϕ(x(i)),ϕ(x(j))⟩\langle \phi(x^{(i)}), \phi(x^{(j)}) \rangle.

Define the kernel:

K(x,z)=ϕ(x)Tϕ(z)K(x, z) = \phi(x)^T \phi(z)

The trick: for many choices of ϕ\phi, we can compute K(x,z)K(x, z) without ever computing ϕ(x)\phi(x).

Example: K(x,z)=(xTz)2K(x, z) = (x^T z)^2. Expanding it gives ∑i,j(xixj)(zizj)\sum_{i,j} (x_i x_j)(z_i z_j). This corresponds to a feature map ϕ\phi with all n2n^2 pairwise products xixjx_i x_j. Computing ϕ\phi explicitly takes O(n2)O(n^2), but computing (xTz)2(x^T z)^2 takes just O(n)O(n).

The recipe:

  1. Write the algorithm in terms of inner products ⟨x,z⟩\langle x, z \rangle only.
  2. Pick a kernel K(x,z)K(x, z).
  3. Replace every inner product with K(x,z)K(x, z).

Common kernels#

  • Linear: K(x,z)=xTzK(x, z) = x^T z (just regular SVM)
  • Polynomial: K(x,z)=(xTz+c)dK(x, z) = (x^T z + c)^d (features: all monomials up to degree dd)
  • Gaussian (RBF): K(x,z)=exp⁡(−∥x−z∥22σ2)K(x, z) = \exp\left(-\frac{\|x - z\|^2}{2\sigma^2}\right)

The Gaussian kernel corresponds to an infinite dimensional feature space. We can still compute it in O(n)O(n).

Intuition: a kernel measures similarity. K(x,z)K(x,z) is large if xx and zz are similar (close together), and small if not.

Which functions are valid kernels?#

A function KK is a valid kernel (it corresponds to some ϕ\phi) if and only if the matrix Kij=K(x(i),x(j))K_{ij} = K(x^{(i)}, x^{(j)}) is symmetric positive semi-definite for any set of points. This is Mercer's theorem.

Kernels beyond SVMs#

The trick isn't SVM specific. Anything that can be written in terms of inner products can be kernelized: linear regression, logistic regression, perceptron, PCA, and so on.

Soft margin SVM (dealing with noise)#

What if the data isn't separable, even in feature space? Or one outlier would force a tiny margin?

We allow some points to violate the margin, but pay a price:

min⁡w,b,ξ  12∥w∥2+C∑i=1mξis.t.y(i)(wTx(i)+b)≥1−ξi,  ξi≥0\min_{w,b,\xi} \; \frac{1}{2}\|w\|^2 + C\sum_{i=1}^{m}\xi_i \quad \text{s.t.} \quad y^{(i)}(w^T x^{(i)} + b) \geq 1 - \xi_i, \; \xi_i \geq 0

The ξi\xi_i are slack variables.

  • Big CC: violations are expensive, so you get a narrow margin that fits training data tightly (risk of overfitting).
  • Small CC: tolerant to violations, wider margin (smoother, risk of underfitting).

The dual looks almost identical to before, except the constraint becomes 0≤αi≤C0 \leq \alpha_i \leq C.

Solving it: SMO#

The dual is a big quadratic program with mm variables. Sequential Minimal Optimization (SMO) solves it by repeatedly picking just two α\alpha's, and optimizing them while keeping all others fixed. (Two, because the constraint ∑αiy(i)=0\sum \alpha_i y^{(i)} = 0 means you can't change just one.) Each step has a closed form solution, so it's really fast.