Docsity

Support vector machines, Lecture notes of Computer Science

Toggle sidebar
bg1
Chapter 6
Support vector machines
This set of notes presents the Support Vector Machine (SVM) learning al-
gorithm. SVMs are among the best (and many believe are indeed the best)
“off-the-shelf” supervised learning algorithms. To tell the SVM story, we’ll
need to first talk about margins and the idea of separating data with a large
“gap.” Next, we’ll talk about the optimal margin classifier, which will lead
us into a digression on Lagrange duality. We’ll also see kernels, which give
a way to apply SVMs efficiently in very high dimensional (such as infinite-
dimensional) feature spaces, and finally, we’ll close off the story with the
SMO algorithm, which gives an efficient implementation of SVMs.
6.1 Margins: intuition
We’ll start our story on SVMs by talking about margins. This section will
give the intuitions about margins and about the “confidence” of our predic-
tions; these ideas will be made formal in Section 6.3.
Consider logistic regression, where the probability p(y= 1|x;θ) is mod-
eled by hθ(x) = g(θTx). We then predict “1” on an input xif and only if
hθ(x)≥0.5, or equivalently, if and only if θTx≥0. Consider a positive
training example (y= 1). The larger θTxis, the larger also is hθ(x) = p(y=
1|x;θ), and thus also the higher our degree of “confidence” that the label is 1.
Thus, informally we can think of our prediction as being very confident that
y= 1 if θTx0. Similarly, we think of logistic regression as confidently
predicting y= 0, if θTx0. Given a training set, again informally it seems
that we’d have found a good fit to the training data if we can find θso that
θTx(i)0 whenever y(i)= 1, and θTx(i)0 whenever y(i)= 0, since this
would reflect a very confident (and correct) set of classifications for all the
60
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Support vector machines and more Lecture notes Computer Science in PDF only on Docsity!

Chapter 6

Support vector machines

This set of notes presents the Support Vector Machine (SVM) learning al- gorithm. SVMs are among the best (and many believe are indeed the best) “off-the-shelf” supervised learning algorithms. To tell the SVM story, we’ll need to first talk about margins and the idea of separating data with a large “gap.” Next, we’ll talk about the optimal margin classifier, which will lead us into a digression on Lagrange duality. We’ll also see kernels, which give a way to apply SVMs efficiently in very high dimensional (such as infinite- dimensional) feature spaces, and finally, we’ll close off the story with the SMO algorithm, which gives an efficient implementation of SVMs.

6.1 Margins: intuition

We’ll start our story on SVMs by talking about margins. This section will give the intuitions about margins and about the “confidence” of our predic- tions; these ideas will be made formal in Section 6.3. Consider logistic regression, where the probability p(y = 1|x; θ) is mod- eled by hθ(x) = g(θT^ x). We then predict “1” on an input x if and only if hθ(x) ≥ 0 .5, or equivalently, if and only if θT^ x ≥ 0. Consider a positive training example (y = 1). The larger θT^ x is, the larger also is hθ(x) = p(y = 1 |x; θ), and thus also the higher our degree of “confidence” that the label is 1. Thus, informally we can think of our prediction as being very confident that y = 1 if θT^ x  0. Similarly, we think of logistic regression as confidently predicting y = 0, if θT^ x  0. Given a training set, again informally it seems that we’d have found a good fit to the training data if we can find θ so that θT^ x(i)^  0 whenever y(i)^ = 1, and θT^ x(i)^  0 whenever y(i)^ = 0, since this would reflect a very confident (and correct) set of classifications for all the

training examples. This seems to be a nice goal to aim for, and we’ll soon formalize this idea using the notion of functional margins. For a different type of intuition, consider the following figure, in which x’s represent positive training examples, o’s denote negative training examples, a decision boundary (this is the line given by the equation θT^ x = 0, and is also called the separating hyperplane) is also shown, and three points have also been labeled A, B and C.



^ B^ 

A

C

