iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📈

Thinking about Optimization (2) — SVM

に公開

Purpose

Continuing from the previous article, this time we will cover SVM (Support Vector Machine).

You might have read about SVM in books and vaguely understood that it is optimized to maximize the margin between support vectors and the decision boundary, leading you to simply use the API. Since this often leaves the underlying process unclear, I would like to actually work through the calculations manually.

Scope of this article

We will find the decision boundary for a dataset consisting of three points. We will formulate the primal problem and derive the dual problem from it. By examining these specifically, we aim to gain a better understanding of how it works.

Dataset

I considered three patterns, but I felt the following one was best from an educational perspective and for ease of calculation, so I will use it. While other cases ultimately lead to similar results, hand-calculations showed that the tedious parts vary each time, making the article unnecessarily long without adding much essence. Therefore, I decided to narrow the problem setting down to one type.

As shown below:

We prepare a dataset \{ (\xi_1, t_1), (\xi_2, t_2), (\xi_3, t_3) \}. Here, t_i \in \{-1, +1\}.

Primal Problem

For a while, I will write it out following PRML.

\begin{align*} y(\mathbf{x}) &= \mathbf{w}^T \mathbf{x} + b = \braket{\mathbf{w}, \mathbf{x}} + b = w^1 x^1 + w^2 x^2 + b \tag{1} \end{align*}

We want to find the decision boundary in this form. We will classify the two classes based on \operatorname{sgn}(y(\mathbf{x})) > 0 and \operatorname{sgn}(y(\mathbf{x})) < 0.

Since the parameter values for the decision boundary have not yet been determined, we will call it a "hyperplane" instead of a boundary[1].

The distance from \xi_i to the hyperplane is given by |y(\xi_i)| / |\mathbf{w}| [2]. Note that since the absolute value of the label t_i is 1, |y(\xi_i)| = t_i y(\xi_i) holds. Then, the distance from \xi_i to the hyperplane is:

\begin{align*} \frac{t_i y(\xi_i)}{|\mathbf{w}|} = \frac{t_i (\mathbf{w}^T \xi_i + b)}{|\mathbf{w}|} \end{align*}

Since the support vectors are those for which this is minimized, the distance to the support vectors is:

\begin{align*} \min_i \frac{t_i (\mathbf{w}^T \xi_i + b)}{|\mathbf{w}|} = \frac{1}{|\mathbf{w}|} \min_i [t_i (\mathbf{w}^T \xi_i + b)] \end{align*}

Since we want to maximize this value—that is, the distance between the support vector and the hyperplane (the margin)—the parameters for the decision boundary can be found by solving the following Max-Min problem:

\begin{align*} \mathbf{w}^*,b^* = \argmax_{\mathbf{w},b} \frac{1}{|\mathbf{w}|} \min_i [t_i (\mathbf{w}^T \xi_i + b)] \end{align*}

Since |y(\xi_i)| / |\mathbf{w}| is invariant under the scaling \mathbf{w} \to \kappa \mathbf{w} and b \to \kappa b, we can adjust \kappa to assume that "|y(\xi_i)| = t_i (\mathbf{w}^T \xi_i + b) = 1 for the \xi_i closest to the hyperplane." In this case, t_i (\mathbf{w}^T \xi_i + b) \geq 1 holds for all data points.

Under these conditions, the remaining task is to maximize \frac{1}{|\mathbf{w}|}, but minimizing \frac{1}{2} |\mathbf{w}|^2 is considered an equivalent condition. Summarizing up to this point, we obtain the quadratic programming problem, which is a convex optimization problem, as the primal problem:

\begin{align*} \min \frac{1}{2} |\mathbf{w}|^2 \quad\text{subject to}\quad t_i (\mathbf{w}^T \xi_i + b) \geq 1 \tag{2} \end{align*}

Dual Problem

Since it is difficult to solve the primal problem as it is, we will find an equivalent problem called the dual problem and solve it. According to the reference [Kernel Methods], when solving a convex optimization problem, considering the dual problem is often useful.

Here, instead of using the PRML notation exactly, we will use \lambda = \{ \lambda_i \} \subset \mathbb{R}_{\geq 0} as the Lagrange multipliers. As is common in the Lagrange multiplier method, we consider the following Lagrangian function, which is the sum of the expression to be optimized and the constraints multiplied by the Lagrange multipliers.

\begin{align*} L(\mathbf{w}, b, \lambda) = \frac{1}{2} |\mathbf{w}|^2 - \sum_i \lambda_i \{ t_i (\mathbf{w}^T \xi_i + b) - 1 \} \tag{3} \end{align*}

