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 .
- 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 is used on inputs with 100 components. How many features does it implicitly compute with, and how many operations does one kernel evaluation take?
expands into every monomial of degree up to 3, = 176,851 of them (with scale factors). Computing it as written takes one 100-term inner product, an addition and a cube. The Gaussian kernel goes further: its feature space is infinite-dimensional.
Name it
The dual form of ridge regression
Ridge regression's weights can be written as a combination of the training features, , with
and predictions . The features appear only through the kernel k, and K is the N × N Gram matrix.
Kernels
- Polynomial: , an inner product of all monomials up to degree M: features in D dimensions.
- Gaussian: , 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: .
Kernel smoothing
The Nadaraya–Watson estimator needs no solve at all: a locally weighted average.
Cost
The primal solve costs about for M features; the dual costs about for N examples and stores 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
- Gram matrix. = 0.6065, so with determinant
- Coefficients.
- Predict at x = 0.5. Both kernels equal = 0.8825, so
- 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
- Features. Here the primal form is cheaper.
- Gram matrix. It has entries, so at 8 bytes each
- Solving. Solving it takes about 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 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
- What a kernel needs. A valid kernel must give a positive semidefinite Gram matrix for every set of points.
- Test . Here the Gram matrix is 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.
- Gaussian kernel. It gives with eigenvalues 1.607 and 0.393, both positive, as they are for any distinct points.
- What works. A distance can't stand in for a similarity; its negative exponential can.
Answer no: 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 for x = (1, 0.5) and z = (3, −0.5)?
Check
How many features does the kernel 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 ?
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°.
Step 2
What is ?
Explain why
How can a kernel compute with infinitely many features at finite cost?
Many learning algorithms use the features only through inner products between pairs of examples, so it's enough to know those inner products, never the feature vectors themselves. For some feature maps the inner product has a closed form in the original inputs: the polynomial kernel's sum over every monomial collapses into a power of the ordinary inner product, and the Gaussian kernel's infinite series of features sums to a single exponential of the squared distance. The algorithm then works with the N × N matrix of kernel values, whose size depends on the number of examples, while the feature space it effectively uses can be as large as the kernel implies.
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.