Skip to content
mlmentorship

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.

Published · 9 min read ·Core ·Foundation
Reading depth

Summary

A kernel computes a feature-space inner product without explicitly constructing the feature vectors. Write . An algorithm expressed through these inner products can use the kernel directly, preserving the feature-space calculation. The shortcut avoids feature construction; it does not automatically avoid pairwise dataset storage or expensive training solves.

The trick

Suppose the desired feature-space algorithm reads inputs only through . For two-dimensional inputs, consider

An exact feature map is

Expanding the feature inner product reproduces the squared polynomial, including its cross-term coefficients. For and , the six contributions are , totaling . The shortcut obtains from the original coordinates. In dimensions, explicit degree-two features number ; one kernel evaluation needs arithmetic.

Learning objective

The kernel reaches 36 without constructing either feature vector

The kernel computes the same inner product without expanding featuresThe coordinate dot product is 4; the kernel (4+2)^2 is 36. This is an exact shortcut, not an approximation.Two exact routes gather the samethirty-six unitsExplicit feature products448884Compact kernel: (coordinate dot 4 +2) squared36 = 36. No feature-expansionapproximation.
Derivation
Two exact routes to the same degree-two polynomial-kernel value For x equals 2 comma 1 and x prime equals 1 comma 3, the explicit route constructs two six-dimensional feature vectors and dots them to get 4 plus 12 plus 9 plus 4 plus 6 plus 1 equals 36. The kernel shortcut stays in two dimensions: x dot x prime equals 5, so open parenthesis 5 plus 1 close parenthesis squared also equals 36. The shortcut returns exactly the same scalar without materializing either feature vector. EXPLICIT FEATURE ROUTE · EXACT, BUT MORE WORK x = (2, 1) · x′ = (1, 3) MATERIALIZE BOTH 6-D VECTORS φ(x) = [4, 2√2, 1, 2√2, √2, 1] φ(x′) = [1, 3√2, 9, √2, 3√2, 1] φ(x) · φ(x′) 4 + 12 + 9 + 4 + 6 + 1 = 36 KERNEL ROUTE · SAME SCALAR, NO FEATURE VECTORS stay in R² x · x′ = 5 skip φ k(x, x′) (5 + 1)² = 36 same inner product: 36 exact shortcut, not an approximation
Read it this way: both routes return 36. The explicit route constructs six-coordinate features; the kernel route evaluates the polynomial directly. This skips feature construction without approximating the inner product. References: Burges's SVM tutorial and scikit-learn's SVM formulation.
Check: Which calculation gives the same 36 without materializing either six-dimensional feature vector?
Read it this way: The coordinate dot product is 4; the kernel (4+2)^2 is 36. This is an exact shortcut, not an approximation.
Does the compact route approximate the expanded feature result?

Change the Distance Scale

New task: Define similarity through distance rather than a fixed polynomial expansion.

Picture and change: Use the Gaussian RBF kernel , with .

Check: The example’s squared distance is . Bandwidths give and .

Use when: A distance-based smooth similarity matches the task. Over a continuous input domain, this kernel generally needs an infinite-dimensional exact feature space, not a finite polynomial map.

The Gram matrix

For points, the Gram matrix has entries and shape .

  • Kernel ridge regression: solve , then predict . This convention uses an unnormalized squared-error sum plus times the squared function norm.
  • Kernel PCA: eigendecompose the centered .
  • Gaussian processes: use as the covariance.
  • SVM: dual formulation depends only on and labels.

New task: Train when a dense Gram matrix exceeds the memory budget.

Picture and change: Approximate with landmark columns and their Gram matrix : .

Check: For and only the first landmark, . The second direction is lost; this is no longer the exact kernel trick.

Use when: Measured approximation error is acceptable. A float64 matrix with rows requires decimal GB before factorization or solver workspace. Dense direct solves cost cubic arithmetic; practical limits depend on hardware and solver structure.

What makes a valid kernel

A real kernel must be symmetric and give a positive-semidefinite Gram matrix for every finite input collection. Feature inner products satisfy this because

The converse supplies a Hilbert-space feature representation. Mercer expansions additionally require their own domain and regularity conditions.

Common kernels:

  • Linear: . The trivial case.
  • Polynomial: , with nonnegative integer degree and .
  • RBF / Gaussian: , with .
  • Laplacian: , with .
  • String kernels: count shared substrings.
  • Graph kernels: count shared subgraphs.

Sums, pointwise products, and nonnegative scalings preserve validity; arbitrary negative scaling does not.

Kernel trick in attention

An inner product in a common feature space is a linear kernel. Attention may use different query and key maps, producing an asymmetric cross-score matrix. Row-normalized attention is not automatically a symmetric PSD Gram matrix. Feature-map attention methods require their particular approximation and normalization guarantees; they are not all RBF random-feature substitutions.

Why deep learning won

Learned representations can adapt similarity to a task; a fixed kernel imposes a chosen geometry. Neither guarantees better results across all datasets. Kernels remain useful for structured prior similarity, Gaussian processes, controlled baselines, and theoretical analysis.

Common pitfalls

  • Missing coefficients. Omitting the square-root-two factors changes the polynomial feature inner product.
  • Uncontrolled scale. Feature units change RBF distances and the effective bandwidth.
  • Confusing shortcuts. An exact kernel evaluation and an approximate low-rank Gram matrix make different guarantees.
  • Overloaded terminology. A convolution filter called a kernel need not define a PSD similarity function.

Reconstruct

Does a symmetric similarity matrix always define a valid kernel?

No. is symmetric but has eigenvalue . For , its quadratic form is , which cannot be a squared feature norm.