Simple Algorithm
Machine Learning · Descent · 22 min

The Kernel Trick

Nonlinear patterns with linear algebra.

No straight line separates a ring of points from the cluster inside it. Add one feature, the squared distance from the center, and a flat plane does. Kernels make this lifting free: any algorithm that touches its data only through inner products can run in an enormous, even infinite-dimensional, feature space at the cost of evaluating one function on pairs of points.

What you’ll learn

  • Define feature maps and kernels, and show that the polynomial kernel (1+x⊤z)p(1+\mathbf x^\top\mathbf z)^p and the RBF kernel e−γ∥x−z∥2e^{-\gamma\norm{\mathbf x-\mathbf z}^2} are inner products in feature spaces of dimension (d+pp)\binom{d+p}{p} and ∞\infty.
  • Prove that every Gram matrix is positive semidefinite, and build new kernels from old ones with the closure rules.
  • Kernelize an algorithm that uses its data only through inner products: derive kernel ridge regression, α=(K+λI)−1y\boldsymbol\alpha=(K+\lambda I)^{-1}\mathbf y, and outline the kernel SVM and kernel PCA.
  • Weigh the O(n3)\order{n^3} cost of exact kernel methods against explicit features, and choose a kernel, its width γ\gamma and the regularization by cross-validation.

Lifting data until a line works

Label the points of a disk y=+1y=+1 and the points of a ring around it y=−1y=-1 (labels in {−1,+1}\{-1,+1\}, as for SVMs). In the plane (x1,x2)(x_1,x_2) no line separates them: every line leaves ring points on the disk's side. Now send each point to three coordinates,

φ(x)=(x1, x2, x12+x22)∈R3.\varphi(\mathbf x)=\big(x_1,\ x_2,\ x_1^2+x_2^2\big)\in\R^3.

The third coordinate is the squared distance from the center: small on the disk, large on the ring. A horizontal plane at height r2r^2, for a radius rr between the disk and the ring, now separates the classes, and back in the input plane it is the circle x12+x22=r2x_1^2+x_2^2=r^2. A linear boundary in feature space is a nonlinear boundary in input space.

This is the basis-function idea of Feature Maps & the Road to Kernels: choose a feature map φ:Rd→RD\varphi:\R^d\to\R^D and fit a linear model f(x)=w⊤φ(x)+bf(\mathbf x)=\mathbf w^\top\varphi(\mathbf x)+b to the features. The catch is size. To let the boundary be any polynomial of degree pp, you need every monomial x1a1⋯xdadx_1^{a_1}\cdots x_d^{a_d} with a1+⋯+ad≤pa_1+\dots+a_d\le p, and there are (d+pp)\binom{d+p}{p} of them.

Predict before you look

With d=100d=100 inputs, how many thousand monomials of degree at most p=3p=3 are there?

Show the answer

177 thousand

(1033)=103⋅102⋅1016=176,851\binom{103}{3}=\frac{103\cdot102\cdot101}{6}=176{,}851. A degree-5 expansion of the same inputs has (1055)≈9.7×107\binom{105}{5}\approx9.7\times10^7 features. Storing and fitting such vectors soon becomes the bottleneck, yet the next step computes the inner product of two of them with about 100 multiplications.

Inner products without the features

Many learning algorithms see their inputs only through inner products xi⊤xj\mathbf x_i^\top\mathbf x_j: the SVM dual, ridge regression in its dual form, PCA through the Gram matrix, the perceptron, the distances of k-means. Run on features, they need only φ(x)⊤φ(z)\varphi(\mathbf x)^\top\varphi(\mathbf z). A kernel is a function that returns this number directly:

k(x,z)=⟨φ(x),φ(z)⟩.k(\mathbf x,\mathbf z)=\langle\varphi(\mathbf x),\varphi(\mathbf z)\rangle.

The kernel trick is to evaluate kk instead of forming φ\varphi, which pays off whenever kk is cheap and φ\varphi is huge.