To find the points that give the extreme values, we take the partial derivatives of L and set them to 0; from \frac{\partial L}{\partial \mathbf{w}} = 0 and \frac{\partial L}{\partial b} = 0, we obtain:

\begin{align*} \mathbf{w} &= \sum_i \lambda_i t_i \xi_i \\ 0 &= \sum_i \lambda_i t_i \tag{4} \end{align*}

Substituting these into equation (3), the coefficient of b disappears due to the second equation of (4), so:

\begin{align*} & \frac{1}{2} \sum_{j, k} (\lambda_j t_j \xi_j^T) (\lambda_k t_k \xi_k) - \sum_i \left[ \lambda_i t_i \{ (\sum_k \lambda_k t_k \xi_k^T) \xi_i + b \} - \lambda_i \right] \\ = & \sum_i \lambda_i - \frac{1}{2} \sum_{j, k} (\lambda_j t_j \xi_j^T) (\lambda_k t_k \xi_k) \end{align*}

Organizing the indices and setting the last expression as \tilde{L}(\lambda):

\begin{align*} \tilde{L}(\lambda) = \sum_i \lambda_i - \frac{1}{2} \sum_{i, j} \lambda_i \lambda_j t_i t_j \xi_i^T \xi_j \end{align*}

The following problem using this \tilde{L}(\lambda) is called the dual problem:

\begin{align*} \max \tilde{L}(\lambda) \quad\text{subject to}\quad \lambda_i \geq 0,\ i = 1,2,\dots \tag{5} \end{align*}

...

Skipping a lot here

It is a bit hard to understand why the task of maximizing \tilde{L}(\lambda) suddenly appears, but my current understanding is that it is to leverage the concept of strong duality. That is:

By reading pp. 212–215 of [Kernel Methods], we see that since the primal problem is a convex optimization problem and satisfies the Slater's condition[3], strong duality holds. Therefore, the optimal value p^* of the primal problem coincides with the optimal value d^* of the dual problem, and for a certain \lambda^* (= \text{argmax}_\lambda \tilde{L}(\lambda)), it equals \tilde{L}(\lambda^*).

In short, in this case, we just need to solve the dual problem to find \lambda^* = \{ \lambda^*_i \}. Once \lambda^* = \{ \lambda^*_i \} is found, we substitute it into the first equation of (4) to determine \mathbf{w}, and then by substituting that \mathbf{w} into equation (1), the decision boundary is determined by:

\begin{align*} y(\mathbf{x}) = (\sum_i \lambda^*_i t_i \xi_i^T) \mathbf{x} + b = \sum_i \lambda^*_i t_i \braket{\xi_i, \mathbf{x}} + b \tag{6} \end{align*}

Since b disappeared during the derivation of the dual problem, only the normal vector of the decision boundary can be obtained from \lambda^*. The value of b is determined based on the assumption that "|y(\xi_i)| = t_i (\mathbf{w}^T \xi_i + b) = 1 for the \xi_i closest to the hyperplane."

Example

Let's find the decision boundary for the three-point dataset mentioned at the beginning.

Writing equation (4) specifically:

\begin{align*} \mathbf{w} &= \begin{pmatrix} 2 \lambda_1 - 3 \lambda_2 \\ \lambda_1 + \lambda_2 + \lambda_3 \end{pmatrix}, \\ 0 &= \lambda_1 - \lambda_2 + \lambda_3 \end{align*}

Also, calculating the inner products between data points beforehand:

\braket{\xi_1, \xi_1} = 5, \braket{\xi_2, \xi_2} = 10, \braket{\xi_3, \xi_3} = 1, \braket{\xi_1, \xi_2} = 5, \braket{\xi_2, \xi_3} = -1, \braket{\xi_3, \xi_1} = 1

Therefore,

\begin{align*} \tilde{L}(\lambda) &= \lambda_1 + \lambda_2 + \lambda_3 - \frac{1}{2}(-10 \lambda_1 \lambda_2 + 2 \lambda_2 \lambda_3 + 2 \lambda_3 \lambda_1 + 5 \lambda_1^2 + 10 \lambda_2^2 + \lambda_3^2) \\ &= \cdots \\ &= - \frac{13}{2} \left( \lambda_2 -\frac{2}{13} (1 + 3 \lambda_1) \right)^2 - \frac{8}{13} (\lambda_1 - \frac{3}{4})^2 + \frac{1}{2} \leq \frac{1}{2} \end{align*}

