The points of one class form a small ring around the origin and the points of the other class form a wider ring around it. No straight line puts one ring on each side, yet a circle separates them easily. Adding features such as squares makes that circle a flat boundary in a larger space, and kernel methods compute what such features would give without ever listing them. This lesson builds the kernel trick from inner products, checks which similarity functions are valid kernels and fits a kernel support vector machine to two rings.
Key terms
Feature space
Definition A feature map is a function from the input space, here , into a real inner product space , called the feature space. The space may be with the dot product, or an infinite-dimensional space of sequences or functions with an inner product . A linear classifier in feature space has the score
with in . Its boundary is a hyperplane in , and the set of inputs with can be curved in . Preprocessing such as standardization happens before and is fitted on training data only.
In plain words A model that is linear in its parameters can still draw curved boundaries when it is linear in new features rather than in the raw inputs. The representation, not the learning rule, decides which shapes are possible. This is the idea behind polynomial features, and kernels make it practical when the new features are very many.
Example Eight constructed training points form two rings. The inner points and have label , and the outer points and have label . No line separates them. For any linear score , opposite points cancel, so the four inner scores add up to and the four outer scores add up to as well, while separation would need the first sum negative and the second positive. With the single feature the inner points have the value and the outer points , and the threshold separates them. In the original plane this boundary is the circle of radius .
Kernel function and kernel trick
Definition A kernel function on is a function of two inputs that equals an inner product of feature vectors,
for some feature map into a real inner product space and all inputs and . The linear kernel has . The kernel trick applies to a learning algorithm that uses the feature vectors, in training and in prediction, only through their inner products. Replacing every inner product by then runs the algorithm in the feature space without computing . The dual forms of the support vector machine and of ridge regression are algorithms of this kind.
In plain words A kernel answers one question about two inputs, the inner product of their feature vectors, and many algorithms never ask anything else. Evaluating the kernel can be far cheaper than building the features. The monomials of degree at most in inputs number , while the matching polynomial kernel needs one dot product of length . The cost moves elsewhere, since the algorithm now works with every pair of training points.
Example The function on is a kernel with the three features , because expanding gives exactly . For and the kernel gives . The features and give the same value, .
Gram matrix and positive semidefinite kernel
Definition For a function and points , the Gram matrix, also called the kernel matrix, is the matrix with entries . A function is a positive semidefinite kernel if it is symmetric, , and for every , every choice of points and every in
which for a symmetric matrix means that all its eigenvalues are nonnegative. Every kernel function has this property, since
Conversely, by the Moore-Aronszajn theorem, every positive semidefinite kernel equals for some feature map into a complete inner product space, a Hilbert space. So the valid kernels are exactly the positive semidefinite ones. Two consequences are and the Cauchy-Schwarz inequality . One positive semidefinite Gram matrix on one data set does not make a kernel, because the condition must hold for every finite set of points.
In plain words Calling a number a similarity does not make it an inner product. A kernel must behave the way inner products do, so no weighted combination of feature vectors may have a negative squared length. This condition also makes the support vector machine dual a concave maximization with a reliable global optimum. A similarity table that fails it has no feature-space reading, and an algorithm that treats it as a kernel loses its guarantees.
Example For the points and , the linear kernel gives the Gram matrix with rows and , whose determinant and trace are both positive, so both eigenvalues are positive. By contrast, no kernel gives any two points the Gram matrix with rows and . Its eigenvalues are and , and the vector gives
which would be the squared length of . It also breaks the Cauchy-Schwarz inequality, since .
Polynomial kernel
Definition For a whole number and a constant , the polynomial kernel on is
It is positive semidefinite, because sums, products and nonnegative multiples of positive semidefinite kernels are again positive semidefinite, and constants are kernels. When its features are all monomials of degree at most , each with a fixed positive weight, and when they are the monomials of degree exactly . A classifier that is linear in these features has a boundary given by a polynomial equation of degree at most in the inputs. With the function can fail to be a kernel, as shows, since the single point then has the Gram matrix with a negative entry. Some software writes with and , which is a positive multiple of the form above with .
In plain words The polynomial kernel fits a linear model on all products of up to input features without forming them. The degree sets the flexibility. Because inputs are multiplied together and raised to powers, a feature on a much larger scale than the others can dominate the kernel values, so the features are standardized first, with the scaler fitted on the training data.
Example For and on , expanding shows that the kernel is the dot product of the six features
For and the kernel gives . The features are and , and their dot product is also . The circle from the ring example is linear in these features, so this kernel can represent it.
Radial basis function kernel
Definition For a parameter , the radial basis function (RBF) kernel, also called the Gaussian kernel, is
for all inputs and . It can also be written as with a bandwidth and . It has , takes values in and decreases as the distance between the inputs grows. It is positive semidefinite for every , its feature space is infinite-dimensional, and its Gram matrix on distinct points is positive definite. So the feature vectors of any finite set of distinct points are linearly independent, and every labeling of distinct training points is linearly separable in this feature space.
In plain words The RBF kernel calls two points similar when they are close in the input features. The parameter sets the distance scale. A large makes similarity fall off over short distances and allows very local boundaries that can follow noise, and a small makes near and far points look alike and gives smooth boundaries that can miss real structure. These are tendencies, not guaranteed rankings of error. With distinct training inputs and a large enough , zero training mistakes are always within reach, so they say little about an RBF model. The value of is chosen together with the penalty on validation data, never on the test set. Because the kernel depends on Euclidean distance, the units of the features matter, and the scaler is fitted on the training folds only.
Example With , which means , two points at distance have similarity , and two points at distance have similarity . For the eight ring points, gives off-diagonal Gram entries between about and , so the Gram matrix is close to the matrix of all ones and the kernel can hardly tell the points apart. With every off-diagonal entry is below , so the Gram matrix is close to the identity and each point is similar only to itself.
Kernel SVM
Definition A kernel SVM is a soft-margin support vector machine fitted in the feature space of a positive semidefinite kernel . With labels and penalty , its dual problem maximizes
subject to and . The fitted score is
and the prediction is when and otherwise. When some example has , it satisfies , and that equation gives the intercept. The dual follows from the primal soft-margin problem in feature space. Setting the derivatives of its Lagrangian to zero gives , the constraint from the unpenalized intercept and the bound from the slack variables, and every inner product that remains is a kernel value. Because is positive semidefinite, is concave and the dual is a convex optimization problem. Only the support vectors, the examples with , enter the score.
In plain words A kernel SVM classifies a new point by a weighted vote of its similarities to the support vectors, positive for one class and negative for the other, plus a constant. It keeps the margin, the hinge loss and the penalty of the linear SVM and gains the shapes of the kernel. Training works with all pairs of training points, and the fitted model stores its support vectors. The kernel, its parameters and are chosen by cross-validation on the training data, with scaling fitted inside each fold, and the chosen pipeline is evaluated once on the test set.
Example Fit the eight ring points with the RBF kernel, and . Turning the plane by a quarter turn maps the data set to itself, so by concavity of some optimal gives all inner points one weight and all outer points another, and the constraint makes the two weights equal. With every the dual becomes
where , so , which lies between and . All eight points are support vectors on their margin hyperplanes, and the condition gives from either ring. New points at degrees score about at radius and about at radius , so both receive the label of their ring. Numerically the score changes sign exactly once along each ray from the origin, at a radius from about on the axes to about on the diagonals, so the boundary is a closed curve around the origin that is close to a circle but not exactly one. Far from every training point the score approaches , so such points are assigned to the outer class. A linear SVM with on the same points has the optimal weight vector , and every intercept from to is optimal, so it predicts one class everywhere and gets of the training points right. These are training results on constructed data, not evidence about new data.
Check yourself
- Two points are at Euclidean distance . Find their RBF similarity for and for , and say which setting gives the more local model. An RBF SVM with classifies every training point correctly but does much worse on the validation data. What should be checked and tried next?
Show the answer
For the similarity is , and for it is . The larger treats the same pair as much less similar, so each training point influences a smaller neighborhood and the boundary can bend more locally.
Perfect training accuracy with poor validation accuracy suggests overfitting, and with distinct training inputs and a large enough an RBF model can always reach zero training mistakes, so the training result carries little weight. Candidate experiments are a smaller or a smaller , which penalizes violations less. They should be compared by cross-validation within the training data, with the scaler fitted inside each training fold. The split itself deserves a check too, for leakage, for groups or time order that the split ignores, and for differences between the training and validation populations or label quality. The test set stays untouched until one final pipeline has been chosen.
- A similarity table for three items gives each item similarity with itself, between the first and second and between the second and third, and between the first and third. The table is symmetric and every off-diagonal entry is at most in absolute value. Can it be the Gram matrix of a kernel?
Show the answer
No. Take . The three diagonal terms contribute . Each of the three pairs appears twice, with the products , and , so the pairs contribute and
which is negative. The eigenvalues are about , and . A kernel Gram matrix would make this number the squared length of , which cannot be negative. Symmetry and the pairwise Cauchy-Schwarz checks are necessary but not sufficient. Here every self-similarity is , so the feature vectors would have length and each similarity would be the cosine of an angle. The value means an angle of about degrees, so the first and third vectors could be at most about degrees apart, with cosine at least , while the value needs about degrees.
- Fit an RBF kernel SVM with and to two training points, with label and with label . Find the dual weights and the intercept, classify the point , and state for which values of this solution holds.
Show the answer
The constraint gives . The kernel value between the points is , and , so
Setting the derivative to zero gives
which lies between and . Both points are therefore support vectors on their margin hyperplanes. The condition gives .
The point has squared distances and to the two training points, so
and it is classified . Every point with is equally far from both training points, so its score is exactly and the convention assigns it .
The solution holds whenever . For a smaller the weights are capped at , both points end up inside the margin, and the formula for no longer applies.