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
Derivation
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.