Skip to content
Ahmed Haroon
← Machine Learning

Kernel Methods

Introduction

A linear model makes predictions using:

θTx \theta^T x

For squared-error regression, a batch gradient descent update is:

θ←θ+α∑i=1n(y(i)−θTx(i))x(i) \theta \leftarrow \theta + \alpha \sum_{i=1}^{n} \left( y^{(i)} - \theta^T x^{(i)} \right) x^{(i)}

The problem is that a linear model can only represent linear relationships in the features we give it.

If the relationship is nonlinear, we can create new features using a feature map:

ϕ(x) \phi(x)

Instead of using xx directly, we replace it with ϕ(x)\phi(x). The update becomes:

θ←θ+α∑i=1n(y(i)−θTϕ(x(i)))ϕ(x(i)) \theta \leftarrow \theta + \alpha \sum_{i=1}^{n} \left( y^{(i)} - \theta^T\phi(x^{(i)}) \right) \phi(x^{(i)})

The model is still linear in the transformed features ϕ(x)\phi(x), but it can now represent a nonlinear relationship in the original input space.

The same idea applies to classification.

Feature map turning a circular decision boundary in input space into a straight line in feature space

A nonlinear boundary in the original input space can become a linear boundary after applying the feature map ϕ\phi.

For example, mapping (x1,x2)(x_1,x_2) to (x12,x22)(x_1^2,x_2^2) turns a boundary of the form

x12+x22=c x_1^2+x_2^2=c

into a linear boundary in the transformed feature space.

The drawback is that the transformed feature vector can become very large.

For example, suppose ϕ(x)\phi(x) contains all ordered products of the original features up to degree 33. The number of features is:

1+d+d2+d3=Θ(d3) 1+d+d^2+d^3 = \Theta(d^3)

So explicitly constructing ϕ(x)\phi(x) and storing θ\theta can require Θ(d3)\Theta(d^3) values.

The Kernel Trick

The key observation is that gradient descent never needs θ\theta to point outside the span of the transformed training examples.

If we start with:

θ0=0 \theta_0=0

then every update adds a linear combination of the vectors ϕ(x(i))\phi(x^{(i)}). Therefore, every later value of θ\theta can be written as:

θ=∑i=1nβiϕ(x(i)) \theta = \sum_{i=1}^{n} \beta_i \phi(x^{(i)})

Instead of storing θ\theta directly, we can therefore store the coefficients βi\beta_i.

Substituting this representation into the gradient update gives an update for those coefficients.

See derivation
θ←θ+α∑i=1n(y(i)−θTϕ(x(i)))ϕ(x(i)) \theta \leftarrow \theta + \alpha \sum_{i=1}^{n} \left( y^{(i)} - \theta^T\phi(x^{(i)}) \right) \phi(x^{(i)}) =∑i=1nβiϕ(x(i))+α∑i=1n(y(i)−θTϕ(x(i)))ϕ(x(i)) = \sum_{i=1}^{n} \beta_i\phi(x^{(i)}) + \alpha \sum_{i=1}^{n} \left( y^{(i)} - \theta^T\phi(x^{(i)}) \right) \phi(x^{(i)})
=∑i=1n[βi+α(y(i)−θTϕ(x(i)))]ϕ(x(i)) = \sum_{i=1}^{n} \left[ \beta_i + \alpha \left( y^{(i)} - \theta^T\phi(x^{(i)}) \right) \right] \phi(x^{(i)})

Therefore:

βi←βi+α(y(i)−θTϕ(x(i))) \beta_i \leftarrow \beta_i + \alpha \left( y^{(i)} - \theta^T\phi(x^{(i)}) \right)

For a batch gradient step, all βi\beta_i are updated simultaneously using the current parameter values.

Now substitute:

θ=∑j=1nβjϕ(x(j)) \theta = \sum_{j=1}^{n} \beta_j\phi(x^{(j)})

into the prediction term.

See derivation
θTϕ(x(i))=(∑j=1nβjϕ(x(j)))Tϕ(x(i)) \theta^T\phi(x^{(i)}) = \left( \sum_{j=1}^{n} \beta_j\phi(x^{(j)}) \right)^T \phi(x^{(i)})
=∑j=1nβjϕ(x(j))Tϕ(x(i)) = \sum_{j=1}^{n} \beta_j \phi(x^{(j)})^T \phi(x^{(i)})

This is the important result: the algorithm only needs inner products between transformed examples.

We define a kernel as this feature-space inner product:

K(x,z)=ϕ(x)Tϕ(z)=⟨ϕ(x),ϕ(z)⟩ K(x,z) = \phi(x)^T\phi(z) = \left\langle \phi(x), \phi(z) \right\rangle

If K(x,z)K(x,z) can be computed directly from xx and zz, we never need to explicitly construct ϕ(x)\phi(x).

This is the kernel trick.

The coefficient update becomes:

βi←βi+α(y(i)−∑j=1nβjK(x(i),x(j))) \beta_i \leftarrow \beta_i + \alpha \left( y^{(i)} - \sum_{j=1}^{n} \beta_j K(x^{(i)},x^{(j)}) \right)