DerivationThe polynomial kernel's hidden features
  1. (x⊤z)2=(∑i=1dxizi)2=∑i=1d∑j=1d(xixj)(zizj)=⟨φ2(x),φ2(z)⟩,φ2(x)=(xixj)i,j=1d(\mathbf x^\top\mathbf z)^2=\Big(\sum_{i=1}^d x_iz_i\Big)^2=\sum_{i=1}^d\sum_{j=1}^d(x_ix_j)(z_iz_j)=\langle\varphi_2(\mathbf x),\varphi_2(\mathbf z)\rangle,\qquad\varphi_2(\mathbf x)=(x_ix_j)_{i,j=1}^d

    Square an inner product and expand. The result is an inner product of the d2d^2 pairwise products:

  2. (1+x⊤z)2=1+2x1z1+2x2z2+x12z12+2x1x2z1z2+x22z22,φ(x)=(1,2x1,2x2,x12,2x1x2,x22)(1+\mathbf x^\top\mathbf z)^2=1+2x_1z_1+2x_2z_2+x_1^2z_1^2+2x_1x_2z_1z_2+x_2^2z_2^2,\qquad\varphi(\mathbf x)=\big(1,\sqrt2x_1,\sqrt2x_2,x_1^2,\sqrt2x_1x_2,x_2^2\big)

    Add the constant 1 and take d=2d=2. Six features appear, one per monomial of degree at most 2, with weights that make the cross terms come out right:

  3. (1+x⊤z)p=∑a0+a1+⋯+ad=pp!a0! a1!⋯ad! ∏i=1d(xizi)ai(1+\mathbf x^\top\mathbf z)^p=\sum_{a_0+a_1+\dots+a_d=p}\frac{p!}{a_0!\,a_1!\cdots a_d!}\,\prod_{i=1}^d(x_iz_i)^{a_i}

    In general the multinomial theorem gives one feature per monomial of degree at most pp (the exponent a0a_0 belongs to the constant 1), so the polynomial kernel is an inner product in (d+pp)\binom{d+p}{p} dimensions that costs O(d)\order{d} to evaluate:

Think first Take x=(1,2)\mathbf x=(1,2) and z=(3,−1)\mathbf z=(3,-1). Compute (1+x⊤z)2(1+\mathbf x^\top\mathbf z)^2 directly, then as φ(x)⊤φ(z)\varphi(\mathbf x)^\top\varphi(\mathbf z) with the six features above.

x⊤z=3−2=1\mathbf x^\top\mathbf z=3-2=1, so the kernel gives (1+1)2=4(1+1)^2=4. The features are φ(x)=(1,2,22,1,22,4)\varphi(\mathbf x)=(1,\sqrt2,2\sqrt2,1,2\sqrt2,4) and φ(z)=(1,32,−2,9,−32,1)\varphi(\mathbf z)=(1,3\sqrt2,-\sqrt2,9,-3\sqrt2,1), and their inner product is 1+6−4+9−12+4=41+6-4+9-12+4=4. Same number, but the kernel needed an inner product of length 2 instead of building two vectors of length 6.

The Gaussian RBF kernel k(x,z)=exp⁡(−γ∥x−z∥2)k(\mathbf x,\mathbf z)=\exp(-\gamma\norm{\mathbf x-\mathbf z}^2), the default choice in practice, goes further: its feature space is infinite-dimensional. Writing γ=1/(2ℓ2)\gamma=1/(2\ell^2) makes its length scale ℓ\ell explicit: points much farther apart than ℓ\ell have k≈0k\approx0.

