Reading club II. The kernel trick
Notes from an ML reading club session, continuing from linear regression.
Recap
First, the computational cost of the two forms from last time. In the primal form, forming costs and inverting it costs . In the dual form, forming costs and inverting costs . The primal form is cheaper when (few features, many data points), and the dual form is cheaper when .
When , fewer training points than features, the primal problem is underdetermined and admits infinitely many solutions. This is closely related to overfitting, since with more parameters than constraints the model can fit noise. Ridge regression selects a unique solution by penalizing large weights,
where controls the trade-off between fit and complexity, and the modified normal equations are always invertible.
Features
Linear regression is not especially expressive. An improvement is to perform linear regression in a basis of fixed, nonlinear features , where each maps the input to a new coordinate. The model becomes
For example, with inputs , a quadratic feature map might be , giving features from inputs.
In the dual form, we never need the individual feature vectors, only their inner products. The Gram matrix becomes
and predictions take the form
Computing each inner product costs , which is expensive when is large.
The kernel trick
The key observation is that certain kernel functions compute the inner product in feature space without ever constructing the feature vectors. For example, the polynomial kernel of degree 2,
costs only to evaluate, since it operates entirely in the original -dimensional input space, yet expanding it for reproduces the inner product of the quadratic feature map above, computed in rather than time.
How can we know the inner product without knowing the features? The features are determined by the choice of kernel. Each valid kernel implicitly defines a unique feature map (up to rotation) via Mercer’s theorem, and the dual form only ever queries inner products, which the kernel supplies directly. We still choose the features, but indirectly, through the choice of kernel, and we never pay the cost of materializing them. This insight is due to Aizerman, Braverman, and Rozonoer (1964), later popularized by Boser, Guyon, and Vapnik (1992) in the context of support vector machines.
Beyond the polynomial kernel, the Gaussian kernel (also called squared exponential or radial basis function) is particularly powerful,
where is the length-scale hyperparameter. Taylor-expanding the Gaussian kernel produces an infinite series of polynomial terms, so it is equivalent to working in an infinite-dimensional feature space, yet each evaluation still costs only . Polynomial kernels of degree capture all multinomial interactions up to order , while the Gaussian kernel captures interactions of all orders.
Prediction in kernel space
In practice, the kernel matrix is constructed once from the training data. At prediction time, only the kernel vector , with entries , is needed,
This costs per prediction, independent of the feature-space dimension . All computation lives in kernel space, the -dimensional space indexed by training points.
Although kernel methods are linear in the implicit feature space and therefore limited in expressiveness compared to deep neural networks, they remain popular when computational speed is prioritized. The FLARE framework for machine-learning interatomic potentials, for example, maps atomic environments to descriptors and uses Gaussian process regression, a kernelized Bayesian extension of ridge regression, to predict energies and forces on the fly during molecular dynamics.