Docsity

Support Vector Machines - Lectures Handouts - Machine Learning - 5, Study notes of Machine Learning

Toggle sidebar
bg1
CS229 Lecture notes
Andrew Ng
Part V
Support Vector Machines
This set of notes presents the Support Vector Machine (SVM) learning al-
gorithm. SVMs are among the best (and many believe is indeed the best)
“off-the-shelf” supervised learning algorithm. 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.
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 3.
Consider logistic regression, where the probability p(y= 1|x;θ) is mod-
eled by hθ(x) = g(θTx). We would 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;w, b), and thus also the higher our degree of “confidence”
that the label is 1. Thus, informally we can think of our prediction as being
a very confident one that y= 1 if θTx0. Similarly, we think of logistic
regression as making a very confident prediction of 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
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Support Vector Machines - Lectures Handouts - Machine Learning - 5 and more Study notes Machine Learning in PDF only on Docsity!

CS229 Lecture notes

Andrew Ng

Part V

Support Vector Machines

This set of notes presents the Support Vector Machine (SVM) learning al- gorithm. SVMs are among the best (and many believe is indeed the best) “off-the-shelf” supervised learning algorithm. 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.

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 3. Consider logistic regression, where the probability p(y = 1|x; θ) is mod- eled by hθ(x) = g(θT^ x). We would 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; w, b), and thus also the higher our degree of “confidence” that the label is 1. Thus, informally we can think of our prediction as being a very confident one that y = 1 if θT^ x  0. Similarly, we think of logistic regression as making a very confident prediction of 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 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’d 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,... , m}, 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,...,m ˆγ(i).

Next, lets 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,... , m}, 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,...,m γ(i).

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:

minγ,w,b

||w||^2

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

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.

5 Lagrange duality

Lets 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) (1)

