Support Vector Machines
A support vector machine (SVM) is a linear classifier. Among all hyperplanes that separate two classes, it picks the one farthest from the nearest training points. SVMs use labels . The classifier scores an input with the linear discriminant
and predicts . Here is the weight vector, normal to the boundary , and is the bias. The signed distance of point to the boundary is . It is positive when the point is classified correctly.Rescaling and together does not move the boundary. The SVM fixes the scale by requiring , with equality for the closest points. The planes then lie at distance on each side of the boundary. The strip between them is the margin, and its width is . Maximizing the width gives the hard-margin SVM, a quadratic program:
It has a solution only when the classes are linearly separable. The soft-margin SVM adds a slack for each point and a penalty :
The slack measures how far point falls short of the margin. is the price of one unit of slack. At the optimum , so the problem equals an unconstrained hinge-loss problem:
Here is the hinge loss at . Dividing by gives the average hinge loss plus the ridge penalty with .Each training point falls in one of four groups:
- Outside the margin: and . Moving the point a little does not change the solution.
- On the margin: and .
- Inside the margin: and . The point lies on the boundary or on the correct side, closer than the margin allows.
- Misclassified: and . Hence is an upper bound on the number of training errors.
The support vectors are the points on the margin, inside it, or misclassified. Removing any other point leaves and unchanged. Large makes slack expensive, and the solution approaches the hard margin. Small allows a wider margin with more violations. is chosen by cross-validation, after standardizing the features. The dual problem is in 4.4a. SVM Dual, and nonlinear boundaries are in 4.4b. Kernel SVM.
Intuition
When a line separates two classes, infinitely many lines do. The perceptron stops at the first one it finds, which may pass close to some training points. A new point near those training points can then fall on the wrong side. The SVM picks the line with the widest empty strip between the classes. The closest training points then sit as far from the boundary as possible. The training points on the edges of the strip are the support vectors, and their positions alone fix the line. “Machine” is an old term for a learning algorithm.
Real data overlap, and one mislabeled point can make the widest strip very narrow or impossible. The soft margin lets some points enter the strip or cross the boundary. Each unit of violation costs . The hinge-loss form gives a second reading of the same model. An SVM is a regularized linear model, like logistic regression, whose loss is zero for every point beyond the margin. Boser, Guyon and Vapnik (1992) introduced the maximum-margin classifier, and Cortes and Vapnik (1995) added the slack variables. ISLR ch. 9, ESL ch. 12 and Bishop ch. 7 give textbook treatments.
Properties
(Hinge Versus Logistic Loss) The hinge loss is exactly zero beyond the margin, which makes the solution sparse but gives no probabilities.
Write . With labels in , logistic regression minimizes , and the SVM minimizes . Both are convex upper bounds on the 0-1 loss , after dividing the logistic loss by . Both grow linearly for very negative , so both tolerate outliers better than squared loss. The logistic loss is positive for every , so every point contributes to the logistic gradient. The hinge loss is zero once , so well-classified points contribute nothing, and only the support vectors remain. The function that minimizes the expected hinge loss is (Lin 2002). This is the Bayes decision, yet itself does not estimate the probability. Platt scaling (Platt 1999) fits a sigmoid to held-out scores when probabilities are needed.
(Choosing C) On separable data a finite reproduces the hard margin, and one outlier can make the hard margin very narrow.
The soft-margin solution equals the hard-margin solution once is at least the largest hard-margin dual multiplier (see 4.4a. SVM Dual). Below that value the soft margin trades training errors for width. A mislabeled point near the other class limits the hard-margin strip to the gap next to that point. The soft margin pays slack for that point instead and keeps a wide strip, as the Hard example shows. multiplies a sum over points, so the same regularizes less on a larger dataset. The parameter does not depend on this way, and stochastic solvers use it. Practitioners search on a logarithmic grid, such as , with cross-validation (Hsu, Chang and Lin 2003).
(Units Matter) Shifting or rotating all inputs leaves the SVM unchanged, while rescaling one feature moves the boundary.
Shifting every input by only changes to , and carries no penalty. A rotation preserves all distances and . Multiplying one feature by stretches distances along that axis, so the widest strip changes. Take a positive point at and a negative point at . The SVM boundary is , and the test point is positive. Now multiply the second feature by , as when it is recorded in other units. In the original units the boundary becomes , and the same test point is negative. Standardizing each feature with the training mean and standard deviation removes this dependence on units.
(Subgradient Training) The hinge-loss form can be minimized by stochastic subgradient descent, which scales to very large .
The hinge loss has a kink at with no gradient. A subgradient there is any slope between the left and right derivatives. Pegasos (Shalev-Shwartz, Singer and Srebro 2007) runs stochastic subgradient descent on . Each step uses one random point . If , the update is . Otherwise it is . Each step costs , and the number of steps for a fixed accuracy does not grow with . The perceptron update is the same step with , , and the threshold replaced by . Pegasos therefore acts like a perceptron that also requires a margin and shrinks its weights. LIBLINEAR (Fan et al. 2008) instead runs coordinate descent on the dual.
(More Than Two Classes) Multi-class SVMs are usually built from binary SVMs, either one class against the rest or one pair at a time.
One-versus-rest trains SVMs, each separating one class from all others, and predicts the class with the largest score . Separately trained SVMs have scores on different scales, so this argmax can be unreliable. The same ambiguity arises with linear discriminants. One-versus-one trains SVMs, one per pair of classes, and predicts by majority vote. For classes that is SVMs, each trained on about a fifth of the data. LIBSVM uses one-versus-one. Crammer and Singer (2001) give a single joint objective with one weight vector per class.
Derivation
The distance from to the hyperplane is , as shown in 4.2. Linear Discriminants. For a correctly classified point this equals . For separable data the maximum-margin problem is therefore
Multiplying and by any leaves every ratio unchanged. We may choose the scale so that the closest point has . All points then satisfy , and the inner minimum equals . Maximizing is the same as minimizing . The square makes the objective differentiable, and the factor removes a from its gradient. The planes and are apart.
For the soft margin, fix and and minimize over the slacks. Each enters the objective with the positive weight , so it takes its smallest feasible value. The constraints and give . Substituting this back yields the hinge-loss form. Each hinge term is a maximum of affine functions of , so it is convex. The penalty is strictly convex in (2.2 Convex Functions). The optimal is therefore unique. The optimal is unique in most problems, with exceptions described in 4.4a. SVM Dual.
Examples
(Easy) Widest Strip Between Four Sensor Readings
A plant monitors machines with two standardized sensor readings. Two faulty machines () read and . Two healthy machines () read and .
Find the hard-margin SVM, its margin width and its support vectors. , and lie on the margin. Setting for these three gives , and . Hence and , so . The fourth point has , so this guess is feasible. To prove it optimal, take any feasible . Add half of the constraint for , half of the constraint for and all of the constraint for : The Cauchy-Schwarz inequality gives , so every feasible has . Our solution has , so it is optimal, with objective . The boundary is the line . The margin width is , the distance from to , the midpoint of the faulty pair. The support vectors are , and . The healthy machine could move anywhere in the half-plane without changing the solution. The weights , and used in the proof are the dual multipliers of 4.4a. SVM Dual.
Guess that
(Medium) Hinge Losses of a Spam Filter
A spam filter uses two scaled features and the classifier , with spam as . Five training emails are spam at , and , and legitimate at and .
Compute each slack, place each email relative to the margin, and evaluate the soft-margin objective with . The values of are , , , and , in the order listed. The slacks are , , , and . Emails 1 and 4 lie outside the margin, and email 2 lies on it. Email 5 is classified correctly but sits inside the margin. Email 3 is spam with a negative score, so it is misclassified. The slacks sum to , which bounds the single training error from above. The penalty is , so the objective is . The margin width is .
(Hard) One Mislabeled Machine
Healthy machines () have standardized vibration readings and . Faulty machines () read and . A fifth machine at is labeled faulty, possibly by mistake.
Find the hard-margin SVM, then the soft-margin SVM with , and compare them. The hard margin must separate the closest opposite pair, and . Requiring and gives and . The boundary is , the margin width is , and the objective is . For the soft margin, guess that and lie on the margin while and violate it. The equations and give and . The reading at has slack , and the reading at has slack . The objective is . To confirm the guess, bound the hinge losses in pairs. Take any and . The readings at and have total hinge loss at least , and at least . The readings at and have total hinge loss at least . The objective is therefore at least For the slope of is , and for it is . The minimum is , which our solution attains, so it is optimal. The soft margin moves the boundary from to and widens the margin from to . It misclassifies the reading at instead of letting it set the boundary. For the soft margin returns the hard-margin solution, because the largest hard-margin dual multiplier is .
See Also
- 4.4a. SVM Dual, for the dual problem, its multipliers and the KKT conditions.
- 4.4b. Kernel SVM, which replaces inner products by kernels to get nonlinear boundaries.
- 4.3. Logistic Regression, the probabilistic linear classifier with a smooth loss.
- 1. Perceptrons, the earlier mistake-driven linear classifier without a margin.
- 3.2.1. Ridge Regression, the same squared penalty paired with squared-error loss.
- 2.3.2. Cross-Validation, for choosing .