Notice that the point A is very far from the decision boundary. If we are asked to make a prediction for the value of y at A, it seems we should be quite confident that y = 1 there. Conversely, the point C is very close to the decision boundary, and while it’s on the side of the decision boundary on which we would predict y = 1, it seems likely that just a small change to the decision boundary could easily have caused out prediction to be y = 0. Hence, we’re much more confident about our prediction at A than at C. The point B lies in-between these two cases, and more broadly, we see that if a point is far from the separating hyperplane, then we may be significantly more confident in our predictions. Again, informally we think it would be nice if, given a training set, we manage to find a decision boundary that allows us to make all correct and confident (meaning far from the decision boundary) predictions on the training examples. We’ll formalize this later using the notion of geometric margins.

this would not change hw,b(x) at all. I.e., g, and hence also hw,b(x), depends only on the sign, but not on the magnitude, of wT^ x + b. However, replacing (w, b) with (2w, 2 b) also results in multiplying our functional margin by a factor of 2. Thus, it seems that by exploiting our freedom to scale w and b, we can make the functional margin arbitrarily large without really changing anything meaningful. Intuitively, it might therefore make sense to impose some sort of normalization condition such as that ||w|| 2 = 1; i.e., we might replace (w, b) with (w/||w|| 2 , b/||w|| 2 ), and instead consider the functional margin of (w/||w|| 2 , b/||w|| 2 ). We’ll come back to this later. Given a training set S = {(x(i), y(i)); i = 1,... , n}, we also define the function margin of (w, b) with respect to S as the smallest of the functional margins of the individual training examples. Denoted by ˆγ, this can therefore be written: ˆγ = min i=1,...,n ˆγ(i).

Next, let’s talk about geometric margins. Consider the picture below:

A w

γ B

(i)

The decision boundary corresponding to (w, b) is shown, along with the vector w. Note that w is orthogonal (at 90◦) to the separating hyperplane. (You should convince yourself that this must be the case.) Consider the point at A, which represents the input x(i)^ of some training example with label y(i)^ = 1. Its distance to the decision boundary, γ(i), is given by the line segment AB. How can we find the value of γ(i)? Well, w/||w|| is a unit-length vector pointing in the same direction as w. Since A represents x(i), we therefore

find that the point B is given by x(i)^ − γ(i)^ · w/||w||. But this point lies on the decision boundary, and all points x on the decision boundary satisfy the equation wT^ x + b = 0. Hence,

wT

x(i)^ − γ(i)^

w ||w||

  • b = 0.

Solving for γ(i)^ yields

γ(i)^ =

wT^ x(i)^ + b ||w||

w ||w||

)T

x(i)^ +

b ||w||

This was worked out for the case of a positive training example at A in the figure, where being on the “positive” side of the decision boundary is good. More generally, we define the geometric margin of (w, b) with respect to a training example (x(i), y(i)) to be

γ(i)^ = y(i)

w ||w||

)T

x(i)^ +

b ||w||

Note that if ||w|| = 1, then the functional margin equals the geometric margin—this thus gives us a way of relating these two different notions of margin. Also, the geometric margin is invariant to rescaling of the parame- ters; i.e., if we replace w with 2w and b with 2b, then the geometric margin does not change. This will in fact come in handy later. Specifically, because of this invariance to the scaling of the parameters, when trying to fit w and b to training data, we can impose an arbitrary scaling constraint on w without changing anything important; for instance, we can demand that ||w|| = 1, or |w 1 | = 5, or |w 1 + b| + |w 2 | = 2, and any of these can be satisfied simply by rescaling w and b. Finally, given a training set S = {(x(i), y(i)); i = 1,... , n}, we also define the geometric margin of (w, b) with respect to S to be the smallest of the geometric margins on the individual training examples:

γ = min i=1,...,n γ(i).

6.4 The optimal margin classifier

Given a training set, it seems from our previous discussion that a natural desideratum is to try to find a decision boundary that maximizes the (ge- ometric) margin, since this would reflect a very confident set of predictions