DerivationThe RBF kernel hides infinitely many features
  1. e−γ∥x−z∥2=e−γ∥x∥2 e−γ∥z∥2 e2γ x⊤ze^{-\gamma\norm{\mathbf x-\mathbf z}^2}=e^{-\gamma\norm{\mathbf x}^2}\,e^{-\gamma\norm{\mathbf z}^2}\,e^{2\gamma\,\mathbf x^\top\mathbf z}

    Expand the squared distance, ∥x−z∥2=∥x∥2+∥z∥2−2x⊤z\norm{\mathbf x-\mathbf z}^2=\norm{\mathbf x}^2+\norm{\mathbf z}^2-2\mathbf x^\top\mathbf z, and split the exponential:

  2. e2γ x⊤z=∑m=0∞(2γ)mm! (x⊤z)me^{2\gamma\,\mathbf x^\top\mathbf z}=\sum_{m=0}^\infty\frac{(2\gamma)^m}{m!}\,(\mathbf x^\top\mathbf z)^m

    The last factor is a power series with positive coefficients: a weighted sum of polynomial kernels of every degree.

  3. φm(x)=e−γx2(2γ)mm!  xm(m=0,1,2,… ),∑m=0∞φm(x) φm(z)=e−γ(x−z)2\varphi_m(x)=e^{-\gamma x^2}\sqrt{\frac{(2\gamma)^m}{m!}}\;x^m\quad(m=0,1,2,\dots),\qquad\sum_{m=0}^\infty\varphi_m(x)\,\varphi_m(z)=e^{-\gamma(x-z)^2}

    So the features are all monomials of all degrees, damped by e−γ∥x∥2e^{-\gamma\norm{\mathbf x}^2}. In one dimension they can be written out, one per power of xx:

Which functions are kernels?

For points x1,…,xn\mathbf x_1,\dots,\mathbf x_n, the Gram matrix collects all pairwise kernel values, Kij=k(xi,xj)K_{ij}=k(\mathbf x_i,\mathbf x_j). It plays the role that XX⊤XX^\top plays for linear methods, and it has a defining property.

If k1,k2k_1,k_2 are kernels, so isWhyExample
c k1c\,k_1 for c≥0c\ge0scale the features by c\sqrt cthe signal variance σf2\sigma_f^2 in front of a GP kernel
k1+k2k_1+k_2concatenate the two feature vectorslinear + RBF: a global trend plus local bumps
k1 k2k_1\,k_2features are all products φ1aφ2b\varphi_{1a}\varphi_{2b}; for Gram matrices, the Schur product theorem(x⊤z)2=(x⊤z)(x⊤z)(\mathbf x^\top\mathbf z)^2=(\mathbf x^\top\mathbf z)(\mathbf x^\top\mathbf z)
f(x) k1(x,z) f(z)f(\mathbf x)\,k_1(\mathbf x,\mathbf z)\,f(\mathbf z) for any fffeatures f(x)φ1(x)f(\mathbf x)\varphi_1(\mathbf x)normalizing: k1/k1(x,x) k1(z,z)k_1/\sqrt{k_1(\mathbf x,\mathbf x)\,k_1(\mathbf z,\mathbf z)}
k1(ψ(x),ψ(z))k_1(\psi(\mathbf x),\psi(\mathbf z)) for any map ψ\psifeatures φ1(ψ(x))\varphi_1(\psi(\mathbf x))an RBF kernel on standardized inputs or on learned embeddings
exp⁡(k1)\exp(k_1), or any power series in k1k_1 with nonnegative coefficientslimits of sums and productse2γx⊤ze^{2\gamma\mathbf x^\top\mathbf z}
With these rules the RBF kernel needs no infinite expansion: e2γx⊤ze^{2\gamma\mathbf x^\top\mathbf z} is the exponential of a kernel, and multiplying it by f(x)f(z)f(\mathbf x)f(\mathbf z) with f(x)=e−γ∥x∥2f(\mathbf x)=e^{-\gamma\norm{\mathbf x}^2} gives e−γ∥x−z∥2e^{-\gamma\norm{\mathbf x-\mathbf z}^2}.

Kernels also free you from vectors. A string kernel compares two documents or DNA sequences by the substrings they share, a graph kernel compares molecules by their walks or subtrees, a set kernel compares bags of items. Anything with a positive semidefinite similarity can be handed to an SVM or a Gaussian process.

Check your understanding

Which of these is not a valid kernel on Rd\R^d?

  1. (1+x⊤z)3(1+\mathbf x^\top\mathbf z)^3
  2. exp⁡(−∥x−z∥)\exp(-\norm{\mathbf x-\mathbf z})
  3. ∥x−z∥2\norm{\mathbf x-\mathbf z}^2
  4. (x⊤z)exp⁡(−∥x−z∥2)(\mathbf x^\top\mathbf z)\exp(-\norm{\mathbf x-\mathbf z}^2)
