Step 5 of 6
Kernel methods & support vector machines
The kernel trick: map data to higher dimensions without computing the coordinates. SVMs, the kernel connection to attention, and why this idea never really left.
12 min read
Some data isn't linearly separable - no straight line can divide the classes. The kernel trick solves this by implicitly mapping data to a higher-dimensional space where a linear separator exists, without ever computing the coordinates in that space. It's one of the most elegant ideas in ML, and its echoes appear in attention mechanisms and modern feature spaces.
The problem: non-linear boundaries#
Linear classifiers draw straight decision boundaries. But many real datasets need curves:
- Points arranged in concentric circles
- XOR-style patterns
- Any classification where the boundary bends
You could manually engineer polynomial features (x₁², x₁x₂, x₂², ...), but this explodes combinatorially and you have to guess which features to create. The kernel trick automates this - and it works in infinite-dimensional spaces.
Support vector machines - maximum margin#
A Support Vector Machine (SVM) finds the hyperplane that separates two classes with the maximum margin - the widest possible gap between the classes:
SVM: find the hyperplane w·x + b = 0 that maximizes the margin 2/‖w‖ while correctly classifying all training points.
The support vectors are the training points closest to the decision boundary - they're the only points that matter. Move any other point and the boundary doesn't change. This makes SVMs robust to outliers far from the boundary.
- Support vectors
- The few training points that sit exactly on the margin boundary - they alone determine where the decision boundary falls.
- Hyperplane
- A flat decision surface in high-dimensional space - a line in 2D, a plane in 3D, and so on.
- Maximum margin
- The widest possible gap between two classes that a linear separator can achieve - wider margins tend to generalize better.
The soft margin - handling noise#
Real data isn't perfectly separable. The soft-margin SVM allows some points to violate the margin, with a penalty for each violation:
Soft-margin SVM: allow margin violations ξᵢ, penalized by C. Large C = narrow margin, few violations. Small C = wide margin, more violations.
The hyperparameter C controls the tradeoff: large C penalizes violations heavily (narrow margin, potentially overfitting); small C allows more violations (wider margin, potentially underfitting).
The kernel trick#
Instead of explicitly computing features in a high-dimensional space, the kernel trick lets you work with dot products between transformed points without ever computing the transformation:
The kernel trick: K(x, z) = φ(x) · φ(z), but you only compute K - never φ. The dot product in the high-dimensional space has a closed-form shortcut.
| Kernel | Formula | What it does |
|---|---|---|
| Linear | x · z | No transformation (standard dot product) |
| Polynomial | (x · z + c)^d | Maps to all polynomial features up to degree d |
| RBF (Gaussian) | exp(−γ‖x − z‖²) | Maps to infinite-dimensional space; smooth, local similarities |
| Sigmoid | tanh(αx · z + c) | Relates to neural networks (historically) |
The RBF kernel is the most commonly used: it measures how similar two points are based on their distance, with γ controlling the "reach" of each point. High γ means each point influences only nearby neighbors (complex, wiggly boundary); low γ means influence extends far (smoother boundary).
The dual formulation#
SVMs are typically solved in the dual form, which depends only on dot products between data points. Replace every dot product with a kernel, and the SVM learns a nonlinear boundary without ever computing the high-dimensional features:
Dual SVM: the decision function is a weighted sum of kernel evaluations between the test point and the support vectors.
Only support vectors have nonzero αᵢ - prediction time scales with the number of support vectors, not the size of the training set.
Kernels beyond SVMs#
The kernel trick isn't limited to SVMs. Any algorithm that can be written in terms of dot products can be "kernelized":
- Kernel PCA: nonlinear dimensionality reduction
- Kernel ridge regression: nonlinear regression
- Gaussian processes: Bayesian learning with kernels as the covariance function
- MMD (Maximum Mean Discrepancy): comparing distributions using kernels
The connection to attention#
There's a deep connection between kernels and attention mechanisms. The standard attention score between query q and key k is:
Attention score: the softmax of query-key dot products is closely related to an RBF kernel with learned features.
This looks like a scaled exponential of a dot product - structurally similar to the RBF kernel. Recent research on linear attention explicitly replaces softmax attention with kernel functions, trading the quadratic cost of standard attention for linear complexity.
- RBF kernel
- Radial Basis Function - measures similarity based on distance; maps data into an infinite-dimensional space with a simple closed-form formula.
- Kernel trick
- Computing dot products in a high-dimensional feature space without ever constructing the coordinates - only the pairwise similarity function is needed.
- Dual formulation
- Rewriting the SVM optimization so it depends only on dot products between data points - this is what makes kernelization possible.
- Gaussian processes
- A Bayesian approach that uses kernels as a prior over functions - provides predictions with built-in uncertainty estimates.
- Linear attention
- A family of efficient attention variants that replace softmax with kernel functions, reducing complexity from O(n²) to O(n).
Where it shows up in ML#
SVMs for small datasets
When you have hundreds to tens of thousands of labeled examples and clear geometric structure, SVMs with RBF kernels are a strong baseline.
Gaussian processes use kernels
GPs model uncertainty with a kernel-defined prior - the kernel encodes assumptions about smoothness, periodicity, and scale.
Kernel methods inspired attention
The softmax attention score is related to kernel evaluations; linear attention makes this connection explicit.
Feature maps vs. kernels
Random Fourier features approximate the RBF kernel with explicit (finite-dimensional) feature maps - enabling kernel-like learning at scale.