Since multiplying w and b by some constant results in the functional margin being multiplied by that same constant, this is indeed a scaling constraint, and can be satisfied by rescaling w, b. Plugging this into our problem above, and noting that maximizing ˆγ/||w|| = 1/||w|| is the same thing as minimizing ||w||^2 , we now have the following optimization problem:

minw,b

||w||^2

s.t. y(i)(wT^ x(i)^ + b) ≥ 1 , i = 1,... , n

We’ve now transformed the problem into a form that can be efficiently solved. The above is an optimization problem with a convex quadratic ob- jective and only linear constraints. Its solution gives us the optimal mar- gin classifier. This optimization problem can be solved using commercial quadratic programming (QP) code.^1 While we could call the problem solved here, what we will instead do is make a digression to talk about Lagrange duality. This will lead us to our optimization problem’s dual form, which will play a key role in allowing us to use kernels to get optimal margin classifiers to work efficiently in very high dimensional spaces. The dual form will also allow us to derive an efficient algorithm for solving the above optimization problem that will typically do much better than generic QP software.

6.5 Lagrange duality

Let’s temporarily put aside SVMs and maximum margin classifiers, and talk about solving constrained optimization problems. Consider a problem of the following form:

minw f (w) s.t. hi(w) = 0, i = 1,... , l.

Some of you may recall how the method of Lagrange multipliers can be used to solve it. (Don’t worry if you haven’t seen it before.) In this method, we define the Lagrangian to be

L(w, β) = f (w) +

∑^ l

i=

βihi(w)

(^1) You may be familiar with linear programming, which solves optimization problems

that have linear objectives and linear constraints. QP software is also widely available, which allows convex quadratic objectives and linear constraints.

Here, the βi’s are called the Lagrange multipliers. We would then find and set L’s partial derivatives to zero:

∂L ∂wi

∂L

∂βi

and solve for w and β. In this section, we will generalize this to constrained optimization prob- lems in which we may have inequality as well as equality constraints. Due to time constraints, we won’t really be able to do the theory of Lagrange duality justice in this class,^2 but we will give the main ideas and results, which we will then apply to our optimal margin classifier’s optimization problem. Consider the following, which we’ll call the primal optimization problem:

minw f (w) s.t. gi(w) ≤ 0 , i = 1,... , k hi(w) = 0, i = 1,... , l.

To solve it, we start by defining the generalized Lagrangian

L(w, α, β) = f (w) +

∑^ k

i=

αigi(w) +

∑^ l

i=

βihi(w).

Here, the αi’s and βi’s are the Lagrange multipliers. Consider the quantity

θP (w) = max α,β : αi≥ 0 L(w, α, β).

Here, the “P” subscript stands for “primal.” Let some w be given. If w violates any of the primal constraints (i.e., if either gi(w) > 0 or hi(w) 6 = 0 for some i), then you should be able to verify that

θP (w) = max α,β : αi≥ 0

f (w) +

∑^ k

i=

αigi(w) +

∑^ l

i=

βihi(w) (6.1)

= ∞. (6.2)

Conversely, if the constraints are indeed satisfied for a particular value of w, then θP (w) = f (w). Hence,

θP (w) =

f (w) if w satisfies primal constraints ∞ otherwise. (^2) Readers interested in learning more about this topic are encouraged to read, e.g., R.

T. Rockarfeller (1970), Convex Analysis, Princeton University Press.

Under our above assumptions, there must exist w∗, α∗, β∗^ so that w∗^ is the solution to the primal problem, α∗, β∗^ are the solution to the dual problem, and moreover p∗^ = d∗^ = L(w∗, α∗, β∗). Moreover, w∗, α∗^ and β∗^ satisfy the Karush-Kuhn-Tucker (KKT) conditions, which are as follows:

∂ ∂wi

L(w∗, α∗, β∗) = 0 , i = 1,... , d (6.3)

