An Introduction to Support Vector Machines (SVM): kernel functions

支持向量机(SVM)概述:核函数

Posted by Gu on June 27, 2019

Recall of the Slack SVM dual problem:

Dual Problem

$$ \max_{\lambda, \mu} \sum_{i=1}^{n}\lambda_i - \frac{1}{2}\sum_{i,j}\lambda_i \lambda_j y_i y_j \mathbf{x}_i^T\mathbf{x}_j\\ \begin{align} s.t.\ & 0 \leq \lambda_i \leq C \\ &\sum_{i=1}^{n} \lambda_i y_i =0 \end{align} $$

Suppose that we have solved the dual problem and get the dual optimum. Let represent the support set related with ; represent the support set related with . Meanwhile, we define and . Then we can compute the primal optimum:

$$ \mathbf{w}^\star = \sum_{i\in S_w}\lambda_i^\star y_i \mathbf{x}_i\\ b^\star= y_j - {\mathbf{w}^\star}^T\mathbf{x}_j = y_j - \sum_{i\in S_w}\lambda_i^\star y_i \mathbf{x}_i^T \mathbf{x}_j \ , \ j\in S_b\\ $$

Given a new point , we can perform classification by computing:

$$ \begin{align} \hat{y} &= {\mathbf{w}^\star}^T \mathbf{x} + b^\star\\ &=\sum_{i\in S_w} \lambda^\star_i y_i \mathbf{x}_i^T \mathbf{x} + b^\star\\ \end{align} $$

According to the formulas above, we notice that in the dual problem, computation of and classification of new points, always appears as a whole.

SVM with kernel functions

Mapping points to a higher dimensional space

In some cases, if the points is not linearly separable in current space, they are possibly linearly separable if we map them into the higher dimension.

Mapping points from 2d to 3d to make them linearly separable.

We define as a mapping function which maps low dimensional data to a high dimensional data. We can first map our data , then solve the dual problem:

$$ \max_{\lambda, \mu} \sum_{i=1}^{n}\lambda_i - \frac{1}{2}\sum_{i,j}\lambda_i \lambda_j y_i y_j \phi(\mathbf{x}_i)^T \phi(\mathbf{x}_j)\\ \begin{align} s.t.\ & 0 \leq \lambda_i \leq C \\ &\sum_{i=1}^{n} \lambda_i y_i =0 \end{align} $$

We notice that in the dual problem, computing and performing classification, always appears as a whole. Therefore, we can avoid computing the exact form of , but instead directly explore the function for the inner product of two mapped points :

$$ K_{i,j}=K(\mathbf{x}_i, \mathbf{x}_j)=<\phi(\mathbf{x}_i), \phi(\mathbf{x}_j)> $$

We call as the kernel function.

What is a valid kernel function?

A kernel function is valid if there exists a mapping function , such that it holds for any .

Moreover, there is an equivalent conclusion on the validness of a kernel function.

A kernel function is valid if for any samples , the kernel matrix is non-negative definite.

Examples of Kernel functions

  1. Polynomial kernel function

    It can be proven that this function is equivalent to first mapping points to higher dimensional space and then computing the inner product.

  2. Gaussian Kernel

    Applying Gaussian kernel is equivalent to first mapping points to a infinitely high dimensional space and then computing the inner product. This can be understood by the Taylor expansion of the exponential function. For detailed explanation please see SVM中,高斯核为什么会把原始维度映射到无穷多维?

Dual problem with kernel function

With the definition of the kernel function, we can rewrite the dual problem and classification task as following.

Dual Problem

$$ \max_{\lambda, \mu} \sum_{i=1}^{n}\lambda_i - \frac{1}{2}\sum_{i,j}\lambda_i \lambda_j y_i y_j K(\mathbf{x}_i, \mathbf{x}_j) \\ \begin{align} s.t.\ & 0 \leq \lambda_i \leq C \\ &\sum_{i=1}^{n} \lambda_i y_i =0 \end{align} $$

Suppose that we have solved the dual problem and get the dual optimum. Let represent the support set related with ; represent the support set related with . Meanwhile, we define and . Then we can compute the primal optimum:

$$ \mathbf{w}^\star = \sum_{i\in S_w}\lambda_i^\star y_i \phi(\mathbf{x}_i)\\ b^\star= y_j - {\mathbf{w}^\star}^T\phi(\mathbf{x}_j) = y_j - \sum_{i\in S_w}\lambda_i^\star y_i K(\mathbf{x}_i, \mathbf{x}_j) \ , \ j\in S_b\\ $$

Given a new point , we can perform classification by computing:

$$ \begin{align} \hat{y} &= {\mathbf{w}^\star}^T \phi(\mathbf{x}) + b^\star\\ &=\sum_{i\in S_w} \lambda^\star_i y_i K(\mathbf{x}_i, \mathbf{x}) + b^\star\\ \end{align} $$

See, in fact is never really computed, since we are only interested in the kernel function!

Solve the dual problem using Gradient Descent Algorithm

We can solve the dual problem using gradient descent algorithm as introduced in the post An Introduction to Support Vector Machines (SVM): Dual problem solution using GD. Just simply select a kernel function, such as polynomial or Gaussian, compte the Kernel matrix for the training dataset, compute the gradient and then perform back propagation to get the dual optimum . After getting , we can compute the primal optimum and perform classification on new points using the equations above.

In the next post, I will introduce how to solve the dual problem using Sequential Minimal Optimization (SMO).