---
title: Support vector machines
heading: The maximum-margin classifier and its canonical form, the margin as one over the weight norm, support vectors and the dual problem with kernels, soft margins with slack variables and the hinge loss, and how the SVM's loss compares with logistic regression's
eyebrow: S14 · Lesson 33 · Support vector machines
xp: 50
---

::: hook
When two classes can be separated by a line, infinitely many lines do it. The perceptron stops at whichever it reaches first; the support vector machine picks the one with the widest gap to the nearest points on each side. That choice has three payoffs: it generalizes well, the answer depends only on the few points at the edge of the gap (the support vectors), and the problem is convex with a dual form that uses the data only through inner products, so lesson 32's kernels slot straight in. Before deep learning, kernel SVMs were the strongest general-purpose classifiers available.
:::

::: play
- Four losses as functions of the margin $m = ty$, the target (±1) times the classifier output: the 0–1 loss counts errors, the **hinge loss** max(0, 1 − m) is the SVM's, the logistic loss (in bits) is logistic regression's, and the squared loss $(1 - m)^2$ is least squares'.
- The hinge loss is exactly zero once a point is beyond the margin, m ≥ 1, so well-classified points stop mattering: they aren't support vectors.
- The squared loss also punishes points that are "too correct", which is why least squares makes a poor classifier.
:::

```sim plot
view: graph
x: m = -2..3
y: E = 0..4
slider: z = -2..3 step 0.05 = 0.3
plot: step(-m) | samples: 600 | color: ch3 | label: 0–1 loss
plot: (1 - m + abs(1 - m))/2 | samples: 400 | label: hinge loss
plot: ln(1 + exp(-m))/ln(2) | samples: 400 | color: ch4 | label: logistic loss (bits)
plot: (1 - m)^2 | samples: 400 | dashed | color: ch5 | label: squared loss
marker: m = z | label: margin
readout: hinge = (1 - z + abs(1 - z))/2 | digits: 4
readout: logistic = ln(1 + exp(-z))/ln(2) | digits: 4
```

::: predict
## A linear SVM has $\mathbf{w}$ = (3, 4). How wide is the gap between its two margin lines, $\mathbf{w}^T\mathbf{x} + b = \pm 1$?

- [ ] 5
- [ ] 1
- [x] 0.4
- [ ] 0.2