∂ ∂βi

L(w∗, α∗, β∗) = 0 , i = 1,... , l (6.4)

α∗ i gi(w∗) = 0 , i = 1,... , k (6.5) gi(w∗) ≤ 0 , i = 1,... , k (6.6) α∗ i ≥ 0 , i = 1,... , k (6.7)

Moreover, if some w∗, α∗, β∗^ satisfy the KKT conditions, then w∗^ is a solution to the primal problem and α∗, β∗^ are solutions to the dual problem. We draw attention to Equation (6.5), which is called the KKT dual complementarity condition. Specifically, it implies that if α∗ i > 0, then gi(w∗) = 0. (I.e., the “gi(w) ≤ 0” constraint is active, meaning it holds with equality rather than with inequality.) Later on, this will be key for showing that the SVM has only a small number of “support vectors”; the KKT dual complementarity condition will also give us our convergence test when we talk about the SMO algorithm.

6.6 Optimal margin classifiers: the dual form

Note: The equivalence of optimization problem (6.8) and the optimization problem (6.12), and the relationship between the primary and dual variables in equation (6.10) are the most important take home messages of this section.

Previously, we posed the following (primal) optimization problem for find- ing the optimal margin classifier:

minw,b

||w||^2 (6.8)

s.t. y(i)(wT^ x(i)^ + b) ≥ 1 , i = 1,... , n

We can write the constraints as

gi(w) = −y(i)(wT^ x(i)^ + b) + 1 ≤ 0.

We have one such constraint for each training example. Note that from the KKT dual complementarity condition, we will have αi > 0 only for the train- ing examples that have functional margin exactly equal to one (i.e., the ones corresponding to constraints that hold with equality, gi(w) = 0). Consider the figure below, in which a maximum margin separating hyperplane is shown by the solid line.

The points with the smallest margins are exactly the ones closest to the decision boundary; here, these are the three points (one negative and two pos- itive examples) that lie on the dashed lines parallel to the decision boundary. Thus, only three of the αi’s—namely, the ones corresponding to these three training examples—will be non-zero at the optimal solution to our optimiza- tion problem. These three points are called the support vectors in this problem. The fact that the number of support vectors can be much smaller than the size the training set will be useful later. Let’s move on. Looking ahead, as we develop the dual form of the prob- lem, one key idea to watch out for is that we’ll try to write our algorithm in terms of only the inner product 〈x(i), x(j)〉 (think of this as (x(i))T^ x(j)) between points in the input feature space. The fact that we can express our algorithm in terms of these inner products will be key when we apply the kernel trick. When we construct the Lagrangian for our optimization problem we have:

L(w, b, α) =

||w||^2 −

∑^ n

i=

αi

[

y(i)(wT^ x(i)^ + b) − 1

]

Note that there’re only “αi” but no “βi” Lagrange multipliers, since the problem has only inequality constraints.

our optimization problem. Hence, we can solve the dual in lieu of solving the primal problem. Specifically, in the dual problem above, we have a maximization problem in which the parameters are the αi’s. We’ll talk later about the specific algorithm that we’re going to use to solve the dual problem, but if we are indeed able to solve it (i.e., find the α’s that maximize W (α) subject to the constraints), then we can use Equation (6.10) to go back and find the optimal w’s as a function of the α’s. Having found w∗, by considering the primal problem, it is also straightforward to find the optimal value for the intercept term b as

b∗^ = −

maxi:y(i)=− 1 w∗T^ x(i)^ + mini:y(i)=1 w∗T^ x(i) 2

(Check for yourself that this is correct.) Before moving on, let’s also take a more careful look at Equation (6.10), which gives the optimal value of w in terms of (the optimal value of) α. Suppose we’ve fit our model’s parameters to a training set, and now wish to make a prediction at a new point input x. We would then calculate wT^ x + b, and predict y = 1 if and only if this quantity is bigger than zero. But using (6.10), this quantity can also be written:

