CS 460G Midterm Hub / Lectures / Week 5 Lecture 2
Week 5 · Lecture 2

Kernel Methods

The kernel trick, the three kernels, and what degree and gamma actually do.

short answermath

How to be non-linear without ever computing the non-linear map.

The problem: XOR

XOR is the canonical linearly non-separable problem: no single straight line separates $\{(0,0),(1,1)\}$ from $\{(0,1),(1,0)\}$.

XOR — not linearly separable
XOR — not linearly separable · slide 12
A linear separator is the wrong decision boundary
A linear separator is the wrong decision boundary · slide 13

The idea: map to a higher dimension

> Solution: there exists a mapping $\phi$ to a higher-dimensional space in which the data is linearly separable.

Suppose $x$ has 2 features. Map

$$\phi(x_1,x_2) = (x_1^2,\; x_2^2,\; \sqrt{2}x_1x_2)$$

and the classifier becomes

$$f(x) = w^T\phi(x) + b$$

The mapping and the classifier in the new space
The mapping and the classifier in the new space · slide 18

For XOR this works because the product term $x_1x_2$ is negative in one class and positive in the other — a single threshold on it separates the classes.

Explicitly computing $\phi$ is expensive. For a degree-$d$ polynomial on $d$ features the mapped dimension grows combinatorially. This is the motivation sentence that must appear in any kernel-trick answer.

The kernel trick

> The dot product between two higher-dimensional mappings can be computed by a kernel function, without ever forming $\phi(x)$:

$$k(x,z) = \phi(x)^T\phi(z)$$

Replace the dot product with a kernel
Replace the dot product with a kernel · slide 20

Because the SVM dual depends on the data only through inner products $\langle x_i, x_j\rangle$, you can replace every inner product with $k(x_i,x_j)$ and obtain a non-linear classifier at the cost of a linear one.

"What is the kernel trick? What is the purpose of a kernel function?" The SVM's dual formulation depends on training points only through inner products. The kernel trick replaces $\langle\phi(x),\phi(z)\rangle$ with a kernel $k(x,z)$ that computes that inner product implicitly, so a linear algorithm becomes non-linear without ever materialising the (possibly infinite-dimensional) feature map. The purpose is computational: the cost becomes one kernel evaluation per pair instead of an explicit high-dimensional expansion. The resulting decision function is $f(x)=\sum_i \alpha_i y_i k(x_i,x)+b$ — a weighted sum of similarities to the support vectors.

The three kernels — exact formulas

Linear kernel
Linear kernel · slide 21
Polynomial and RBF kernels
Polynomial and RBF kernels · slide 22
Kernel$k(x,z)$Hyperparameters and their role
Linear$x^Tz$none (plus $C$)
Polynomial, degree $d$$(x^Tz)^d$$d$ — order of interaction; $d=1$ is linear
Polynomial, degree up to $d$$(x^Tz+1)^d$$d$ — includes all lower-order terms
RBF / Gaussian$\exp\!\big(-\gamma\|x-z\|^2\big)$$\gamma$ — width of the influence of one point

What $\gamma$ does — the deck shows it directly at $\gamma=0.1$ and $\gamma=1$:

  • Small $\gamma$ — the kernel decays slowly, so every training point influences the prediction almost equally. The boundary is smooth, almost linear; risk of underfitting.
  • Large $\gamma$ — the kernel decays fast, so each point only influences its own immediate neighbourhood. The boundary becomes tight and wiggly around individual points; risk of overfitting.

$\gamma$ behaves like the inverse of a radius: it is $1/(2\sigma^2)$ for a Gaussian of standard deviation $\sigma$.

RBF with gamma = 0.1 — smooth boundary
RBF with gamma = 0.1 — smooth boundary · slide 27
RBF with gamma = 1 — tight boundary
RBF with gamma = 1 — tight boundary · slide 28

What degree does — degree 2, 5 and 10 in sequence. Low degree underfits complex boundaries; high degree produces very flexible, high-variance boundaries and becomes numerically unstable.

Polynomial degree 2
Polynomial degree 2 · slide 24
Polynomial degree 10
Polynomial degree 10 · slide 26

"Name commonly used SVM kernels and discuss their hyperparameters." Linear $k(x,z)=x^Tz$ — no kernel hyperparameter; best when $d \gg N$ or the data is already separable. Polynomial $(x^Tz+1)^d$ — $d$ controls the order of feature interactions; larger $d$ = more flexible, more unstable. RBF $\exp(-\gamma\|x-z\|^2)$ — $\gamma$ sets the radius of influence, small $\gamma$ = smooth/underfit, large $\gamma$ = spiky/overfit. All three are combined with the margin-violation penalty $C$. Practical note: always scale features first, because the RBF kernel is a function of Euclidean distance.