For a fixed training set, we can store all pairwise kernel values in the Gram matrix:

Kij=K(x(i),x(j)) K_{ij} = K(x^{(i)},x^{(j)})

This avoids recomputing the same kernel values on every iteration.

A feature-map example

Suppose our feature map contains all ordered products of degrees 00 through 33.

Computing the inner product explicitly would require Θ(d3)\Theta(d^3) work. However, the same inner product can be simplified to a function of the original dot product ⟨x,z⟩\langle x,z\rangle.

See derivation
⟨ϕ(x),ϕ(z)⟩=1+∑i=1dxizi+∑i,jxixjzizj+∑i,j,kxixjxkzizjzk \left\langle \phi(x), \phi(z) \right\rangle = 1 + \sum_{i=1}^{d}x_i z_i + \sum_{i,j} x_i x_j z_i z_j + \sum_{i,j,k} x_i x_j x_k z_i z_j z_k
=1+∑i=1dxizi+(∑i=1dxizi)(∑j=1dxjzj)+∑i,j,kxixjxkzizjzk = 1 + \sum_{i=1}^{d}x_i z_i + \left( \sum_{i=1}^{d}x_i z_i \right) \left( \sum_{j=1}^{d}x_j z_j \right) + \sum_{i,j,k} x_i x_j x_k z_i z_j z_k
=1+∑i=1dxizi+(∑i=1dxizi)2+(∑i=1dxizi)(∑j=1dxjzj)(∑k=1dxkzk) = 1 + \sum_{i=1}^{d}x_i z_i + \left( \sum_{i=1}^{d}x_i z_i \right)^2 + \left( \sum_{i=1}^{d}x_i z_i \right) \left( \sum_{j=1}^{d}x_j z_j \right) \left( \sum_{k=1}^{d}x_k z_k \right)
=1+∑i=1dxizi+(∑i=1dxizi)2+(∑i=1dxizi)3 = 1 + \sum_{i=1}^{d}x_i z_i + \left( \sum_{i=1}^{d}x_i z_i \right)^2 + \left( \sum_{i=1}^{d}x_i z_i \right)^3 =1+⟨x,z⟩+⟨x,z⟩2+⟨x,z⟩3 = 1 + \langle x,z\rangle + \langle x,z\rangle^2 + \langle x,z\rangle^3

So instead of constructing Θ(d3)\Theta(d^3) features, we compute one ordinary dot product in O(d)O(d) time and evaluate:

K(x,z)=1+⟨x,z⟩+⟨x,z⟩2+⟨x,z⟩3 K(x,z) = 1 + \langle x,z\rangle + \langle x,z\rangle^2 + \langle x,z\rangle^3

The kernel trick avoids the cost of explicitly constructing a large feature vector, but it introduces a different cost: kernel methods often scale poorly with the number of training examples.

Storing the full Gram matrix requires:

O(n2) O(n^2)

memory.

Prediction also requires only kernel evaluations. For a new input xx:

θTϕ(x)=∑i=1nβiK(x(i),x) \theta^T\phi(x) = \sum_{i=1}^{n} \beta_i K(x^{(i)},x)

So both training and prediction can be carried out without explicitly computing ϕ(x)\phi(x).

Common Kernels

A kernel determines how two inputs are compared in feature space.

Common choices include:

Klinear(x,z)=xTz K_{\mathrm{linear}}(x,z) = x^Tz Kpolynomial(x,z)=(xTz+c)p,p∈N,c≥0 K_{\mathrm{polynomial}}(x,z) = (x^Tz+c)^p, \qquad p\in\mathbb{N}, \quad c\geq0 KRBF(x,z)=exp⁡(−∥x−z∥222σ2),σ>0 K_{\mathrm{RBF}}(x,z) = \exp \left( -\frac{\|x-z\|_2^2}{2\sigma^2} \right), \qquad \sigma>0

The linear kernel is just the ordinary dot product, so it does not introduce nonlinear features.

The polynomial kernel allows the model to use interactions between features up to degree pp when c>0c>0.

The RBF kernel gives high similarity to nearby points and low similarity to distant points. It corresponds to an infinite-dimensional feature space.

The polynomial degree pp and RBF length scale σ\sigma control how flexible the model is. These should usually be chosen using validation data.

Feature scaling is especially important for polynomial and RBF kernels because both depend strongly on feature magnitudes or distances.

Kernel Ridge Regression

The kernel trick is not a learning algorithm by itself. It is a way to rewrite algorithms so that they depend only on kernel evaluations.

Kernel ridge regression is one example.

It fits the training data using squared error while penalizing overly complex functions:

f∗=arg⁡min⁡f∈HK[∑i=1n(y(i)−f(x(i)))2+λ∥f∥HK2],λ>0 f^* = \arg\min_{f\in\mathcal{H}_K} \left[ \sum_{i=1}^{n} \left( y^{(i)}-f(x^{(i)}) \right)^2 + \lambda \|f\|_{\mathcal{H}_K}^2 \right], \qquad \lambda>0

The representer theorem tells us that the solution can always be written using the training examples:

f(x)=∑i=1nαiK(x(i),x) f(x) = \sum_{i=1}^{n} \alpha_i K(x^{(i)},x)