::: reveal
Each margin line is $1/\|\mathbf{w}\|$ = 1/5 = 0.2 from the decision boundary (lesson 15's distance formula with an output of ±1), so the gap is $2/\|\mathbf{w}\|$ = 0.4. Maximizing the margin means minimizing $\|\mathbf{w}\|$.
:::
:::

::: nameit
## The hard margin

Scale **w** and b so the closest points satisfy $t_n(\mathbf{w}^T\boldsymbol{\phi}(\mathbf{x}_n) + b) = 1$ (the canonical form). The margin is then $1/\|\mathbf{w}\|$, and the SVM solves

$$\min_{\mathbf{w}, b}\ \tfrac{1}{2}\|\mathbf{w}\|^2 \quad\text{subject to}\quad t_n(\mathbf{w}^T\boldsymbol{\phi}(\mathbf{x}_n) + b) \ge 1$$

a convex quadratic program with a unique solution.

## The dual and support vectors

With a Lagrange multiplier $a_n \ge 0$ per constraint, $\mathbf{w} = \sum_na_nt_n\boldsymbol{\phi}(\mathbf{x}_n)$ and predictions are

$$y(\mathbf{x}) = \sum_na_nt_nk(\mathbf{x}, \mathbf{x}_n) + b$$

Only points on the margin get $a_n > 0$: these are the **support vectors**, and the rest of the data could be deleted without changing the classifier. The bias follows from any support vector: $b = t_s - \sum_na_nt_nk(\mathbf{x}_s, \mathbf{x}_n)$.

## Soft margins

For overlapping classes, slack variables $\xi_n \ge 0$ let points violate the margin at a price:

$$\min\ \tfrac{1}{2}\|\mathbf{w}\|^2 + C\sum_n\xi_n, \qquad t_ny_n \ge 1 - \xi_n$$

At the optimum $\xi_n = \max(0, 1 - t_ny_n)$, the **hinge loss**, and the multipliers are capped at $0 \le a_n \le C$. Large C fits the training data hard; small C widens the margin and tolerates errors, acting as regularization.
:::

::: example
## The training set is (1, 1) with t = +1 and (−1, −1) with t = −1. Find the SVM's weights, margin and multipliers.

::: solution
1. **Use the symmetry.** The boundary passes through the origin perpendicular to (1, 1), so b = 0 and $\mathbf{w} = c(1, 1)$.
2. **Scale w.** The canonical condition $\mathbf{w}^T(1, 1) = 1$ gives c = 0.5, so
   $$\mathbf w = (0.5, 0.5)$$
3. **Margin.**
   $$\|\mathbf{w}\| = 0.707$$
   and the margin is 1.414, the distance from the origin to each point.
4. **Multipliers.**
   $$\begin{aligned} \mathbf w &= a_1(1, 1) \\ &\quad + a_2(-1)(-1, -1) \\ &= (a_1 + a_2)(1, 1) \end{aligned}$$
   With $a_1 = a_2$, each multiplier is 0.25.

**Answer:** **w** = (0.5, 0.5) and b = 0, a margin of 1.414, and multipliers of 0.25 each.
:::
:::

::: example
## Find the hinge and logistic losses for margins 0.3, −0.5 and 2.

::: solution
1. **Hinge loss.**
   $$\max(0, 1 - m)$$
   gives 0.7, 1.5 and 0.
2. **Logistic loss.**
   $$\log_2(1 + e^{-m})$$
   gives 0.800, 1.405 and 0.183 bits.
3. **Compare.** Both grow linearly for badly misclassified points, but only the hinge loss reaches zero, which is what makes the SVM's solution sparse.

**Answer:** the hinge losses are 0.7, 1.5 and 0, and the logistic losses 0.800, 1.405 and 0.183 bits.
:::
:::

::: example
## An RBF-kernel SVM trained on 2000 examples keeps 150 support vectors, on 64-dimensional inputs. Bound its leave-one-out error, and count the work per prediction.

::: solution
1. **Which examples can be errors.** Leaving out an example that isn't a support vector leaves the solution unchanged, and that example was classified correctly, so only support vectors can become leave-one-out errors.
2. **Bound.**
   $$\begin{aligned} \text{error} &\le 150/2000 \\ &= 7.5\% \end{aligned}$$
3. **Work per prediction.** Each prediction evaluates the kernel against every support vector:
   $$150 \times 64 = 9600$$
   subtract-square-add steps, plus 150 exponentials.
4. **Fewer support vectors.** They mean both a tighter bound and faster predictions.

**Answer:** the leave-one-out error is at most 7.5%, and each prediction takes about 9600 subtract-square-add steps plus 150 exponentials.
:::
:::

::: check
given: a = 1 | 3 | 0.5; b = 2 | 4
answer: 1/sqrt(a^2 + b^2)
tolerance: 0.5%

## A canonical linear SVM has w = ({a}, {b}). What's its margin?

::: right
Right. $1/\|\mathbf{w}\|$ = {answer}.
:::

::: misses
- [2/sqrt(a^2 + b^2)] That's the full gap between the margin lines; the margin is half of it.
- [any] Not yet. Distance from the boundary to a point with output 1.
:::
:::

::: check
given: t = 1 | -1; y = -0.8 | 0.4 | 0.7
answer: (1 - t*y + abs(1 - t*y))/2
tolerance: 0.5%

## A point with target {t} gets output {y}. What's its hinge loss?

::: right
Right. max(0, 1 − ty) = {answer}. A point beyond the margin, ty ≥ 1, would cost nothing.
:::

::: misses
- [any] Not yet. max(0, 1 − ty).
:::
:::

::: check
given: p = 1 | 2; q = 1 | 3
answer: sqrt(p^2 + q^2)
tolerance: 0.5%

## An SVM is trained on two points: ({p}, {q}) labeled +1 and (−{p}, −{q}) labeled −1. What's its margin?

::: right
Right. The boundary bisects the two points, so the margin is $\|(p, q)\|$ = {answer}.
:::

::: misses
- [2*sqrt(p^2 + q^2)] That's the distance between the points; the boundary sits halfway.
- [any] Not yet. Distance from the bisecting boundary to either point.
:::
:::

::: check
given: a = 0.3 | 1.2; c = 0.5 | 0.8; d = 0.6 | 2; k = 0.2 | 0.9; b = -0.3 | 0.5
answer: a*c - d*k + b
tolerance: 0.5%

## A kernel SVM has two support vectors: one with t = +1, multiplier {a} and kernel value {c} with a new input, and one with t = −1, multiplier {d} and kernel value {k}. The bias is {b}. What's the output?

::: right
Right. $\sum a_nt_nk(\mathbf{x}, \mathbf{x}_n) + b$ = {answer}.
:::

::: misses
- [a*c + d*k + b] Include each support vector's label.
- [any] Not yet. Sum multiplier × label × kernel, then add b.
:::
:::

::: check
given: a = 1 | -2; b = 0.5 | 2.5; x = 1 | 2; y = -1 | 0.5; t = 1 | -1
answer: t - (a*x + b*y)
tolerance: 0.5%

## A linear SVM has w = ({a}, {b}), and ({x}, {y}) is a support vector with label {t}. What's the bias b?

::: right
Right. $b = t_s - \mathbf{w}^T\mathbf{x}_s$ = {answer}.
:::

::: misses
- [a*x + b*y - t] Solve $t_s(\mathbf{w}^T\mathbf{x}_s + b) = 1$ for b.
- [any] Not yet. Support vectors sit exactly on the margin.
:::
:::

::: check
given: w = 4 | 20 | 50; C = 0.1 | 1 | 10; s = 2 | 8
answer: w/2 + C*s
tolerance: 0.5%

## A soft-margin SVM has $\|\mathbf{w}\|^2$ = {w}, C = {C} and total slack {s}. What's its objective?

::: right
Right. $\frac{1}{2}\|\mathbf{w}\|^2 + C\sum\xi$ = {answer}.
:::

::: misses
- [w + C*s] The norm term has a factor of one half.
- [any] Not yet. Half the squared norm plus C times the slack.
:::
:::

::: check
kind: parts
given: w = 1 | 4; C = 0.1 | 1 | 10; a = 1.5 | 2.5; b = 0.8 | -0.2; c = 1.2 | 0.3

## A soft-margin SVM has $\|\mathbf w\|^2$ = {w} and C = {C}. Three training points have margins $t_ny_n$ = {a}, {b} and {c}.

::: part
answer: ((1 - a) + abs(1 - a))/2 + ((1 - b) + abs(1 - b))/2 + ((1 - c) + abs(1 - c))/2
tolerance: 0.5%

### What is their total slack?

::: misses
- [(1 - a) + (1 - b) + (1 - c)] Points beyond the margin have zero slack, not negative.
- [any] $\sum\max(0, 1 - t_ny_n)$.
:::
:::

::: part
answer: w/2 + C*(((1 - a) + abs(1 - a))/2 + ((1 - b) + abs(1 - b))/2 + ((1 - c) + abs(1 - c))/2)
tolerance: 0.5%

### What is the objective?

::: misses
- [any] $\frac12\|\mathbf w\|^2 + C\sum\xi_n$.
:::
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3, c = -3..3, x = -5..5, y = -5..5

## A canonical linear SVM has weights (a, b) and bias c; consider a point (x, y).

::: step
answer: a*x + b*y + c

### What is the SVM's output there?
:::

::: step
answer: (a*x + b*y + c)/sqrt(a^2 + b^2)

### What is the point's signed distance from the boundary?
:::

::: step
answer: 1/sqrt(a^2 + b^2)

### How wide is the margin?
:::

::: right
Right. On the margin the output is ±1, one margin's width from the boundary.
:::
:::

::: explain
## Why does an SVM's solution depend only on a few training points?

::: model
The SVM's constraints only bind for points that sit on or inside the margin. A point comfortably on the correct side has zero hinge loss, so moving it slightly, or deleting it, doesn't change the objective, and at the optimum its Lagrange multiplier is zero. The weights are a combination of the training points weighted by their multipliers, so only the points with nonzero multipliers, the support vectors at the edge of the gap or violating it, determine the boundary. That sparsity makes predictions cheaper, since the kernel sum runs over support vectors only, and it's a direct consequence of the hinge loss being exactly flat beyond the margin.
:::
:::

::: practice

::: check
given: a = 0.2 | 5; b = 0.2 | 5
answer: 1/sqrt(a^2 + b^2)
tolerance: 0.5%

## Margin for w = ({a}, {b})?

::: misses
- [any] $1/\|\mathbf{w}\|$.
:::
:::

::: check
given: t = 1 | -1; y = -0.6 | 0.3 | 0.95
answer: (1 - t*y + abs(1 - t*y))/2
tolerance: 0.5%

## Hinge loss: t = {t}, y = {y}?

::: misses
- [any] max(0, 1 − ty).
:::
:::

::: check
given: a = 0.1 | 2; c = 0.1 | 1; d = 0.1 | 2; k = 0.1 | 1; b = -1 | 1
answer: a*c - d*k + b
tolerance: 0.5%

## Kernel SVM output: a₁ = {a}, k₁ = {c}, a₂ = {d} (t = −1), k₂ = {k}, b = {b}?

::: misses
- [any] $\sum a_nt_nk_n + b$.
:::
:::
:::
