Maximum margin and support vectors

Image analysis

About this lecture

Last unit: many lines separate the training points. This unit picks one: the line with the widest empty street around it (Boser, Guyon & Vapnik, 1992; Cortes & Vapnik, 1995).

  • The maximum-margin line and its support vectors.
  • Fixing the scale: the street is -1 \le f \le 1, its width is 2/\|\mathbf w\|.
  • A worked example by hand.
  • Overlapping classes: the soft margin and the penalty C.
  • The hinge loss, compared with the 0–1, logistic and squared losses.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

The widest street

Of all lines with zero training errors, take the one with the widest empty street around it: the line in the middle, the two edges through the closest points of each class.

  • The street’s half-width is the margin: the distance from the line to the closest training point.
  • Here: blue at (0, 0), (0, 2); red at (2, 1), (3, 0). The widest street is 0 \le x_1 \le 2, the line is x_1 = 1, the width 2.

Support vectors

The points on the edge of the street are the support vectors: here (0, 0), (0, 2) and (2, 1).

  • Move any other point, without entering the street: nothing changes. The point (3, 0) could be anywhere to the right of x_1 = 2.
  • Move a support vector: the street moves with it.
  • The classifier depends on a few training points, the ones that are hard to classify. A forest or a nearest-mean rule uses all of them.

Two panels of the same eight points with the street between x1 = 0 and x1 = 2 and three ringed support vectors; in the right panel two unringed points have been moved (arrows) and the street is unchanged, width 2.00.

Ringed: support vectors. Moving two other points (arrows) leaves the street as it was.

Why a wide street?

  • Robustness: features are noisy (a pixel’s smoothed intensity, an object’s measured area). A point that moves a little should not cross the line. The wider the street, the larger the change it survives.
  • Generalisation (stated, not proven here): among separating lines, a wider margin goes with a smaller expected error on new points. The theory bounds the error by the margin relative to the spread of the data, not by the number of features.
  • A unique answer: the widest street is a single, well-defined line; “any line with zero errors” is not.

The four by-hand points with circles of radius 0.6 showing how far each may move. Left: a separating line with margin 0.27; the circle of the point (0, 2) crosses it. Right: the widest street's line x1 = 1, margin 1; no circle crosses.

Circles: each point may move by 0.6. Only the wide margin survives that.

Fixing the scale

Unit 1: (\mathbf w, b) and (c\mathbf w, cb) give the same line. Choose the scale so that the closest points have f = \pm 1:

  • The street is -1 \le f(\mathbf x) \le 1; its edges are the lines f = -1 and f = +1.
  • Each edge lies at distance 1/\|\mathbf w\| from the line: the width is \mathbf{2/\|w\|}.
  • Every training point on its side, outside the street: y_i\,f(\mathbf x_i) \ge 1.

Widest street = smallest \|\mathbf w\| such that all y_i f(\mathbf x_i) \ge 1.

Example: f = x_1 - 1, so \mathbf w = (1, 0), b = -1, width 2/1 = 2. Check: y\,f = 1, 1, 1, 2 for (0,0), (0,2), (2,1), (3,0).

Two panels with the by-hand points and the band -1 ≤ f ≤ 1 shaded grey, each point labelled with y f. Left: w = (2, 0), band width 1, values 2, 2, 2, 4. Right: w = (1, 0), band width 2, values 1, 1, 1, 2.

Shrinking \|\mathbf w\| widens the band -1 \le f \le 1 until it touches the closest points.

The math: the hard-margin problem

The edges f = \pm 1 are at distance |{\pm 1}|/\|\mathbf w\| from f = 0 (unit 1’s distance formula), so the width is 2/\|\mathbf w\|. Maximising it is minimising \|\mathbf w\|, or more conveniently

\min_{\mathbf w, b}\ \tfrac12\|\mathbf w\|^2 \quad\text{subject to}\quad y_i(\mathbf w\cdot\mathbf x_i + b) \ge 1, \quad i = 1, \dots, n.

  • A convex quadratic objective with linear constraints: one optimum, no local minima. (A neural network’s loss has many; the neural networks day.)
  • Feasible only if the data are separable. The constraints that hold with equality belong to the support vectors.
  • Solved in the next unit through its dual, in which only dot products \mathbf x_i\cdot\mathbf x_j appear.

Try it: the widest street

Real data overlap: the soft margin