Let K∈Rn×nK\in\mathbb{R}^{n\times n} be the Gram matrix:

Kij=K(x(i),x(j)) K_{ij} = K(x^{(i)},x^{(j)})

and let:

y=[y(1)⋮y(n)] y = \begin{bmatrix} y^{(1)} \\ \vdots \\ y^{(n)} \end{bmatrix}

The optimal coefficients satisfy:

(K+λI)α=y (K+\lambda I)\alpha = y

We solve this linear system for α\alpha.

For a new input xx, define:

kx=[K(x(1),x)⋮K(x(n),x)] k_x = \begin{bmatrix} K(x^{(1)},x) \\ \vdots \\ K(x^{(n)},x) \end{bmatrix}

Prediction is then:

f(x)=kxTα f(x) = k_x^T\alpha

The parameter λ\lambda controls regularization.

A larger λ\lambda penalizes complex functions more strongly, while a smaller λ\lambda places more emphasis on fitting the training data.

Because λ>0\lambda>0, the matrix:

K+λI K+\lambda I

is positive definite even when KK is only positive semidefinite, so the linear system has a unique solution.

A direct solve takes:

O(n3) O(n^3)

time, while storing the Gram matrix requires:

O(n2) O(n^2)

memory.

This motivates approximate kernel methods for large datasets.

Validity of Kernels

Not every similarity function can be used as a kernel.

A valid kernel must behave like an inner product in some feature space.

More formally, K(x,z)K(x,z) must be symmetric, and for any finite set of inputs:

x(1),…,x(n) x^{(1)},\ldots,x^{(n)}

the Gram matrix:

Kij=K(x(i),x(j)) K_{ij} = K(x^{(i)},x^{(j)})

must be positive semidefinite.

Equivalently, there must exist a feature map ϕ\phi such that:

K(x,z)=⟨ϕ(x),ϕ(z)⟩ K(x,z) = \left\langle \phi(x), \phi(z) \right\rangle

One way to prove that a kernel is valid is to explicitly construct such a feature map.

Consider the quadratic kernel:

K(x,z)=(xTz+c)2,c≥0 K(x,z) = (x^Tz+c)^2, \qquad c\geq0

For x∈R3x\in\mathbb{R}^3, one possible feature map is:

ϕ(x)=[x12x22x322x1x22x1x32x2x32cx12cx22cx3c] \phi(x) = \begin{bmatrix} x_1^2 \\ x_2^2 \\ x_3^2 \\ \sqrt{2}x_1x_2 \\ \sqrt{2}x_1x_3 \\ \sqrt{2}x_2x_3 \\ \sqrt{2c}x_1 \\ \sqrt{2c}x_2 \\ \sqrt{2c}x_3 \\ c \end{bmatrix}

Its inner product reproduces the kernel.

See derivation
ϕ(x)Tϕ(z)=x12z12+x22z22+x32z32 \phi(x)^T\phi(z) = x_1^2z_1^2 + x_2^2z_2^2 + x_3^2z_3^2 +2x1x2z1z2+2x1x3z1z3+2x2x3z2z3 \quad + 2x_1x_2z_1z_2 + 2x_1x_3z_1z_3 + 2x_2x_3z_2z_3 +2c∑i=13xizi+c2 \quad + 2c \sum_{i=1}^{3} x_i z_i + c^2
=(∑i=13xizi)2+2c(∑i=13xizi)+c2 = \left( \sum_{i=1}^{3} x_i z_i \right)^2 + 2c \left( \sum_{i=1}^{3} x_i z_i \right) + c^2
=(xTz)2+2c(xTz)+c2 = (x^Tz)^2 + 2c(x^Tz) + c^2 =(xTz+c)2 = (x^Tz+c)^2 =K(x,z) = K(x,z)

Therefore, this is a valid kernel.

More generally, every valid kernel must produce a positive-semidefinite Gram matrix for every finite set of inputs.

For any coefficient vector aa:

aTKa≥0 a^TKa \geq 0

To see why, suppose:

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

Then:

See derivation
aTKa=∑i∑jaiKijaj a^TKa = \sum_i \sum_j a_i K_{ij} a_j =∑i∑jaiϕ(x(i))Tϕ(x(j))aj = \sum_i \sum_j a_i \phi(x^{(i)})^T \phi(x^{(j)}) a_j =∑i∑jai(∑kϕk(x(i))ϕk(x(j)))aj = \sum_i \sum_j a_i \left( \sum_k \phi_k(x^{(i)}) \phi_k(x^{(j)}) \right) a_j
=∑k(∑iaiϕk(x(i)))(∑jajϕk(x(j))) = \sum_k \left( \sum_i a_i \phi_k(x^{(i)}) \right) \left( \sum_j a_j \phi_k(x^{(j)}) \right)
=∑k(∑iaiϕk(x(i)))2 = \sum_k \left( \sum_i a_i \phi_k(x^{(i)}) \right)^2 ≥0 \geq 0

The final expression is a sum of squares, so it can never be negative. This is why any kernel that corresponds to an inner product produces a positive-semidefinite Gram matrix.