wT^ x + b =

( (^) n ∑

i=

αiy(i)x(i)

)T

x + b (6.14)

∑^ n

i=

αiy(i)〈x(i), x〉 + b. (6.15)

Hence, if we’ve found the αi’s, in order to make a prediction, we have to calculate a quantity that depends only on the inner product between x and the points in the training set. Moreover, we saw earlier that the αi’s will all be zero except for the support vectors. Thus, many of the terms in the sum above will be zero, and we really need to find only the inner products between x and the support vectors (of which there is often only a small number) in order calculate (6.15) and make our prediction. By examining the dual form of the optimization problem, we gained sig- nificant insight into the structure of the problem, and were also able to write the entire algorithm in terms of only inner products between input feature vectors. In the next section, we will exploit this property to apply the ker- nels to our classification problem. The resulting algorithm, support vector machines, will be able to efficiently learn in very high dimensional spaces.

6.7 Regularization and the non-separable case

The derivation of the SVM as presented so far assumed that the data is linearly separable. While mapping data to a high dimensional feature space via φ does generally increase the likelihood that the data is separable, we can’t guarantee that it always will be so. Also, in some cases it is not clear that finding a separating hyperplane is exactly what we’d want to do, since that might be susceptible to outliers. For instance, the left figure below shows an optimal margin classifier, and when a single outlier is added in the upper-left region (right figure), it causes the decision boundary to make a dramatic swing, and the resulting classifier has a much smaller margin.

