Just to clarify, these contents are mainly summarized from the course I took: “Fundamental of Big Data Analytics”, taught by Prof. Mathar Rudolf. For for information please visit: https://www.ti.rwth-aachen.de
Recall of the SVM primal problem and dual problem:
The the last post we introduced how to apply Lagrangian duality to SVM and how to get the primal optimum once we get the dual optimum. In this post we mainly discuss how to solve the dual problem and get the dual optimum.
Gradient Descent Algorithm for Dual Problem
To apply GD to SVM, we need to reformulate the objective function of the dual problem. Our new objective function will be:
where is the weighting factor for the constraint . For the constraint , we can satisfy this constraint by clipping into the region after each back propagation during gradient descent.
Discussion: why not also put the constraints also into the loss function by introducing an extra hinge loss term? Then the final loss function will be:
This is reasonable in theory but not so feasible in practice. This will introduce one extra hyper parameter , and we will be lost in endlessly fine tuning and balancing the hyper parameters and . Test results also show that achieving the constraint using clipping is efficient and this method also easily support more general cases of SVM with penalty terms. This will be discussed later.
Based on the loss function, We can compute the gradient:
We define a function . To maintain the consistence with future posts, we can this function as kernel function. Given a training dataset , we can get a kernel matrix:
Then the gradient can be expressed by the kernel matrix:
where , with the element being 1 and other elements being 0. The sign represents the element-wise multiplication two vectors and .
We can also write the expression of the gradient of with respect to the whole vector :
In practice, when we implement the gradient descent algorithm, we don’t need to compute in each iteration, since does not rely on . Instead, we can simply compute before applying gradient descent and store it in the memory, and call it each time when computing the gradient.
Another implicit advantage of using such a kernel matrix expression is that such a definition can be extended into a broader definition of SVM – SVM with kernels, where we can give a more sophisticated definition to the kernel function , instead of just vector dot product. But even in that case, the expression of the gradient still remains the same. We just simply pre-calculate the kernel matrix based on the new definition of kernel function, and then apply gradient descent algorithm to find the optimal solution. We will discuss kernel SVM in the future posts.
Implementation and Experiments
I implement the Gradient Descent algorithm to compute the dual optimum and use it to solve the original SVM optimization problem. The code is available in my github SupportVectorMachine/gd-dual-svm.py. The change of the hyperplane over iterations is shown in figure Hyperplane Over Iteration
In the above figure, the points with solid color are the support vectors. As the training goes on, more and more points are excluded from the support vector set. Finally there are only 3 support vectors. The finally separating hyperplane is obviously the optimal separating hyperplane with maximized margin.
One important feature of the Gradient Descent Algorithm is that in each iteration there is a matrix vector multiplication , with a time complexity . This might be computationally challenging if is large.
Apart from the gradient descent method, there is another method called Sequential Minimal Optimization (SMO), which is a more efficient and specialized solution. We will discuss that in the following posts. Before we go further, I would like to introduce the SVM in more general cases.