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