S1432 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S14 · Lesson 32 · Kernel methods

Rewriting ridge regression in terms of the data points, kernels as inner products in a feature space you never build, polynomial and Gaussian kernels and when a function is a valid kernel, kernel ridge regression worked by hand, kernel smoothing, and what the dual form costs

Lesson 9's basis functions get expensive fast: a cubic in 100 inputs has 176,851 terms. But ridge regression's answer can be rewritten so the features appear only through inner products between pairs of data points, and some inner products of enormous, even infinite, feature vectors can be computed in a few operations straight from the inputs. That's the kernel trick. It turns any linear method that depends only on inner products, ridge regression, PCA, the support vector machine of lesson 33, into a nonlinear one, at a cost that grows with the number of examples instead of the number of features.

  • Kernel smoothing (the Nadaraya–Watson estimator) on five data points: each prediction is a weighted average of the targets, with Gaussian weights k(x,xn)=e−(x−xn)2/2h2.
  • A small bandwidth h follows every point; a large one flattens toward the overall mean. It's lesson 9's bias–variance trade with h as the knob.
  • Kernel ridge regression uses the same kernel but solves for its weights, which lets it fit the data more closely.

Predict

The kernel k(𝐱,𝐳)=(𝐱T𝐳+1)3 is used on inputs with 100 components. How many features does it implicitly compute with, and how many operations does one kernel evaluation take?

Name it

The dual form of ridge regression

Ridge regression's weights can be written as a combination of the training features, 𝐰=ΦT𝐚, with

𝐚=(K+λIN)−1𝐭,Knm=k(𝐱n,𝐱m)=𝝓(𝐱n)T𝝓(𝐱m)

and predictions y(𝐱)=∑nank(𝐱,𝐱n). The features appear only through the kernel k, and K is the N × N Gram matrix.

Kernels

  • Polynomial: (𝐱T𝐳+c)M, an inner product of all monomials up to degree M: (D+MM) features in D dimensions.
  • Gaussian: exp⁡(−‖𝐱−𝐳‖2/2s2), with an infinite feature space; s sets how far each point's influence reaches.

A function is a valid kernel when every Gram matrix it makes is positive semidefinite (Mercer's condition). Sums, products and positive multiples of valid kernels are valid. Distances in feature space follow from the kernel too: ‖𝝓(𝐱)−𝝓(𝐳)‖2=k(𝐱,𝐱)+k(𝐳,𝐳)−2k(𝐱,𝐳).

Kernel smoothing

The Nadaraya–Watson estimator y(x)=∑nk(x,xn)tn/∑nk(x,xn) needs no solve at all: a locally weighted average.

Cost

The primal solve costs about M3 for M features; the dual costs about N3 for N examples and stores N2 kernel values. Kernels win when features vastly outnumber examples and lose when examples number in the millions.

Worked example

Fit kernel ridge regression to the points (0, 1) and (1, 2) with a Gaussian kernel of s = 1 and λ = 0.1, and predict at x = 0.5.

Show the solution
  1. Gram matrix. k(0,1)=e−1/2 = 0.6065, so K+λI=[1.10.60650.60651.1] with determinant det⁡=1.21−0.3679=0.8421
  2. Coefficients. a1=1.1×1−0.6065×20.8421=−0.134a2=1.1×2−0.60650.8421=1.892
  3. Predict at x = 0.5. Both kernels equal e−1/8 = 0.8825, so y=0.8825×(−0.134+1.892)=1.551
  4. At the training points. The fit gives 1.013 and 1.811, pulled slightly toward each other by λ.

Answer the prediction at x = 0.5 is 1.551.

Worked example

How many features does a quadratic kernel use on 10 inputs, and what does a Gram matrix for 50,000 examples cost to store in double precision?

Show the solution
  1. Features. (122)=66 Here the primal form is cheaper.
  2. Gram matrix. It has 2.5×109 entries, so at 8 bytes each 2.5×109×8=20GB
  3. Solving. Solving it takes about 1014 operations, which is why large data sets use approximations.

Answer the quadratic kernel uses 66 features, and the Gram matrix takes 20 GB.

Worked example

Is k(x,z)=(x−z)2 a valid kernel? Test it on the two points 0 and 1, and compare the Gaussian kernel with s = 1 on the same points.

Show the solution
  1. What a kernel needs. A valid kernel must give a positive semidefinite Gram matrix for every set of points.
  2. Test (x−z)2. Here the Gram matrix is [0110] with eigenvalues 1 and −1: the combination (1, −1) would have a negative squared length in feature space, so no feature map exists and the function isn't a kernel.
  3. Gaussian kernel. It gives [10.6070.6071] with eigenvalues 1.607 and 0.393, both positive, as they are for any distinct points.
  4. What works. A distance can't stand in for a similarity; its negative exponential can.

Answer no: (x−z)2 gives a Gram matrix with eigenvalue −1, so it isn't a kernel; the Gaussian kernel's eigenvalues, 1.607 and 0.393, are both positive.

Check

Two inputs are 1.5 apart. What's their Gaussian kernel value with width s = 0.5?

Check

What's the polynomial kernel (𝐱T𝐳+1)2 for x = (1, 0.5) and z = (3, −0.5)?

Check

How many features does the kernel (𝐱T𝐳+1)2 correspond to for 20-component inputs?

Check

Two inputs are 1.5 apart. With a Gaussian kernel of width 1, what's the squared distance between their feature vectors?

Check

Kernel ridge regression on two points with targets 1 and −1, kernel values 1 on the diagonal and 0.7 between them, and λ = 0.5. What does it predict at the first training point?

Check

How many gigabytes does the Gram matrix for 30000 examples take in double precision (8 bytes per entry)?

Check

Two training inputs are 0.5 apart, and a Gaussian kernel of width s = 0.5 is used.

(a)

What is the Gram matrix's smaller eigenvalue?

(b)

What is its condition number?

Check

Kernel ridge regression uses two training points with targets t and u, a Gram matrix with ones on the diagonal and k off it, and regularization λ.

Step 1

What is the first dual coefficient a1?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

How can a kernel compute with infinitely many features at finite cost?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.