To make the algorithm work for non-linearly separable datasets as well as be less sensitive to outliers, we reformulate our optimization (using ` 1 regularization) as follows:

minγ,w,b

||w||^2 + C

∑^ n

i=

ξi

s.t. y(i)(wT^ x(i)^ + b) ≥ 1 − ξi, i = 1,... , n ξi ≥ 0 , i = 1,... , n.

Thus, examples are now permitted to have (functional) margin less than 1, and if an example has functional margin 1 − ξi (with ξ > 0), we would pay a cost of the objective function being increased by Cξi. The parameter C controls the relative weighting between the twin goals of making the ||w||^2 small (which we saw earlier makes the margin large) and of ensuring that most examples have functional margin at least 1. As before, we can form the Lagrangian:

L(w, b, ξ, α, r) =

wT^ w + C

∑^ n

i=

ξi −

∑^ n

i=

αi

[

y(i)(xT^ w + b) − 1 + ξi

]

∑^ n

i=

riξi.

6.8.1 Coordinate ascent

Consider trying to solve the unconstrained optimization problem

max α W (α 1 , α 2 ,... , αn).

Here, we think of W as just some function of the parameters αi’s, and for now ignore any relationship between this problem and SVMs. We’ve already seen two optimization algorithms, gradient ascent and Newton’s method. The new algorithm we’re going to consider here is called coordinate ascent:

Loop until convergence: {

For i = 1,... , n, { αi := arg maxαˆi W (α 1 ,... , αi− 1 , αˆi, αi+1,... , αn). }

}

Thus, in the innermost loop of this algorithm, we will hold all the variables except for some αi fixed, and reoptimize W with respect to just the parameter αi. In the version of this method presented here, the inner-loop reoptimizes the variables in order α 1 , α 2 ,... , αn, α 1 , α 2 ,.. .. (A more sophisticated version might choose other orderings; for instance, we may choose the next variable to update according to which one we expect to allow us to make the largest increase in W (α).) When the function W happens to be of such a form that the “arg max” in the inner loop can be performed efficiently, then coordinate ascent can be a fairly efficient algorithm. Here’s a picture of coordinate ascent in action:

−2 −1.5 −1 −0.5 0 0.5 1 1.5 2 2.

−

−1.

−

−0.

0

1

2

The ellipses in the figure are the contours of a quadratic function that we want to optimize. Coordinate ascent was initialized at (2, −2), and also plotted in the figure is the path that it took on its way to the global maximum. Notice that on each step, coordinate ascent takes a step that’s parallel to one of the axes, since only one variable is being optimized at a time.

6.8.2 SMO

We close off the discussion of SVMs by sketching the derivation of the SMO algorithm. Here’s the (dual) optimization problem that we want to solve:

maxα W (α) =

∑^ n

i=

αi −

∑^ n

i,j=

y(i)y(j)αiαj 〈x(i), x(j)〉. (6.19)

s.t. 0 ≤ αi ≤ C, i = 1,... , n (6.20) ∑^ n

i=

αiy(i)^ = 0. (6.21)

Let’s say we have set of αi’s that satisfy the constraints (6.20-6.21). Now, suppose we want to hold α 2 ,... , αn fixed, and take a coordinate ascent step and reoptimize the objective with respect to α 1. Can we make any progress? The answer is no, because the constraint (6.21) ensures that

α 1 y(1)^ = −

∑^ n

i=

αiy(i).

Or, by multiplying both sides by y(1), we equivalently have

α 1 = −y(1)

∑^ n

i=

αiy(i).

(This step used the fact that y(1)^ ∈ {− 1 , 1 }, and hence (y(1))^2 = 1.) Hence, α 1 is exactly determined by the other αi’s, and if we were to hold α 2 ,... , αn fixed, then we can’t make any change to α 1 without violating the con- straint (6.21) in the optimization problem. Thus, if we want to update some subject of the αi’s, we must update at least two of them simultaneously in order to keep satisfying the constraints. This motivates the SMO algorithm, which simply does the following:

Repeat till convergence {

From the constraints (6.20), we know that α 1 and α 2 must lie within the box [0, C] × [0, C] shown. Also plotted is the line α 1 y(1)^ + α 2 y(2)^ = ζ, on which we know α 1 and α 2 must lie. Note also that, from these constraints, we know L ≤ α 2 ≤ H; otherwise, (α 1 , α 2 ) can’t simultaneously satisfy both the box and the straight line constraint. In this example, L = 0. But depending on what the line α 1 y(1)^ + α 2 y(2)^ = ζ looks like, this won’t always necessarily be the case; but more generally, there will be some lower-bound L and some upper-bound H on the permissible values for α 2 that will ensure that α 1 , α 2 lie within the box [0, C] × [0, C]. Using Equation (6.22), we can also write α 1 as a function of α 2 :

α 1 = (ζ − α 2 y(2))y(1).

(Check this derivation yourself; we again used the fact that y(1)^ ∈ {− 1 , 1 } so that (y(1))^2 = 1.) Hence, the objective W (α) can be written

W (α 1 , α 2 ,... , αn) = W ((ζ − α 2 y(2))y(1), α 2 ,... , αn).

Treating α 3 ,... , αn as constants, you should be able to verify that this is just some quadratic function in α 2. I.e., this can also be expressed in the form aα^22 + bα 2 + c for some appropriate a, b, and c. If we ignore the “box” constraints (6.20) (or, equivalently, that L ≤ α 2 ≤ H), then we can easily maximize this quadratic function by setting its derivative to zero and solving. We’ll let αn 2 ew,unclippeddenote the resulting value of α 2. You should also be able to convince yourself that if we had instead wanted to maximize W with respect to α 2 but subject to the box constraint, then we can find the resulting value optimal simply by taking αn 2 ew,unclippedand “clipping” it to lie in the [L, H] interval, to get

αn 2 ew =

H if α 2 new,unclipped > H αn 2 ew,unclipped if L ≤ αn 2 ew,unclipped ≤ H L if α 2 new,unclipped < L

Finally, having found the αn 2 ew, we can use Equation (6.22) to go back and find the optimal value of αn 1 ew. There’re a couple more details that are quite easy but that we’ll leave you to read about yourself in Platt’s paper: One is the choice of the heuristics used to select the next αi, αj to update; the other is how to update b as the SMO algorithm is run.