Blame
|
1 | # 7. Support Vector Machines |
||||||
|
2 | |||||||
| 3 | Support vector machines are large-margin linear classifiers. They pick the boundary that |
|||||||
| 4 | maximizes the distance to the nearest points, control overfitting with the hinge loss and a |
|||||||
| 5 | penalty $C$, and use kernels to fit nonlinear boundaries without ever forming the feature map. |
|||||||
| 6 | Throughout, labels are $y \in \{-1,+1\}$ and the decision uses a raw score $z = w^T x - b$. |
|||||||
| 7 | ||||||||
|
8 | ## 7.1 Optimal margin classifier |
||||||
|
9 | |||||||
| 10 | Labels are $y \in \{-1,+1\}$, with weight vector $w \in \mathbb{R}^{n}$ and bias $b$. |
|||||||
| 11 | ||||||||
|
12 | ### 7.1.1 Hypothesis and boundary |
||||||
|
13 | |||||||
| 14 | The hypothesis is defined as the sign of the raw score $z = w^T x - b$: |
|||||||
| 15 | ||||||||
| 16 | $$\boxed{ h(x) = \operatorname{sign}(w^T x - b) }$$ |
|||||||
| 17 | ||||||||
| 18 | The decision boundary is the set of points with zero score: |
|||||||
| 19 | ||||||||
| 20 | $$\boxed{ w^T x - b = 0 }$$ |
|||||||
| 21 | ||||||||
| 22 | *Remark:* $w$ is orthogonal to the boundary, so it sets the orientation, and $b$ sets the offset. |
|||||||
| 23 | ||||||||
|
24 | ### 7.1.2 Geometric margin |
||||||
|
25 | |||||||
| 26 | The geometric margin of example $i$ is defined as its signed distance to the boundary, made |
|||||||
| 27 | positive by the label: |
|||||||
| 28 | ||||||||
| 29 | $$\boxed{ \gamma^{(i)} = y^{(i)} \, \frac{w^T x^{(i)} - b}{\lVert w \rVert} }$$ |
|||||||
| 30 | ||||||||
| 31 | A correctly classified point has $\gamma^{(i)} > 0$. The margin of the dataset is the smallest |
|||||||
| 32 | $\gamma^{(i)}$ over all examples. |
|||||||
| 33 | ||||||||
| 34 | *Remark:* dividing by $\lVert w \rVert$ makes the margin invariant to rescaling $(w,b)$, unlike |
|||||||
| 35 | the raw score $z$. |
|||||||
| 36 | ||||||||
|
37 | ### 7.1.3 Hard-margin primal |
||||||
|
38 | |||||||
| 39 | Fixing the scale so the closest points satisfy $y^{(i)}(w^T x^{(i)} - b) = 1$, maximizing the |
|||||||
| 40 | margin is equivalent to minimizing $\lVert w \rVert^2$ subject to a unit functional margin: |
|||||||
| 41 | ||||||||
| 42 | $$\boxed{ \min_{w,b} \tfrac{1}{2}\lVert w \rVert^2 \quad \text{s.t.} \quad y^{(i)}(w^T x^{(i)} - b) \ge 1 \ \ \forall i }$$ |
|||||||
| 43 | ||||||||
| 44 | This is a convex quadratic program with linear constraints, so it has a unique optimum. |
|||||||
| 45 | ||||||||
| 46 | *Remark:* it requires the data to be linearly separable. The next lesson relaxes that with slack |
|||||||
| 47 | variables. |
|||||||
| 48 | ||||||||
|
49 |  |
||||||
|
50 | |||||||
| 51 | *The optimal hyperplane (solid) maximizes the margin (dashed). Circled points are the support vectors.* |
|||||||
| 52 | ||||||||
|
53 | ## 7.2 Hinge loss |
||||||
|
54 | |||||||
| 55 | The raw score is $z = w^T x - b$ and labels are $y \in \{-1,+1\}$. |
|||||||
| 56 | ||||||||
|
57 | ### 7.2.1 Hinge loss |
||||||
|
58 | |||||||
| 59 | The hinge loss is defined as the amount by which the margin $yz$ falls short of $1$, clipped at zero: |
|||||||
| 60 | ||||||||
| 61 | $$\boxed{ L(z,y) = \max(0,\, 1 - yz), \quad z = w^T x - b }$$ |
|||||||
| 62 | ||||||||
| 63 | It is zero once $yz \ge 1$ (the point is correct and beyond the margin) and grows linearly inside |
|||||||
| 64 | or past the margin. |
|||||||
| 65 | ||||||||
| 66 | *Remark:* the hinge loss is convex but not differentiable at $yz = 1$, so it is optimized with |
|||||||
| 67 | subgradients. |
|||||||
| 68 | ||||||||
|
69 | ### 7.2.2 Soft-margin primal |
||||||
|
70 | |||||||
| 71 | Introduce a slack $\xi_i \ge 0$ per example to allow margin violations, penalized by $C > 0$: |
|||||||
| 72 | ||||||||
| 73 | $$\boxed{ \min_{w,b,\xi} \tfrac{1}{2}\lVert w \rVert^2 + C\sum_{i=1}^{m}\xi_i \quad \text{s.t.} \quad y^{(i)}(w^T x^{(i)} - b) \ge 1 - \xi_i, \ \ \xi_i \ge 0 }$$ |
|||||||
| 74 | ||||||||
| 75 | At the optimum $\xi_i = \max(0,\, 1 - y^{(i)}(w^T x^{(i)} - b))$, so eliminating the slacks gives |
|||||||
| 76 | the unconstrained regularized form: |
|||||||
| 77 | ||||||||
| 78 | $$\boxed{ \min_{w,b} \tfrac{1}{2}\lVert w \rVert^2 + C\sum_{i=1}^{m}\max\!\big(0,\, 1 - y^{(i)}(w^T x^{(i)} - b)\big) }$$ |
|||||||
| 79 | ||||||||
| 80 | This is regularization plus hinge loss: the $\tfrac{1}{2}\lVert w \rVert^2$ term widens the margin |
|||||||
| 81 | and the sum penalizes violations. |
|||||||
| 82 | ||||||||
|
83 | ### 7.2.3 Role of $C$ |
||||||
|
84 | |||||||
| 85 | | $C$ | Penalty on violations | Margin | Behaviour | |
|||||||
| 86 | | --- | --- | --- | --- | |
|||||||
| 87 | | small | weak | wide | more violations tolerated, lower variance | |
|||||||
| 88 | | large | strong | narrow | fewer violations, fits training data harder | |
|||||||
| 89 | ||||||||
| 90 | *Remark:* as $C \to \infty$ no violation is tolerated, which recovers the hard-margin classifier. |
|||||||
| 91 | ||||||||
|
92 | ## 7.3 Kernels |
||||||
|
93 | |||||||
|
94 | ### 7.3.1 Kernel definition |
||||||
|
95 | |||||||
| 96 | A kernel is defined as the inner product of a feature map $\phi$ applied to two inputs: |
|||||||
| 97 | ||||||||
| 98 | $$\boxed{ K(x,z) = \phi(x)^T \phi(z) }$$ |
|||||||
| 99 | ||||||||
| 100 | A valid kernel computes this inner product directly, so $\phi$ never has to be formed (it may even |
|||||||
| 101 | be infinite-dimensional). |
|||||||
| 102 | ||||||||
|
103 | ### 7.3.2 Kernel trick |
||||||
|
104 | |||||||
| 105 | The SVM dual depends on the data only through inner products $\langle x^{(i)}, x^{(j)} \rangle$. |
|||||||
| 106 | The kernel trick replaces each inner product with a kernel: |
|||||||
| 107 | ||||||||
| 108 | $$\boxed{ \langle x^{(i)}, x^{(j)} \rangle \ \longrightarrow \ K(x^{(i)}, x^{(j)}) }$$ |
|||||||
| 109 | ||||||||
| 110 | This fits a linear boundary in the feature space, which is nonlinear in the original space, at the |
|||||||
| 111 | cost of evaluating $K$ instead of $\phi$. |
|||||||
| 112 | ||||||||
| 113 | A widely used choice is the Gaussian (RBF) kernel: |
|||||||
| 114 | ||||||||
| 115 | $$\boxed{ K(x,z) = \exp\!\left( -\frac{\lVert x - z \rVert^2}{2\sigma^2} \right) }$$ |
|||||||
| 116 | ||||||||
|
117 | ### 7.3.3 Mercer condition |
||||||
|
118 | |||||||
| 119 | A function $K$ is a valid kernel if and only if, for every finite sample, its Gram matrix is |
|||||||
| 120 | symmetric positive semidefinite: |
|||||||
| 121 | ||||||||
| 122 | $$\boxed{ K = K^T, \qquad K \succeq 0 }$$ |
|||||||
| 123 | ||||||||
| 124 | *Remark:* this is the Mercer condition. It guarantees a feature map $\phi$ exists, so the dual stays |
|||||||
| 125 | convex. |
|||||||
| 126 | ||||||||
|
127 | ### 7.3.4 Common kernels |
||||||
|
128 | |||||||
| 129 | | Kernel | $K(x,z)$ | Note | |
|||||||
| 130 | | --- | --- | --- | |
|||||||
| 131 | | Linear | $x^T z$ | no mapping, recovers the linear SVM | |
|||||||
| 132 | | Polynomial | $(x^T z + c)^d$ | degree $d$, offset $c$ | |
|||||||
| 133 | | Gaussian (RBF) | $\exp\!\big(-\tfrac{\lVert x - z \rVert^2}{2\sigma^2}\big)$ | infinite-dimensional, local | |
|||||||
| 134 | ||||||||
| 135 | *Remark:* a small $\sigma$ makes the RBF kernel very local, which can overfit. It trades off against |
|||||||
| 136 | $C$. |
|||||||
| 137 | ||||||||
|
138 |  |
||||||
|
139 | |||||||
| 140 | *An RBF kernel separates classes that are not linearly separable, with a nonlinear boundary in the input space.* |
|||||||
| 141 | ||||||||
|
142 | ## 7.4 Lagrangian and duality |
||||||
|
143 | |||||||
|
144 | ### 7.4.1 Lagrangian |
||||||
|
145 | |||||||
| 146 | For a primal objective $f(w)$ with inequality constraints $g_i(w) \le 0$ and multipliers |
|||||||
| 147 | $\beta_i \ge 0$, the Lagrangian is defined as: |
|||||||
| 148 | ||||||||
| 149 | $$\boxed{ \mathcal{L}(w,\beta) = f(w) + \sum_{i=1}^{m} \beta_i \, g_i(w) }$$ |
|||||||
| 150 | ||||||||
| 151 | Applied to the SVM primal $\tfrac{1}{2}\lVert w \rVert^2$ with constraints |
|||||||
| 152 | $1 - y^{(i)}(w^T x^{(i)} - b) \le 0$, the stationarity conditions $\nabla_w \mathcal{L} = 0$ and |
|||||||
| 153 | $\partial_b \mathcal{L} = 0$ give: |
|||||||
| 154 | ||||||||
| 155 | $$\boxed{ w = \sum_{i=1}^{m} \beta_i\, y^{(i)} x^{(i)}, \qquad \sum_{i=1}^{m} \beta_i\, y^{(i)} = 0 }$$ |
|||||||
| 156 | ||||||||
| 157 | So the optimal $w$ is a linear combination of the training inputs weighted by $\beta_i y^{(i)}$. |
|||||||
| 158 | ||||||||
|
159 | ### 7.4.2 Dual problem |
||||||
|
160 | |||||||
| 161 | Substituting these back eliminates $w$ and $b$, leaving a problem in $\beta$ that depends on the |
|||||||
| 162 | data only through inner products: |
|||||||
| 163 | ||||||||
| 164 | $$\boxed{ \max_{\beta} \ \sum_{i=1}^{m}\beta_i - \tfrac{1}{2}\sum_{i,j}\beta_i \beta_j\, y^{(i)} y^{(j)} \langle x^{(i)}, x^{(j)} \rangle \quad \text{s.t.} \quad \beta_i \ge 0, \ \ \sum_{i}\beta_i y^{(i)} = 0 }$$ |
|||||||
| 165 | ||||||||
|
166 | The inner products are exactly where a kernel $K$ is substituted (see [Kernels](/en/Machine%20Learning/07%20Support%20Vector%20Machines#73-kernels)). |
||||||
|
167 | |||||||
|
168 | ### 7.4.3 KKT and support vectors |
||||||
|
169 | |||||||
| 170 | At the optimum, complementary slackness ties each multiplier to its constraint: |
|||||||
| 171 | ||||||||
| 172 | $$\boxed{ \beta_i \big[\, y^{(i)}(w^T x^{(i)} - b) - 1 \,\big] = 0 }$$ |
|||||||
| 173 | ||||||||
| 174 | Support vectors are defined as the examples with a nonzero multiplier: |
|||||||
| 175 | ||||||||
| 176 | $$\boxed{ \text{support vectors} = \{\, i : \beta_i > 0 \,\} }$$ |
|||||||
| 177 | ||||||||
| 178 | These are the points exactly on the margin. All others have $\beta_i = 0$ and do not affect $w$. |
|||||||
| 179 | ||||||||
|
180 | ### 7.4.4 Kernelized decision |
||||||
|
181 | |||||||
| 182 | Replacing the inner product by a kernel gives a decision rule expressed only through support vectors: |
|||||||
| 183 | ||||||||
| 184 | $$\boxed{ h(x) = \operatorname{sign}\!\left( \sum_{i=1}^{m} \beta_i\, y^{(i)}\, K(x^{(i)}, x) - b \right) }$$ |
|||||||
| 185 | ||||||||
| 186 | *Remark:* only support vectors ($\beta_i > 0$) contribute, so prediction cost scales with their |
|||||||
| 187 | count, not with $m$. |
|||||||
| 188 | ||||||||
|
189 | ### 7.4.5 From primal to decision |
||||||
|
190 | |||||||
| 191 | ```mermaid |
|||||||
| 192 | flowchart TD |
|||||||
| 193 | A["primal QP: minimize half norm squared"] |
|||||||
| 194 | B["Lagrangian with multipliers"] |
|||||||
| 195 | C["dual problem in beta"] |
|||||||
| 196 | D["KKT conditions"] |
|||||||
| 197 | E["support vectors: beta greater than zero"] |
|||||||
| 198 | F["kernelized decision rule"] |
|||||||
| 199 | A --> B |
|||||||
| 200 | B --> C |
|||||||
| 201 | C --> D |
|||||||
| 202 | D --> E |
|||||||
| 203 | E --> F |
|||||||
| 204 | ``` |
|||||||
| 205 | ||||||||
| 206 | *Support vector machines draw a single, possibly kernelized, boundary. The final part takes a different route: split the feature space with simple rules and combine many such models into an ensemble.* |
|||||||
| 207 | ||||||||
| 208 | --- |
|||||||
|
209 | Next: [Decision trees and ensemble methods](/en/Machine%20Learning/08%20Decision%20trees%20and%20ensemble%20methods) · [Course overview](/en/Machine%20Learning) |
||||||