Show the answer

C. ∥x−z∥2\norm{\mathbf x-\mathbf z}^2

For two distinct points the Gram matrix is (0aa0)\begin{pmatrix}0&a\\a&0\end{pmatrix} with a=∥x1−x2∥2>0a=\norm{\mathbf x_1-\mathbf x_2}^2>0. Its eigenvalues are ±a\pm a, and c=(1,−1)\mathbf c=(1,-1) gives c⊤Kc=−2a<0\mathbf c^\top K\mathbf c=-2a<0, impossible for a squared norm. A distance measures dissimilarity; kernels measure similarity.

Kernelizing an algorithm

Turning a linear method into a kernel method takes three moves: show that the solution is a combination of the training features, w=∑iαiφ(xi)\mathbf w=\sum_i\alpha_i\varphi(\mathbf x_i); rewrite everything in terms of inner products; replace every inner product by kk. Ridge regression is the cleanest case.

DerivationKernel ridge regression
  1. w^=arg min⁡w ∑i=1n(yi−w⊤φ(xi))2+λ∥w∥2=(Φ⊤Φ+λID)−1Φ⊤y\hat{\mathbf w}=\argmin_{\mathbf w}\ \sum_{i=1}^n\big(y_i-\mathbf w^\top\varphi(\mathbf x_i)\big)^2+\lambda\norm{\mathbf w}^2=(\Phi^\top\Phi+\lambda I_D)^{-1}\Phi^\top\mathbf y

    Ridge regression in feature space, with Φ∈Rn×D\Phi\in\R^{n\times D} the matrix whose rows are the φ(xi)⊤\varphi(\mathbf x_i)^\top:

  2. w^=Φ⊤α=∑i=1nαi φ(xi),α=(K+λIn)−1y,K=ΦΦ⊤\hat{\mathbf w}=\Phi^\top\boldsymbol\alpha=\sum_{i=1}^n\alpha_i\,\varphi(\mathbf x_i),\qquad\boldsymbol\alpha=(K+\lambda I_n)^{-1}\mathbf y,\qquad K=\Phi\Phi^\top

    The push-through identity of Ridge Regression in Depth, (Φ⊤Φ+λI)−1Φ⊤=Φ⊤(ΦΦ⊤+λI)−1(\Phi^\top\Phi+\lambda I)^{-1}\Phi^\top=\Phi^\top(\Phi\Phi^\top+\lambda I)^{-1}, moves the inverse onto an n×nn\times n matrix, and ΦΦ⊤\Phi\Phi^\top is exactly the Gram matrix:

  3. f^(x)=w^⊤φ(x)=∑i=1nαi k(xi,x)=k(x)⊤(K+λI)−1y\hat f(\mathbf x)=\hat{\mathbf w}^\top\varphi(\mathbf x)=\sum_{i=1}^n\alpha_i\,k(\mathbf x_i,\mathbf x)=\mathbf k(\mathbf x)^\top(K+\lambda I)^{-1}\mathbf y

    Predictions need only the kernel values between the new point and the training points, k(x)=(k(x1,x),…,k(xn,x))⊤\mathbf k(\mathbf x)=\big(k(\mathbf x_1,\mathbf x),\dots,k(\mathbf x_n,\mathbf x)\big)^\top:

  4. (f^(x1),…,f^(xn))⊤=K(K+λI)−1y → λ→0+  y\big(\hat f(\mathbf x_1),\dots,\hat f(\mathbf x_n)\big)^\top=K(K+\lambda I)^{-1}\mathbf y\ \xrightarrow{\ \lambda\to0^+\ }\ \mathbf y

    φ\varphi has disappeared. With the RBF kernel the fit is a weighted sum of Gaussian bumps centered on the training points, and λ\lambda trades fitting the labels against the size of the weights. As λ→0\lambda\to0 (with KK invertible, as it is for the RBF kernel on distinct points) the fit interpolates the data:

python
import numpy as np