We maximize this. The possible maximum value is \frac{1}{2}.

First, for the second term on the right-hand side, \lambda_1 = \frac{3}{4} is a candidate. In this case, setting the first term on the right-hand side to 0, we find \lambda_2 = \frac{1}{2}. However, \lambda_3 = \lambda_2 - \lambda_1 = \frac{1}{2} - \frac{3}{4} = - \frac{1}{4} < 0, which violates the constraints. Thus, we see that the optimal solution cannot be found simply.

Ultimately, though the calculation is a bit tedious, the solution is found in the shaded region, and by maximizing - \frac{13}{2} \left( x -\frac{2}{13} (1 + 3 x) \right)^2 - \frac{8}{13} (x - \frac{3}{4})^2, we get x = \frac{2}{5}. That is, \lambda_1 = \lambda_2 = \frac{2}{5}, \lambda_3 = 0 is the optimal solution.

At this point, from equation (6), the decision boundary is:

\begin{align*} y(\mathbf{x}) &= \frac{2}{5} \braket{\mathbf{x}, \xi_1} - \frac{2}{5} \braket{\mathbf{x}, \xi_2} + b \\ &= \frac{2}{5} (2 x^1 + x^2) - \frac{2}{5} (3 x^1 - x^2) + b \\ &= - \frac{2}{5} x^1 + \frac{4}{5} x^2 + b \end{align*}

Determining b from the assumption that "|y(\xi_i)| = t_i (\mathbf{w}^T \xi_i + b) = 1 for the \xi_i closest to the hyperplane" gives b = 1. Therefore, the final decision boundary is:

\begin{align*} y(\mathbf{x}) &= \frac{2}{5} \braket{\mathbf{x}, \xi_1} - \frac{2}{5} \braket{\mathbf{x}, \xi_2} + b \\ &= \frac{2}{5} (2 x^1 + x^2) - \frac{2}{5} (3 x^1 - x^2) + b \\ &= - \frac{2}{5} x^1 + \frac{4}{5} x^2 + 1 \end{align*}

y(\mathbf{x}) = 0 is indicated by the red line in the figure at the beginning.

Implementing in Python

Since we are here, let's also take a look at the implementation in Python. We will use scikit-learn.

from sklearn.svm import LinearSVC

X = np.array([
    [2, 1],
    [3, -1],
    [0, 1],
])
y = np.array([1, -1, 1])

clf = LinearSVC(C=.1)
result = clf.fit(X, y)

I think something like this should work. Since I wasn't sure how to visualize the decision boundary, I referred to [Python] Thoroughly Explaining the Theory and Implementation of Support Vector Machines (SVM).

import mglearn

mglearn.plots.plot_2d_separator(result, X, fill=False)
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)

plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')

plt.show()

I suspect the formulas used internally might be slightly different, resulting in the decision boundary shown below, but I believe it is essentially not much different from what we calculated this time. Or so I would like to believe[4].

Summary

We attempted to find the SVM decision boundary by manual calculation for a dataset that is as simple as possible. Even for such a small dataset, the calculation is very demanding, making it clear that realistic problems cannot be solved by hand. Therefore, while we usually handle it with APIs as before, I think it's not a bad thing to know the ideas behind it.

References

  • [PRML] C. M. Bishop, Pattern Recognition and Machine Learning, pp.325-331, Springer, 2006
  • [Kernel Methods] Kenji Fukumizu, Introduction to Kernel Methods (カーネル法入門), Asakura Shoten, 2010
脚注
  1. In this case, it is a regular plane. ↩︎

  2. Since the normal vector of the hyperplane is \mathbf{w}, we just need to find t such that \xi_i + \mathbf{w} t lies on the hyperplane. At this point, |\mathbf{w} t| = |\mathbf{w}| |t| is the distance we want to calculate. Since 0 = y(\xi_i + \mathbf{w} t) = \mathbf{w}^T \xi_i + \mathbf{w}^T \mathbf{w} t + b = y(\xi_i) + |\mathbf{w}|^2 t, the distance to the hyperplane is |\mathbf{w}| |t| = |y(\xi_i)| / |\mathbf{w}|. ↩︎

  3. While an extreme dataset might not satisfy it, the three-point dataset considered here has sufficient separability and satisfies the condition. ↩︎

  4. Since it's a manual calculation, there is of course a high possibility that I made a mistake. ↩︎

GitHubで編集を提案

Discussion