Most data cannot be separated, or only by a very narrow street dictated by one odd point.

  • Allow points inside the street or even on the wrong side, at a price.
  • The slack \xi_i \ge 0 of a point is how far it falls short of y_i f = 1: \xi_i = \max(0, 1 - y_i f(\mathbf x_i)). Inside the street: 0 < \xi_i < 1. Wrong side: \xi_i > 1.
  • Minimise “narrowness + C × total slack”. C sets the price of a violation:
    • small C: violations are cheap → a wide street, many points inside it, a smooth, stable line;
    • large C: violations are expensive → a narrow street, few violations, sensitive to single outliers.
  • Support vectors are now all points on or inside the street (and on the wrong side).

The outlier example of the demo twice. Left, C = 1000: a narrow street (width 0.61) squeezed past one red outlier among the blue points, 0 errors. Right, C = 1: a wider street (width 1.94); the outlier is on the wrong side with its slack drawn as a yellow segment, 1 error.

The demo’s “one outlier”. Yellow: slack. Ringed: support vectors.

The math: the soft-margin problem

\min_{\mathbf w, b, \boldsymbol\xi}\ \tfrac12\|\mathbf w\|^2 + C\sum_i \xi_i \quad\text{subject to}\quad y_i f(\mathbf x_i) \ge 1 - \xi_i,\quad \xi_i \ge 0.

At the optimum each \xi_i is as small as allowed: \xi_i = \max(0, 1 - y_i f(\mathbf x_i)). Substituting gives an unconstrained problem:

\min_{\mathbf w, b}\ \tfrac12\|\mathbf w\|^2 + C\sum_i \max\big(0,\ 1 - y_i(\mathbf w\cdot\mathbf x_i + b)\big).

  • A regulariser plus a data term, as everywhere in the course: dividing by C gives \sum_i \text{loss}_i + \lambda\cdot\tfrac12\|\mathbf w\|^2 with \lambda = 1/C. Large C = weak regularisation.
  • Still convex. The hinge has a kink at y f = 1, but (sub)gradient descent works: step against \mathbf w - C\sum_{i:\,y_i f_i < 1} y_i\mathbf x_i (and -C\sum_{i:\,y_i f_i < 1} y_i for b). Only the violating points pull.

Try it: an outlier, and overlapping classes

Losses: what does the SVM minimise?

Write the margin value m = y\,f(\mathbf x): positive = correct side, \ge 1 = outside the street.

loss formula
0–1 1 if m \le 0, else 0 what we care about; flat with a jump
hinge \max(0,\ 1 - m) 0 outside the street, grows linearly
logistic \log_2(1 + e^{-m}) never exactly 0; every point has a say
squared (1 - m)^2 also punishes points that are “too correct”
  • The 0–1 loss cannot be minimised by walking downhill: its slope is 0 or undefined.
  • The hinge is the closest convex stand-in: zero for safe points, so only the support vectors matter.
  • The hinge is a ReLU of 1 - m: the same bend as the neural networks day’s units. The logistic loss is that day’s cross-entropy loss (in bits): an SVM and a logistic neuron differ only in the loss.

The losses by hand

Five points with y\,f = 2,\ 1,\ 0.5,\ 0,\ -1:

y f 2 1 0.5 0 −1 total
0–1 0 0 0 1 1 2
hinge 0 0 0.5 1 2 3.5
logistic 0.18 0.45 0.68 1 1.90 4.21
squared 1 0 0.25 1 4 6.25
  • The point at y f = 0.5 is correct but inside the street: the hinge charges it, the 0–1 loss does not.
  • The point at y f = 2 is safe: the hinge ignores it; the squared loss punishes it as much as the point on the line.

The four losses plotted against y f from -1.5 to 2.5: the 0-1 step, the hinge (zero from y f = 1 on), the logistic curve and the squared parabola with its minimum at 1; dots mark the hinge loss of the five points of the table.

Try it: four losses

Summary

  • The SVM picks the line with the widest street: -1 \le f \le 1 empty, width 2/\|\mathbf w\|.
  • Only the support vectors (on or inside the street) determine it; other points can move freely.
  • Soft margin: points may violate the street at a cost C per unit of slack. Small C = wide, smooth, tolerant; large C = narrow, sensitive to outliers.
  • The SVM minimises \tfrac12\|\mathbf w\|^2 + C\sum_i hinge(y_i f_i): zero for safe points, linear for violations. Convex: one optimum.