def rbf(A, B, gamma):
    # squared distances between the rows of A and the rows of B
    sq = (A**2).sum(1)[:, None] + (B**2).sum(1)[None, :] - 2 * A @ B.T
    return np.exp(-gamma * np.maximum(sq, 0.0))

def krr_fit(X, y, gamma, lam):
    K = rbf(X, X, gamma)                                  # n x n Gram matrix
    return np.linalg.solve(K + lam * np.eye(len(X)), y)   # alpha: O(n^3)

def krr_predict(X_train, alpha, X_new, gamma):
    return rbf(X_new, X_train, gamma) @ alpha             # sum_i alpha_i k(x_i, x)

The whole method in a dozen lines: O(n2d)\order{n^2d} time and O(n2)\order{n^2} memory for the Gram matrix, O(n3)\order{n^3} for the solve, O(nd)\order{nd} per prediction. scikit-learn's KernelRidge(alpha=lam, kernel='rbf', gamma=gamma) fits the same estimator; its alpha is this lesson's λ\lambda.

AlgorithmSees the data throughKernelized solution
Ridge regressionXX⊤XX^\top, in its dual formf^(x)=∑iαik(xi,x)\hat f(\mathbf x)=\sum_i\alpha_ik(\mathbf x_i,\mathbf x) with α=(K+λI)−1y\boldsymbol\alpha=(K+\lambda I)^{-1}\mathbf y
Support vector machinexi⊤xj\mathbf x_i^\top\mathbf x_j in the dualf^(x)=∑iαiyik(xi,x)+b\hat f(\mathbf x)=\sum_i\alpha_iy_ik(\mathbf x_i,\mathbf x)+b, with αi>0\alpha_i>0 only for the support vectors
PCAthe Gram matrix of centered dataeigenvectors of HKHHKH, H=I−1n11⊤H=I-\tfrac1n\mathbf 1\mathbf 1^\top; each component is a kernel expansion ∑iaik(xi,x)\sum_ia_ik(\mathbf x_i,\mathbf x) (after centering)
Perceptronw⊤x=∑iαiyi xi⊤x\mathbf w^\top\mathbf x=\sum_i\alpha_iy_i\,\mathbf x_i^\top\mathbf xsign⁡(∑iαiyik(xi,x))\sign\big(\sum_i\alpha_iy_ik(\mathbf x_i,\mathbf x)\big), where αi\alpha_i counts the mistakes made on example ii
Gaussian process regressioncovariances of function valuesposterior mean k(x)⊤(K+σ2I)−1y\mathbf k(\mathbf x)^\top(K+\sigma^2I)^{-1}\mathbf y, which is kernel ridge regression with λ=σ2\lambda=\sigma^2, plus an error bar
The SVM's dual is derived in Support Vector Machines and dissected in The SVM Dual & Support Vectors; kernel PCA returns in PCA in Depth, and Gaussian processes in Gaussian Processes.

Cost, and choosing the kernel

Exact kernel methods trade the feature dimension DD for the number of examples nn:

CostExplicit features (primal)Kernel (dual)
TrainingO(nD2+D3)\order{nD^2+D^3}O(n2d+n3)\order{n^2d+n^3}
MemoryO(nD)\order{nD}O(n2)\order{n^2}
One predictionO(D)\order{D}O(nd)\order{nd}; for an SVM, only the support vectors count
Best whenn≫Dn\gg DD≫nD\gg n, or D=∞D=\infty
When nn and DD are both huge, neither column is affordable and approximations take over (Scaling Kernel Methods).
Predict before you look

How many gigabytes does the Gram matrix of n=100,000n=100{,}000 training points take in double precision (8 bytes per entry)?

Show the answer

80 GB

n2=1010n^2=10^{10} entries times 8 bytes is 8×10108\times10^{10} bytes, 80 GB, before the solve of about n3/3≈3×1014n^3/3\approx3\times10^{14} operations even starts. Symmetry halves the storage, but the scaling is the real problem: ten times more data costs a hundred times the memory and a thousand times the time.

