Pattern Recognition 2018 Support Vector Machines Ad Feelders Universiteit Utrecht Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 1 / 48
Support Vector Machines Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 2 / 48
Overview 1 Separable Case 2 Kernel Functions 3 Allowing Errors (Soft Margin) 4 SVM s in R. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 3 / 48
Linear Classifier for two classes Linear model y(x) = w φ(x) + b (7.1) with t n { 1, +1}. Predict t 0 = +1 if y(x 0 ) 0 and t 0 = 1 otherwise. The decision boundary is given by y(x) = 0. This is a linear classifier in feature space φ(x). Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 4 / 48
Mapping φ y(x) = w φ(x) + b = 0 φ maps x into higher dimensional space where data is linearly separable. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 5 / 48
Data linearly separable Assume training data is linearly separable in feature space, so there is at least one choice of w, b such that: 1 y(x n ) > 0 for t n = +1; 2 y(x n ) < 0 for t n = 1; that is, all training points are classified correctly. Putting 1. and 2. together: t n y(x n ) > 0 for n = 1,..., N Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 6 / 48
Maximum Margin There may be many solutions that separate the classes exactly. Which one gives smallest prediction error? SVM chooses line with maximal margin, where the margin is the distance between the line and the closest data point. In this way, it avoids low confidence classifications. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 7 / 48
Two-class training data Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 8 / 48
Many Linear Separators Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 9 / 48
SVM Decision Boundary Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 10 / 48
Maximize Margin Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 11 / 48
Support Vectors Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 12 / 48
Weight vector is orthogonal to the decision boundary Consider two points x A and x B both of which lie on the decision surface. Because y(x A ) = y(x B ) = 0, we have (w x A + b) (w x B + b) = w (x A x B ) = 0 and so the vector w is orthogonal to the decision surface. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 13 / 48
Distance of a point to a line x 2 x y(x) = w x + b = 0 r w x x 1 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 14 / 48
Distance to decision surface (φ(x) = x) We have w x = x + r w. (4.6) where w w is the unit vector in the direction of w, x is the orthogonal projection of x onto the line y(x) = 0, and r is the (signed) distance of x to the line. Multiply (4.6) left and right by w and add b: w x + b }{{} y(x) = w x + b }{{} 0 +r w w w So we get r = y(x) w w 2 = y(x) w (4.7) Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 15 / 48
Distance of a point to a line The signed distance of x n to the decision boundary is r = y(x n) w For lines that separate the data perfectly, we have t n y(x n ) = y(x n ), so that the distance is given by t n y(x n ) w = t n(w φ(x n ) + b) w (7.2) Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 16 / 48
Maximum margin solution Now we are ready to define the optimization problem: { [ tn (w ]} φ(x n ) + b) arg max min. (7.3) w,b n w 1 Since w does not depend on n, it can be moved outside of the minimization: { } 1 arg max w,b w min[t n(w φ(x n ) + b)]. (7.3) n Direct solution of this problem would be rather complex. A more convenient representation is possible. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 17 / 48
Canonical Representation The hyperplane (decision boundary) is defined by Then also w φ(x) + b = 0 κ(w φ(x) + b) = κw φ(x) + κb = 0 so rescaling w κw and b κb gives just another representation of the same decision boundary. To resolve this ambiguity, we choose the scaling factor such that for the points x i closest to the decision boundary. t i (w φ(x i ) + b) = 1 (7.4) Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 18 / 48
Canonical Representation (square=1,circle= 1) y(x) = 1 y(x) = 0 y(x) = 1 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 19 / 48
Canonical Representation In this case we have Quadratic program subject to the constraints (7.5). t n (w φ(x n ) + b) 1 n = 1,..., N (7.5) arg min w,b 1 2 w 2 (7.6) This optimization problem has a unique global minimum. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 20 / 48
Lagrangian Function Introduce Lagrange multipliers a n 0 to get Lagrangian function L(w, b, a) = 1 N 2 w 2 a n {t n (w φ(x n ) + b) 1} (7.7) n=1 with L(w, b, a) w N = w a n t n φ(x n ) n=1 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 21 / 48
Lagrangian Function and for b: L(w, b, a) b = N a n t n n=1 Equating the derivatives to zero yields the conditions: w = N a n t n φ(x n ) (7.8) n=1 and N a n t n = 0 (7.9) n=1 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 22 / 48
Dual Representation Eliminating w and b from L(w, b, a) gives the dual representation. L(w, b, a) = 1 N 2 w 2 a n {t n (w φ(x n ) + b) 1} n=1 = 1 N N N 2 w 2 a n t n w φ(x n ) b a n t n + = 1 2 n=1 n=1 n=1 N N a n a m t n t m φ(x n ) φ(x m ) n=1 m=1 N N N a n t n a m t m φ(x n ) φ(x m ) + = n=1 m=1 N a n 1 2 n=1 n=1 a n N N a n t n a m t m φ(x n ) φ(x m ) n=1 m=1 a n Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 23 / 48
Dual Representation Maximize L(a) = N a n 1 N a n t n a m t m φ(x n ) φ(x m ) (7.10) 2 n=1 n,m=1 with respect to a and subject to the constraints a n 0, n = 1,..., N (7.11) N a n t n = 0. (7.12) n=1 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 24 / 48
Kernel Function We map x to a high-dimensional space φ(x) in which data is linearly separable. Performing computations in this high-dimensional space may be very expensive. Use a kernel function k that computes a dot product in this space without making the actual mapping ( kernel trick ): k(x, x ) = φ(x) φ(x ) Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 25 / 48
Example: polynomial kernel Suppose x IR 3 and φ(x) IR 10 with φ(x) = (1, 2x 1, 2x 2, 2x 3, x1 2, x2 2, x3 2, 2x 1 x 2, 2x 1 x 3, 2x 2 x 3 ) Then φ(x) φ(z) = 1 + 2x 1 z 1 + 2x 2 z 2 + 2x 3 z 3 + x1 2 z1 2 + x2 2 z2 2 + x3 2 z3 2 + 2x 1 x 2 z 1 z 2 + 2x 1 x 3 z 1 z 3 + 2x 2 x 3 z 2 z 3 But this can be written as (1 + x z) 2 = (1 + x 1 z 1 + x 2 z 2 + x 3 z 3 ) 2 which costs much less operations to compute. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 26 / 48
Polynomial kernel: numeric example Suppose x = (3, 2, 6) and z = (4, 1, 5). Then φ(x) = (1, 3 2, 2 2, 6 2, 9, 4, 36, 6 2, 18 2, 12 2) φ(z) = (1, 4 2, 1 2, 5 2, 16, 1, 25, 4 2, 20 2, 5 2) Then φ(x) φ(z) = 1 + 24 + 4 + 60 + 144 + 4 + 900 + 48 + 720 + 120 = 2025. But (1 + x z) 2 = (1 + (3)(4) + (2)(1) + (6)(5)) 2 = 45 2 = 2025 is a more efficient way to compute this dot product. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 27 / 48
Kernels Linear kernel k(x, x ) = x x Two popular non-linear kernels are the polynomial kernel (of degree M): k(x, x ) = (x x + c) M and Gaussian (or radial) kernel: k(x, x ) = exp( x x 2 /2σ 2 ), (6.23) or where γ = 1 2σ 2. k(x, x ) = exp( γ x x 2 ), Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 28 / 48
Dual Representation with kernels Using k(x, x ) = φ(x) φ(x ) we get dual representation: Maximize L(a) = N a n 1 N a n t n a m t m k(x n, x m ) (7.10) 2 n=1 n,m=1 with respect to a and subject to the constraints a n 0, n = 1,..., N (7.11) N a n t n = 0. (7.12) n=1 Is this dual easier than the original problem? Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 29 / 48
Prediction Recall that Substituting into (7.1), we get y(x) = w φ(x) + b (7.1) N w = a n t n φ(x n ) (7.8) n=1 N y(x) = b + a n t n k(x, x n ) (7.13) n=1 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 30 / 48
Constrained Optimization Minimize subject to f (x) g i (x) 0 Lagrangian function: L(x, λ) = f (x) i λ i g i (x) KKT conditions for solution: 1 g i (x) 0 2 λ i 0 3 λ i g i (x) = 0 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 31 / 48
Prediction: support vectors KKT conditions: a n 0 (7.14) t n y(x n ) 1 0 (7.15) a n {t n y(x n ) 1} = 0 (7.16) From (7.16) it follows that for every data point, either 1 a n = 0, or 2 t n y(x n ) = 1. The former play no role in making predictions (see 7.13), and the latter are the support vectors that lie on the maximum margin hyper planes. Only the support vectors play a role in predicting the class of new attribute vectors! Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 32 / 48
Only the support vectors are important for prediction Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 33 / 48
Prediction: computing b Since for any support vector x n we have t n y(x n ) = 1, we can use (7.13) to get ( t n b + ) a m t m k(x n, x m ) = 1, (7.17) m S where S denotes the set of support vectors. Hence we have t n b + t n a m t m k(x n, x m ) = 1 m S t n b = 1 t n a m t m k(x n, x m ) m S and since t n { 1, +1} and so 1/t n = t n : b = t n m S a m t m k(x n, x m ) (7.17a) Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 34 / 48
Prediction: Example We receive the following output from the optimization software for fitting a support vector machine with linear kernel and perfect separation of the training data: n x n,1 x n,2 t n a n 1 2 2 1 0 1 2 1 3 1 8 3 3 1 1 1 8 4 3 6 +1 0 5 4 4 +1 1 4 6 6 5 +1 0 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 35 / 48
Prediction: Example The figure below is a plot of the same data set, where the dots represent points with class 1, and the crosses points with class +1. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 36 /48
Prediction: Example (a) Compute the value of the SVM bias term b. Data points with a > 0 are support vectors. Let s take the point x 1 = 4, x 2 = 4 with class label +1: b = t m N [ a n t n x mx n = 1 + 1 1 8 [4 4] 3 n=1 ] [ ] [ ] + 1 3 8 [4 4] 1 4 1 4 [4 4] = 3 4 (b) Which class does the SVM predict for the data point x 1 = 5, x 2 = 2? N [ y(x) = b + a n t n x x n = 3 1 1 8 [5 2] 3 n=1 ] [ ] [ ] 1 3 8 [5 2] + 1 4 1 4 [5 2] = 1 4 2 Since the sign is positive, we predict class +1. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 37 / 48
Prediction: Example Decision boundary and support vectors. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 38 /48
Allowing Errors So far we assumed that the training data points are linearly separable in feature space φ(x). Resulting SVM gives exact separation of training data in original input space x, with non-linear decision boundary. Class distributions typically overlap, in which case exact separation of the training data leads to poor generalization (overfitting). Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 39 / 48
Allowing Errors Data points are allowed to be on the wrong side of the margin boundary, but with a penalty that increases with the distance from that boundary. For convenience we make this penalty a linear function of the distance to the margin boundary. Introduce slack variables ξ n 0 with one slack variable for each training data point. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 40 / 48
Definition of Slack Variables We define ξ n = 0 for data points that are on the inside of the correct margin boundary and ξ n = t n y(x n ) for all other data points. ξ = 0 ξ = 0 ξ < 1 ξ > 1 y(x) = 1 y(x) = 0 y(x) = 1 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 41 / 48
New objective function Our goal is to maximize the margin while softly penalizing points that lie on the wrong side of the margin boundary. We therefore minimize N C ξ n + 1 2 w 2 (7.21) n=1 where the parameter C > 0 controls the trade-off between the slack variable penalty and the margin. Alternative view (divide by C and put λ = 1 2C ): N n=1 ξ n + λ w 2 i First term represents lack-of-fit (hinge loss) and second term takes care of regularization. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 42 / 48
Dual Maximize L(a) = N a n 1 N a n t n a m t m k(x n, x m ) (7.32) 2 n=1 n,m=1 with respect to a and subject to the constraints 0 a n C, n = 1,..., N (7.33) N a n t n = 0. (7.34) n=1 The same as the separable case, except for the constraints a n C. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 43 / 48
Model Selection As usual we are confronted with the problem of selecting the appropriate model complexity. The relevant parameters are C and any parameters of the chosen kernel function. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 44 / 48
SVM in R LIBSVM is available in package e1071 in R. It can also perform regression and non-binary classification. Non-binary classification is performed as follows: Train K(K 1)/2 binary SVM s on all possible pairs of classes. To classify a new point, let it be classified by every binary SVM, and pick the class with the highest number of votes. This is done automatically by function svm in e1071. Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 45 / 48
How to in R: analysis of optdigits data # SVM with radial kernel and gamma=1/62 and cost=1 (default settings) > optdigits.svm <- svm(optdigits.train[, -c(1, 40,65)],optdigits.train[,65]) # make predictions on test set > svm.pred <- predict(optdigits.svm,optdigits.test[, -c(1, 40,65)]) > table(optdigits.test[,65],svm.pred) svm.pred 0 1 2 3 4 5 6 7 8 9 0 177 0 0 0 1 0 0 0 0 0 1 0 179 0 0 2 0 0 0 1 0 2 0 7 167 0 3 0 0 0 0 0 3 0 0 3 172 2 2 0 1 2 1 4 0 1 0 0 179 0 0 0 1 0 5 0 0 0 0 0 181 0 0 0 1 6 1 0 0 0 1 0 179 0 0 0 7 0 0 0 0 0 0 0 172 0 7 8 0 6 0 0 3 0 0 0 159 6 9 0 0 0 2 0 1 1 1 1 174 # accuracy on test sample is somewhat better than regularized multinomial logit # which had "only" 95% accuracy > sum(diag(table(optdigits.test[,65],svm.pred)))/nrow(optdigits.test) [1] 0.967724 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 46 / 48
How to in R: : analysis of optdigits data # tune cost parameter with cross-validation > optdigits.svm.tune <- tune.svm(optdigits.train[, -c(1, 40,65)], optdigits.train[,65],cost=1:10) Warning messages: 1: In svm.default(list(v2 = c(0l, 0L, 6L, 0L, 3L, 1L, 0L, 0L, 0L, 0L, : Variable(s) V57 constant. Cannot scale data. # V57 is almost always zero, let s remove it > optdigits.svm.tune <- tune.svm(optdigits.train[, -c(1, 40,57,65)], optdigits.train[,65],cost=1:10) # show performance for each value of the cost parameter > optdigits.svm.tune$performances cost error dispersion 1 1 0.01490643 0.005227509 2 2 0.01202958 0.005934869 3 3 0.01229136 0.005376797 4 4 0.01176917 0.005119188 5 5 0.01150739 0.004803942 6 6 0.01150739 0.004803942 7 7 0.01176849 0.005115891 8 8 0.01150739 0.004803942 9 9 0.01176849 0.005115891 10 10 0.01176849 0.005115891 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 47 / 48
How to in R: : analysis of optdigits data # cost=5 is the smallest among the parameter values that minimize # cross-validation error # We fit the SVM with this parameter value on the whole training set: > optdigits.svm.tuned <- svm(optdigits.train[, -c(1, 40,57,65)], optdigits.train[,65],cost=5) # Generate predictions on the test set > svm.tuned.pred <- predict(optdigits.svm.tuned, optdigits.test[, -c(1, 40,57,65)]) # Compute accuracy on test set > sum(diag(table(optdigits.test[,65],svm.tuned.pred)))/nrow(optdigits.test) [1] 0.9727323 Ad Feelders ( Universiteit Utrecht ) Pattern Recognition 48 / 48