Skip to content
mentorship

concepts

Kernel methods and the kernel trick

Compute inner products in a high-dimensional feature space without ever materializing the features. The mathematical move that lets a linear classifier draw nonlinear boundaries.

Reviewed · 4 min read

One-line definition

A kernel computes the inner product of two points in a feature space defined by , without explicitly evaluating . Algorithms expressible in terms of inner products (SVM, Gaussian processes, kernel PCA, kernel ridge regression) can swap dot products for and operate implicitly in feature space.

Why it matters

Linear models are limited to linear decision boundaries. The classical fix was to engineer nonlinear features and run a linear model on them. Kernel methods give you that move for free: any positive-definite kernel implicitly defines a (potentially infinite-dimensional) feature space, and the algorithm only ever touches inner products.

This was the dominant nonlinear ML approach from roughly 1995 to 2012. Then deep learning replaced it for almost every supervised task. Kernels remain central to Gaussian processes, attention (the softmax of is a kernelized similarity), and certain interpretability and theoretical-analysis tools (NTK, kernel ridge regression as a baseline).

The trick

Suppose your algorithm only reads data through inner products . Substitute . You are now running the algorithm in the feature space defined by , where , without touching .

Concrete example: polynomial kernel of degree 2 in :

Expand it. You will find this equals where maps to a -dimensional space of monomials up to degree 2. Computing explicitly is memory and compute; computing is .

For the RBF kernel , the implicit feature space is infinite-dimensional. You cannot compute explicitly even in principle.

The Gram matrix

For a dataset of points, the kernel defines an Gram matrix . Many kernel algorithms reduce to operations on :

  • Kernel ridge regression: where .
  • Kernel PCA: eigendecompose the centered .
  • Gaussian processes: use as the covariance.
  • SVM: dual formulation depends only on and labels.

The Gram matrix shape is the bottleneck: memory, to invert. Limits naive kernel methods to roughly .

What makes a valid kernel

must be positive semi-definite: for any finite set of points, the Gram matrix is PSD ( for all ). Equivalently, for some into some inner-product space (Mercer’s theorem).

Common kernels:

  • Linear: . The trivial case.
  • Polynomial: .
  • RBF / Gaussian: . The default.
  • Laplacian: .
  • String kernels: count shared substrings.
  • Graph kernels: count shared subgraphs.

Combinations of valid kernels (sums, products, scalings) are valid kernels. Standard recipe for engineering domain-specific kernels.

Kernel trick in attention

A bilinear attention score is the linear kernel. Linear attention papers replace this with feature-map kernels for cheaper computation; a softmax-attention variant can be approximated by random Fourier features for the RBF kernel.

Why deep learning won

Kernels make a strong assumption: the right similarity function is fixed in advance. Deep learning learns the feature representation jointly with the task. For high-dimensional structured data (images, text, audio), learned representations beat hand-picked kernels by huge margins.

Kernels remain useful when:

  • Data is small (Gaussian processes).
  • The kernel encodes domain knowledge (string kernels in computational biology).
  • Theoretical analysis is the goal (NTK, infinite-width networks).

Common pitfalls

  • Confusing the kernel trick with the kernel matrix. The trick is the substitution; the matrix is the data structure.
  • Using RBF without scaling. Standardize or whiten features first; the bandwidth is sensitive to feature scale.
  • Treating kernel methods as scalable. The Gram matrix kills naive applications above points. Approximations (Nyström, random Fourier features, inducing points for GPs) exist.
  • Conflating “kernel” in the SVM sense with “kernel” in the convolution sense. Two unrelated meanings of the same word.