Choosing a kernel is choosing an inductive bias: whatever the kernel calls similar, the model treats alike. Practical rules:

  • Standardize the inputs. The RBF and polynomial kernels use Euclidean geometry, so a feature measured in thousands drowns one measured in fractions.
  • The width γ=1/(2ℓ2)\gamma=1/(2\ell^2) is a bias–variance knob. As γ→0\gamma\to0 every pair of points looks alike, KK approaches the all-ones matrix, and the fit flattens: underfitting. As γ→∞\gamma\to\infty, K→IK\to I and each training point becomes an isolated spike: the model memorizes and predicts about zero elsewhere.
  • Start from the median heuristic, ℓ\ell equal to the median distance between training points, then search γ\gamma together with λ\lambda (or the SVM's CC) on a logarithmic grid by cross-validation.
  • Encode what you know. Periodicity, additive structure, invariances or a known roughness can be built into the kernel (Kernel Design for Gaussian Processes), and Gaussian processes can fit kernel parameters by maximizing the marginal likelihood.

Key takeaways

  • A kernel is an inner product of feature maps, k(x,z)=⟨φ(x),φ(z)⟩k(\mathbf x,\mathbf z)=\langle\varphi(\mathbf x),\varphi(\mathbf z)\rangle. The polynomial kernel (1+x⊤z)p(1+\mathbf x^\top\mathbf z)^p hides (d+pp)\binom{d+p}{p} monomials and costs O(d)\order{d}; the RBF kernel hides infinitely many features.
  • A symmetric function is a kernel exactly when all its Gram matrices are positive semidefinite. Sums, products, nonnegative scalings and exponentials of kernels are kernels, and so is a kernel evaluated on transformed inputs, k(ψ(x),ψ(z))k(\psi(\mathbf x),\psi(\mathbf z)).
  • Kernelizing: write w=∑iαiφ(xi)\mathbf w=\sum_i\alpha_i\varphi(\mathbf x_i) and replace inner products by kk. Kernel ridge regression has α=(K+λI)−1y\boldsymbol\alpha=(K+\lambda I)^{-1}\mathbf y and f^(x)=∑iαik(xi,x)\hat f(\mathbf x)=\sum_i\alpha_ik(\mathbf x_i,\mathbf x); the SVM, PCA, the perceptron and Gaussian processes follow the same pattern.
  • Exact kernel methods cost O(n2)\order{n^2} memory and O(n3)\order{n^3} time. Use the kernel when features outnumber examples, explicit (or approximate) features when examples dominate.
  • Standardize the inputs, then choose the kernel, its width γ\gamma and λ\lambda or CC by cross-validation: small γ\gamma underfits, large γ\gamma memorizes.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A polynomial kernel (1+x⊤z)3(1+\mathbf x^\top\mathbf z)^3 is used on d=50d=50 inputs. How large is its feature space, and what does one kernel evaluation cost?

  1. 503=125,00050^3=125{,}000 features, and an evaluation must form them: about 125,000 multiplications
  2. (533)=23,426\binom{53}{3}=23{,}426 features, but one evaluation costs about 50 multiplications
  3. 3×50=1503\times50=150 features, about 150 multiplications
  4. Infinitely many features, so the kernel can only be approximated
Show the answer

B. (533)=23,426\binom{53}{3}=23{,}426 features, but one evaluation costs about 50 multiplications

The features are the monomials of degree at most 3 in 50 variables, (533)=23,426\binom{53}{3}=23{,}426 of them. The kernel never forms them: it computes x⊤z\mathbf x^\top\mathbf z (50 multiplications), adds 1 and cubes.

Question 2

Why is every Gram matrix of a valid kernel positive semidefinite?

  1. Because c⊤Kc=∥∑iciφ(xi)∥2\mathbf c^\top K\mathbf c=\norm{\sum_ic_i\varphi(\mathbf x_i)}^2, a squared length, which cannot be negative
  2. Because all its entries are nonnegative
  3. Because it is symmetric
  4. Because every 2×22\times2 principal submatrix has a nonnegative determinant
Show the answer

A. Because c⊤Kc=∥∑iciφ(xi)∥2\mathbf c^\top K\mathbf c=\norm{\sum_ic_i\varphi(\mathbf x_i)}^2, a squared length, which cannot be negative

A quadratic form in the Gram matrix is the squared norm of a combination of feature vectors. That is the whole proof, and it is why the converse (Moore–Aronszajn) is the natural definition of a kernel.

Question 3

After fitting kernel ridge regression with an RBF kernel on nn points, what do you need to predict at a new input x\mathbf x?

  1. The weight vector w\mathbf w in feature space
  2. Only the support vectors, since most coefficients are zero
  3. The coefficients α=(K+λI)−1y\boldsymbol\alpha=(K+\lambda I)^{-1}\mathbf y and the kernel values k(xi,x)k(\mathbf x_i,\mathbf x) for all nn training points
  4. A new solve of (K+λI)α=y(K+\lambda I)\boldsymbol\alpha=\mathbf y that includes x\mathbf x
Show the answer

C. The coefficients α=(K+λI)−1y\boldsymbol\alpha=(K+\lambda I)^{-1}\mathbf y and the kernel values k(xi,x)k(\mathbf x_i,\mathbf x) for all nn training points

f^(x)=∑iαik(xi,x)\hat f(\mathbf x)=\sum_i\alpha_ik(\mathbf x_i,\mathbf x): the coefficients are computed once, in O(n3)\order{n^3}, and each prediction is a weighted sum costing O(nd)\order{nd}.

Question 4

You fit RBF kernel ridge regression with a γ\gamma so large that k(xi,xj)≈0k(\mathbf x_i,\mathbf x_j)\approx0 for every pair of distinct training points. What does the model do?

  1. It fits a nearly straight line
  2. It returns about yi/(1+λ)y_i/(1+\lambda) at each training point and about 0 everywhere else
  3. It becomes linear regression on the raw inputs
  4. It fails, because KK becomes singular
Show the answer

B. It returns about yi/(1+λ)y_i/(1+\lambda) at each training point and about 0 everywhere else

With K≈IK\approx I, α≈y/(1+λ)\boldsymbol\alpha\approx\mathbf y/(1+\lambda) and f^(x)=∑iαik(xi,x)\hat f(\mathbf x)=\sum_i\alpha_ik(\mathbf x_i,\mathbf x) is a row of narrow spikes, one per training point. The training labels are reproduced (shrunk), and away from the data the prediction collapses to 0: overfitting in its purest form.

Question 5

You have n=2,000,000n=2{,}000{,}000 examples with d=20d=20 inputs and want a degree-2 polynomial model. Which route makes sense?

  1. The kernel trick, because it avoids forming features
  2. Either: the two routes cost about the same
  3. Neither: a degree-2 model needs n≤dn\le d
  4. Explicit features: there are only (222)=231\binom{22}{2}=231, so a linear fit costs about nD2≈1011nD^2\approx10^{11} operations, while the Gram matrix alone would have 4×10124\times10^{12} entries
Show the answer

D. Explicit features: there are only (222)=231\binom{22}{2}=231, so a linear fit costs about nD2≈1011nD^2\approx10^{11} operations, while the Gram matrix alone would have 4×10124\times10^{12} entries

The kernel trick wins when features outnumber examples. Here D=231≪nD=231\ll n: the primal fit is cheap, while the dual's n×nn\times n matrix needs about 32 TB in double precision and its solve about n3=8×1018n^3=8\times10^{18} operations.

Go deeper

Further reading

  • B. Schölkopf & A. J. Smola, Learning with Kernels (MIT Press, 2002).
  • T. Hofmann, B. Schölkopf & A. J. Smola, "Kernel methods in machine learning," Annals of Statistics 36(3), 1171–1220 (2008).
  • B. E. Boser, I. M. Guyon & V. N. Vapnik, "A training algorithm for optimal margin classifiers," Proceedings of the 5th Annual Workshop on Computational Learning Theory (COLT), 144–152 (1992).
  • B. Schölkopf, A. Smola & K.-R. Müller, "Nonlinear component analysis as a kernel eigenvalue problem," Neural Computation 10(5), 1299–1319 (1998).