= ∞. (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,... , n (3)

∂ ∂βi

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

α∗ i gi(w∗) = 0 , i = 1,... , k (5) gi(w∗) ≤ 0 , i = 1,... , k (6) α∗^ ≥ 0 , i = 1,... , k (7)

Moreover, if some w∗, α∗, β∗^ satisfy the KKT conditions, then it is also a solution to the primal and dual problems. We draw attention to Equation (5), which is called the KKT dual com- plementarity condition. Specifically, it implies that if α∗ i > 0, then gi(w∗) =

  1. (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 comple- mentarity condition will also give us our convergence test when we talk about the SMO algorithm.

6 Optimal margin classifiers

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

minγ,w,b

||w||^2

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

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. Lets move on. Looking ahead, as we develop the dual form of the problem, 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 −

∑^ m

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. Lets find the dual form of the problem. To do so, we need to first minimize L(w, b, α) with respect to w and b (for fixed α), to get θD, which we’ll do by

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 (9) 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, lets also take a more careful look at Equation (9), 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 (9), this quantity can also be written:

wT^ x + b =

( (^) m ∑

i=

αiy(i)x(i)

)T

x + b (12)

∑^ m

i=

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

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 (13) 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.

7 Kernels

Back in our discussion of linear regression, we had a problem in which the input x was the living area of a house, and we considered performing regres-

sion using the features x, x^2 and x^3 (say) to obtain a cubic function. To distinguish between these two sets of variables, we’ll call the “original” input value the input attributes of a problem (in this case, x, the living area). When that is mapped to some new set of quantities that are then passed to the learning algorithm, we’ll call those new quantities the input features. (Unfortunately, different authors use different terms to describe these two things, but we’ll try to use this terminology consistently in these notes.) We will also let φ denote the feature mapping, which maps from the attributes to the features. For instance, in our example, we had

φ(x) =

x x^2 x^3

Rather than applying SVMs using the original input attributes x, we may instead want to learn using some features φ(x). To do so, we simply need to go over our previous algorithm, and replace x everywhere in it with φ(x). Since the algorithm can be written entirely in terms of the inner prod- ucts 〈x, z〉, this means that we would replace all those inner products with 〈φ(x), φ(z)〉. Specificically, given a feature mapping φ, we define the corre- sponding Kernel to be

K(x, z) = φ(x)T^ φ(z).

Then, everywhere we previously had 〈x, z〉 in our algorithm, we could simply replace it with K(x, z), and our algorithm would now be learning using the features φ. Now, given φ, we could easily compute K(x, z) by finding φ(x) and φ(z) and taking their inner product. But what’s more interesting is that often, K(x, z) may be very inexpensive to calculate, even though φ(x) itself may be very expensive to calculate (perhaps because it is an extremely high di- mensional vector). In such settings, by using in our algorithm an efficient way to calculate K(x, z), we can get SVMs to learn in the high dimensional feature space space given by φ, but without ever having to explicitly find or represent vectors φ(x). Lets see an example. Suppose x, z ∈ Rn, and consider

K(x, z) = (xT^ z)^2.

for n = 3)

φ(x) =

x 1 x 1 x 1 x 2 x 1 x 3 x 2 x 1 x 2 x 2 x 2 x 3 x 3 x 1 x 3 x 2 √^ x^3 x^3 √^2 cx^1 √^2 cx^2 2 cx 3 c

and the parameter c controls the relative weighting between the xi (first order) and the xixj (second order) terms. More broadly, the kernel K(x, z) = (xT^ z + c)d^ corresponds to a feature mapping to an

(n+d d

feature space, corresponding of all monomials of the form xi 1 xi 2... xik that are up to order d. However, despite working in this O(nd)-dimensional space, computing K(x, z) still takes only O(n) time, and hence we never need to explicitly represent feature vectors in this very high dimensional feature space. Now, lets talk about a slightly different view of kernels. Intuitively, (and there are things wrong with this intuition, but nevermind), if φ(x) and φ(z) are close together, then we might expect K(x, z) = φ(x)T^ φ(z) to be large. Conversely, if φ(x) and φ(z) are far apart—say nearly orthogonal to each other—then K(x, z) = φ(x)T^ φ(z) will be small. So, we can think of K(x, z) as some measurement of how similar are φ(x) and φ(z), or of how similar are x and z. Given this intuition, suppose that for some learning problem that you’re working on, you’ve come up with some function K(x, z) that you think might be a reasonable measure of how similar x and z are. For instance, perhaps you chose

K(x, z) = exp

||x − z||^2 2 σ^2

This is a resonable measure of x and z’s similarity, and is close to 1 when x and z are close, and near 0 when x and z are far apart. Can we use this definition of K as the kernel in an SVM? In this particular example, the answer is yes. (This kernel is called the Gaussian kernel, and corresponds

to an infinite dimensional feature mapping φ.) But more broadly, given some function K, how can we tell if it’s a valid kernel; i.e., can we tell if there is some feature mapping φ so that K(x, z) = φ(x)T^ φ(z) for all x, z? Suppose for now that K is indeed a valid kernel corresponding to some feature mapping φ. Now, consider some finite set of m points (not necessarily the training set) {x(1),... , x(m)}, and let a square, m-by-m matrix K be defined so that its (i, j)-entry is given by Kij = K(x(i), x(j)). This matrix is called the Kernel matrix. Note that we’ve overloaded the notation and used K to denote both the kernel function K(x, z) and the kernel matrix K, due to their obvious close relationship. Now, if K is a valid Kernel, then Kij = K(x(i), x(j)) = φ(x(i))T^ φ(x(j)) = φ(x(j))T^ φ(x(i)) = K(x(j), x(i)) = Kji, and hence K must be symmetric. More- over, letting φk(x) denote the k-th coordinate of the vector φ(x), we find that for any vector z, we have

zT^ Kz =

i

j

ziKij zj

i

j

ziφ(x(i))T^ φ(x(j))zj

i

j

zi

k

φk(x(i))φk(x(j))zj

k

i

j

ziφk(x(i))φk(x(j))zj

k

i

ziφk(x(i))

The second-to-last step above used the same trick as you saw in Problem set 1 Q1. Since z was arbitrary, this shows that K is positive semi-definite (K ≥ 0). Hence, we’ve shown that if K is a valid kernel (i.e., if it corresponds to some feature mapping φ), then the corresponding Kernel matrix K ∈ Rm×m is symmetric positive semidefinite. More generally, this turns out to be not only a necessary, but also a sufficient, condition for K to be a valid kernel (also called a Mercer kernel). The following result is due to Mercer.^5

(^5) Many texts present Mercer’s theorem in a slightly more complicated form involving

L^2 functions, but when the input attributes take values in Rn, the version given here is equivalent.

algorithms that we’ll see later in this class will also be amenable to this method, which has come to be known as the “kernel trick.”

8 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

∑^ m

i=

ξi

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

Thus, examples are now permitted to have (functional) margin less than 1, and if an example whose functional margin is 1 − ξi, 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 large (which we saw earlier makes the margin small) 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

∑^ m

i=

ξi −

∑^ m

i=

αi

[

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

]

∑^ m

i=

riξi.

Here, the αi’s and ri’s are our Lagrange multipliers (constrained to be ≥ 0). We won’t go through the derivation of the dual again in detail, but after setting the derivatives with respect to w and b to zero as before, substituting them back in, and simplifying, we obtain the following dual form of the problem:

maxα W (α) =

∑^ m

i=

αi −

∑^ m

i,j=

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

s.t. 0 ≤ αi ≤ C, i = 1,... , m ∑^ m

i=

αiy(i)^ = 0,

As before, we also have that w can be expressed in terms of the αi’s as given in Equation (9), so that after solving the dual problem, we can continue to use Equation (13) to make our predictions. Note that, somewhat surprisingly, in adding ` 1 regularization, the only change to the dual problem is that what was originally a constraint that 0 ≤ αi has now become 0 ≤ αi ≤ C. The calculation for b∗^ also has to be modified (Equation 11 is no longer valid); see the comments in the next section/Platt’s paper. Also, the KKT dual-complementarity conditions (which in the next sec- tion will be useful for testing for the convergence of the SMO algorithm) are:

αi = 0 ⇒ y(i)(wT^ x(i)^ + b) ≥ 1 (14) αi = C ⇒ y(i)(wT^ x(i)^ + b) ≤ 1 (15) 0 < αi < C ⇒ y(i)(wT^ x(i)^ + b) = 1. (16)

Now, all that remains is to give an algorithm for actually solving the dual problem, which we will do in the next section.

9 The SMO algorithm

The SMO (sequential minimal optimization) algorithm, due to John Platt, gives an efficient way of solving the dual problem arising from the derivation