Skip to content

Kernel SVM: Inner Products Without Computing High-Dimensional Features | NTU Hsuan-Tien Lin Machine Learning Techniques Lecture 3

Sep 30, 20261 min
TL;DRThe dual SVM's variable count no longer depends on the feature dimension d̃, but every entry of the Q matrix is still one d̃-dimensional inner product. This lecture merges "transform + inner product" into a single kernel function K(x, x′); for the second-order transform the inner product takes only O(d). Once training, computing b, and prediction all use K, the SVM never touches the Z space. Pushed to the limit, the same move gives the Gaussian kernel, which corresponds to an infinite-dimensional transform, at the price that too large a γ still overfits.

Look up & save

Loading dictionary…

🌏 中文版

Video status: Videos included. This article is written from the official MOOC handout 203_handout.pdf and transcripts of the four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/22 to 22/22), and wherever the text says "in the lecture" it is paraphrasing what is said in the video.

This is Techniques Lecture 3 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. The Roadmap on page 1 of the handout divides Techniques into three ways of handling features, and this lecture is still inside the first, Embedding Numerous Features: Kernel Models. The same page sums up the previous lecture, Techniques Lecture 2: Dual SVM, this way: the dual SVM is another QP, with valuable geometric messages and "almost no dependence on d~\tilde{d}". The word almost is printed in gray on the slide, and what this lecture does is remove it.

The prerequisites are the dual problem from the previous lecture, the two formulas that recover w\mathbf{w} and bb from α\boldsymbol{\alpha}, the definition of support vectors, and the polynomial transform from Foundations Lecture 12: Nonlinear Transformation. The Gaussian kernel section uses the Taylor expansion of the exponential function. After reading, you should be able to derive the kernel of the second-order transform yourself, write down the four steps of the kernel hard-margin SVM and carry them out by hand on a small data set, explain why the Gaussian kernel is an inner product of infinite-dimensional vectors, and use a 2×2 matrix to show that a given function is not a valid kernel.

The path through the lecture:

  1. Find the bottleneck: the dual problem's QDQ_D still needs d~\tilde{d}-dimensional inner products.
  2. A shortcut for the second-order transform: computing the transform and the inner product together takes only O(d)O(d).
  3. The kernel trick: the three places in the dual SVM that use an inner product are all replaced by a kernel function.
  4. The polynomial kernel: adding scaling to the transform gives (ζ+γx⊤x′)Q(\zeta + \gamma\mathbf{x}^\top\mathbf{x}')^Q.
  5. The Gaussian kernel: the same move pushed to infinitely many dimensions.
  6. Comparison and limits: the trade-offs among three kernels, and which functions count as kernels.

Can the transform and the inner product be computed together: the kernel trick

YouTube:Kernel Support Vector Machine :: Kernel Trick @ Machine Learning Techniques (機器學習技法)

(Video 00:00–03:33 reviews the previous lecture and points out the bottleneck, 03:33–10:47 derives the fast inner product for the second-order transform, 10:47–17:41 defines the kernel function and plugs it into three places in the dual SVM, 17:41–19:55 gives the full algorithm and its cost, and Fun Time follows.)

The bottleneck left by the previous lecture

Page 2 of the handout copies out the previous lecture's dual problem again, with half-way done written next to the heading:

min⁡α12α⊤QDα−1⊤αsubject toy⊤α=0;αn≥0,n=1,2,…,N\begin{aligned} \min_{\boldsymbol{\alpha}}\quad & \frac{1}{2}\boldsymbol{\alpha}^\top Q_D\boldsymbol{\alpha} - \mathbf{1}^\top\boldsymbol{\alpha} \\ \text{subject to}\quad & \mathbf{y}^\top\boldsymbol{\alpha} = 0; \\ & \alpha_n \ge 0,\quad n = 1, 2, \dots, N \end{aligned}

There are NN variables and N+1N+1 constraints, and neither number depends on d~\tilde{d}. d~\tilde{d} is hiding in the entries of QDQ_D:

qn,m=ynym zn⊤zm,zn⊤zm=Φ(xn)⊤Φ(xm)q_{n,m} = y_n y_m\,\mathbf{z}_n^\top\mathbf{z}_m, \qquad \mathbf{z}_n^\top\mathbf{z}_m = \boldsymbol{\Phi}(\mathbf{x}_n)^\top\boldsymbol{\Phi}(\mathbf{x}_m)

This is an inner product in Rd~\mathbb{R}^{\tilde{d}}. The numbers Lin gives in the lecture: an inner product in a 10,000-dimensional space takes 10,000 steps if you compute it by definition, and in 100,000 dimensions it takes 100,000 steps. Without these coefficients the QP cannot even be started, so in the lecture Lin calls this step the bottleneck of the whole solution process. The handout states the requirement in one line: zn⊤zm\mathbf{z}_n^\top\mathbf{z}_m must be calculated faster than O(d~)O(\tilde{d}).

In the lecture, Lin then looks at this inner product as two actions. Seen from the X space, zn⊤zm\mathbf{z}_n^\top\mathbf{z}_m means transforming each of the two x\mathbf{x} first and then taking the inner product. Transforming into 10,000 dimensions costs 10,000 units of effort, and the inner product in 10,000 dimensions costs another 10,000; as long as the two steps are done separately, neither cost can be avoided. Here Lin uses a Taiwanese expression for a sneaky shortcut: can the two steps be merged and computed together, faster?

The inner product of the second-order transform takes O(d)

Page 3 of the handout uses the 2nd order polynomial transform as its example. This Φ2\boldsymbol{\Phi}_2 is written slightly differently from the one in Foundations Lecture 12:

Φ2(x)=(1, x1,x2,…,xd, x12,x1x2,…,x1xd, x2x1,x22,…,x2xd, …, xd2)\boldsymbol{\Phi}_2(\mathbf{x}) = (1,\ x_1, x_2, \dots, x_d,\ x_1^2, x_1x_2, \dots, x_1x_d,\ x_2x_1, x_2^2, \dots, x_2x_d,\ \dots,\ x_d^2)

x1x2x_1x_2 and x2x1x_2x_1 have the same value, yet both are included. The slide's note says this is for 'simplicity'; in the lecture Lin says outright that it is padding, done so that the formulas that follow tidy up more cleanly. Counted this way, Φ2(x)\boldsymbol{\Phi}_2(\mathbf{x}) has 1+d+d21 + d + d^2 components: one constant, dd first-order terms, and d2d^2 second-order terms (one xixjx_ix_j for every pair (i,j)(i, j)).

Now take two points x\mathbf{x} and x′\mathbf{x}' and, following the definition, multiply the transformed vectors component by component and add everything up:

Φ2(x)⊤Φ2(x′)=1⋅1⏟constant+∑i=1dxixi′⏟1st order+∑i=1d∑j=1dxixj xi′xj′⏟2nd order\boldsymbol{\Phi}_2(\mathbf{x})^\top\boldsymbol{\Phi}_2(\mathbf{x}') = \underbrace{1 \cdot 1}_{\text{constant}} + \underbrace{\sum_{i=1}^{d} x_i x_i'}_{\text{1st order}} + \underbrace{\sum_{i=1}^{d}\sum_{j=1}^{d} x_i x_j\, x_i' x_j'}_{\text{2nd order}}

The troublesome part is the double sum at the end, which has d2d^2 terms. What Lin does in the lecture is collect the factors that involve ii on one side and the factors that involve jj on the other. Each term is a product of four numbers, and xixi′x_i x_i' does not contain jj, so it can be pulled out of the inner sum:

∑i=1d∑j=1dxixj xi′xj′=∑i=1dxixi′(∑j=1dxjxj′)=(∑i=1dxixi′)(∑j=1dxjxj′)\sum_{i=1}^{d}\sum_{j=1}^{d} x_i x_j\, x_i' x_j' = \sum_{i=1}^{d} x_i x_i' \Big(\sum_{j=1}^{d} x_j x_j'\Big) = \Big(\sum_{i=1}^{d} x_i x_i'\Big)\Big(\sum_{j=1}^{d} x_j x_j'\Big)

The second equality holds because the sum in parentheses does not contain ii, so it is a constant as far as the outer sum is concerned. Both parentheses are the inner product in the original space, x⊤x′\mathbf{x}^\top\mathbf{x}', so

Φ2(x)⊤Φ2(x′)=1+x⊤x′+(x⊤x′)2\boldsymbol{\Phi}_2(\mathbf{x})^\top\boldsymbol{\Phi}_2(\mathbf{x}') = 1 + \mathbf{x}^\top\mathbf{x}' + (\mathbf{x}^\top\mathbf{x}')^2

To evaluate the right-hand side, spend O(d)O(d) computing x⊤x′\mathbf{x}^\top\mathbf{x}' once, then square and add. The handout's conclusion is that for Φ2\boldsymbol{\Phi}_2, transform + inner product can be carefully done in O(d)O(d) instead of O(d2)O(d^2). In the lecture, Lin adds a caveat: merging the two steps does not always make the computation faster; it just happens to work in this special case.

Here is a small check made up for this article (not from the slides). Take d=2d = 2, x=(1,2)\mathbf{x} = (1, 2), and x′=(3,1)\mathbf{x}' = (3, 1).

  • By definition: Φ2(x)=(1, 1,2, 1,2,2,4)\boldsymbol{\Phi}_2(\mathbf{x}) = (1,\ 1, 2,\ 1, 2, 2, 4) and Φ2(x′)=(1, 3,1, 9,3,3,1)\boldsymbol{\Phi}_2(\mathbf{x}') = (1,\ 3, 1,\ 9, 3, 3, 1), each with 1+2+4=71 + 2 + 4 = 7 components. The inner product is 1+3+2+9+6+6+4=311 + 3 + 2 + 9 + 6 + 6 + 4 = 31.
  • By the shortcut: x⊤x′=3+2=5\mathbf{x}^\top\mathbf{x}' = 3 + 2 = 5, and 1+5+52=311 + 5 + 5^2 = 31.

The two agree. With d=2d = 2 the difference is invisible, but it becomes obvious once dd is 100: the definition requires expanding each point into 1+100+1002=10,1011 + 100 + 100^2 = 10{,}101 dimensions before taking the inner product, while the shortcut needs one 100-dimensional inner product plus two operations.

The kernel function: wrapping the transform inside

Page 4 of the handout gives this shortcut a name. Every transform Φ\boldsymbol{\Phi} corresponds to a kernel function:

KΦ(x,x′)≡Φ(x)⊤Φ(x′)K_{\boldsymbol{\Phi}}(\mathbf{x}, \mathbf{x}') \equiv \boldsymbol{\Phi}(\mathbf{x})^\top\boldsymbol{\Phi}(\mathbf{x}')

The result above is written KΦ2(x,x′)=1+x⊤x′+(x⊤x′)2K_{\boldsymbol{\Phi}_2}(\mathbf{x}, \mathbf{x}') = 1 + \mathbf{x}^\top\mathbf{x}' + (\mathbf{x}^\top\mathbf{x}')^2, with the subscript saying which transform it corresponds to. In the lecture, Lin stresses that a kernel function takes the original x\mathbf{x} as input: it operates directly on things in the X space, and that stands for the two steps of transforming and then taking the inner product in the Z space, with the transform wrapped inside the function. The hope, of course, is that this function is fast to compute.

Next, the places in the dual SVM that use the Z space are found one by one and replaced. There are three.

First, the quadratic coefficients. This is the bottleneck itself, and the substitution is direct:

qn,m=ynym zn⊤zm=ynymK(xn,xm)q_{n,m} = y_n y_m\,\mathbf{z}_n^\top\mathbf{z}_m = y_n y_m K(\mathbf{x}_n, \mathbf{x}_m)

Second, computing bb. The previous lecture picks a support vector (xs,ys)(\mathbf{x}_s, y_s) and uses b=ys−w⊤zsb = y_s - \mathbf{w}^\top\mathbf{z}_s. What appears here is the inner product of w\mathbf{w} and zs\mathbf{z}_s, and w\mathbf{w} is a d~\tilde{d}-dimensional vector. In the lecture, Lin voices the student's doubt first: at first glance this is not an inner product of two z\mathbf{z} vectors, so a kernel cannot be used. The way out is another KKT condition from the previous lecture, w=∑nαnynzn\mathbf{w} = \sum_n \alpha_n y_n \mathbf{z}_n: w\mathbf{w} is itself a linear combination of the zn\mathbf{z}_n. Substitute it in, and the inner product distributes over the sum:

b=ys−w⊤zs=ys−(∑n=1Nαnynzn)⊤zs=ys−∑n=1Nαnyn zn⊤zs=ys−∑n=1NαnynK(xn,xs)b = y_s - \mathbf{w}^\top\mathbf{z}_s = y_s - \Big(\sum_{n=1}^{N}\alpha_n y_n \mathbf{z}_n\Big)^\top\mathbf{z}_s = y_s - \sum_{n=1}^{N}\alpha_n y_n\,\mathbf{z}_n^\top\mathbf{z}_s = y_s - \sum_{n=1}^{N}\alpha_n y_n K(\mathbf{x}_n, \mathbf{x}_s)

Third, prediction. For a new input x\mathbf{x}, the classifier is sign(w⊤Φ(x)+b)\text{sign}(\mathbf{w}^\top\boldsymbol{\Phi}(\mathbf{x}) + b). Φ(x)\boldsymbol{\Phi}(\mathbf{x}) is just another z\mathbf{z}, so the same move applies:

gSVM(x)=sign(w⊤Φ(x)+b)=sign(∑n=1NαnynK(xn,x)+b)g_{\text{SVM}}(\mathbf{x}) = \text{sign}\big(\mathbf{w}^\top\boldsymbol{\Phi}(\mathbf{x}) + b\big) = \text{sign}\Big(\sum_{n=1}^{N}\alpha_n y_n K(\mathbf{x}_n, \mathbf{x}) + b\Big)

With all three replaced, Lin goes back over the procedure in the lecture: is there a single moment where an inner product is really taken in the Z space? No. w\mathbf{w} is never computed at all. It only ever appears in the form of an inner product with some z\mathbf{z}, and every such appearance has been replaced by a kernel. The handout calls this the kernel trick: plug in an efficient kernel function to avoid dependence on d~\tilde{d}.

The kernel hard-margin SVM algorithm

Page 5 of the handout rewrites the previous lecture's dual SVM as the Kernel Hard-Margin SVM Algorithm. The steps are the same as before, except that every inner product in the Z space becomes KK:

  1. Compute qn,m=ynymK(xn,xm)q_{n,m} = y_n y_m K(\mathbf{x}_n, \mathbf{x}_m) and p=−1N\mathbf{p} = -\mathbf{1}_N, then prepare (A,c)(A, \mathbf{c}) from the equality constraint y⊤α=0\mathbf{y}^\top\boldsymbol{\alpha} = 0 and the bound constraints αn≥0\alpha_n \ge 0.
  2. α←QP(QD,p,A,c)\boldsymbol{\alpha} \leftarrow \text{QP}(Q_D, \mathbf{p}, A, \mathbf{c}).
  3. Pick a support vector (xs,ys)(\mathbf{x}_s, y_s) and set b←ys−∑SVαnynK(xn,xs)b \leftarrow y_s - \sum_{\text{SV}} \alpha_n y_n K(\mathbf{x}_n, \mathbf{x}_s).
  4. Return the support vectors, their αn\alpha_n, and bb. For a new x\mathbf{x}, gSVM(x)=sign(∑SVαnynK(xn,x)+b)g_{\text{SVM}}(\mathbf{x}) = \text{sign}\big(\sum_{\text{SV}} \alpha_n y_n K(\mathbf{x}_n, \mathbf{x}) + b\big).

The sums in steps 3 and 4 run over the support vectors only, because terms with αn=0\alpha_n = 0 contribute nothing. For step 3, Lin adds a practical note in the lecture: mathematically one support vector is enough, but you can also pick several, compute bb from each, and average, which is numerically more stable.

The handout lists the cost of each step:

StepCostRelation to d~\tilde{d}
1O(N2)O(N^2) kernel evaluationsnone, if the kernel is fast to compute
2a QP with NN variables and N+1N+1 constraintsnone
3, 4O(#SV)O(\#\text{SV}) kernel evaluationsnone, if the kernel is fast to compute

The handout's summary: kernel SVM uses a computational shortcut to avoid d~\tilde{d} and predicts with SVs only. An addition in this article, on a consequence that is easy to overlook: what you have to store after training changes. The primal SVM stores (b,w)(b, \mathbf{w}). The kernel SVM has no w\mathbf{w} to store; it stores the support vectors themselves, their αn\alpha_n (and yny_n), and bb. Every prediction on a new example costs one kernel evaluation against each support vector.

A numerical example: kernel SVM on three points

The following is an example made up for this article, not from the slides. Its purpose is to walk through all four steps above and to see that the kernel route and really transforming into the Z space give the same answer.

The data are three points in one dimension:

nnxnx_nyny_n
1−1-1+1+1
200−1-1
311+1+1

The negative example is sandwiched between the two positive ones, so no single cut on the number line separates them, and the linear hard-margin SVM has no solution. The kernel is K2(x,x′)=(1+xx′)2K_2(x, x') = (1 + xx')^2, which is only introduced formally in the next section (treat it as a given function for now; its transform is Φ(x)=(1,2x,x2)\boldsymbol{\Phi}(x) = (1, \sqrt{2}x, x^2), because 1+2xx′+x2x′2=(1+xx′)21 + 2xx' + x^2x'^2 = (1 + xx')^2).

Step 1: compute the kernel matrix and QDQ_D. For example, K(x1,x3)=(1+(−1)(1))2=0K(x_1, x_3) = (1 + (-1)(1))^2 = 0, K(x1,x2)=(1+0)2=1K(x_1, x_2) = (1 + 0)^2 = 1, and K(x1,x1)=(1+1)2=4K(x_1, x_1) = (1 + 1)^2 = 4. Compute them all, then multiply by ynymy_n y_m:

K=[410111014],QD=[4−10−11−10−14]K = \begin{bmatrix} 4 & 1 & 0 \\ 1 & 1 & 1 \\ 0 & 1 & 4 \end{bmatrix}, \qquad Q_D = \begin{bmatrix} 4 & -1 & 0 \\ -1 & 1 & -1 \\ 0 & -1 & 4 \end{bmatrix}

The entries of QDQ_D that change sign are the four at (1,2)(1,2), (2,1)(2,1), (2,3)(2,3), and (3,2)(3,2), because point 2 has the opposite label from the other two.

Step 2: solve the dual problem. The equality constraint is α1−α2+α3=0\alpha_1 - \alpha_2 + \alpha_3 = 0, so α2=α1+α3\alpha_2 = \alpha_1 + \alpha_3. Expand QDQ_D:

α⊤QDα=4α12+α22+4α32−2α1α2−2α2α3=4α12+4α32+α2(α2−2(α1+α3))\boldsymbol{\alpha}^\top Q_D\boldsymbol{\alpha} = 4\alpha_1^2 + \alpha_2^2 + 4\alpha_3^2 - 2\alpha_1\alpha_2 - 2\alpha_2\alpha_3 = 4\alpha_1^2 + 4\alpha_3^2 + \alpha_2\big(\alpha_2 - 2(\alpha_1 + \alpha_3)\big)

Substituting α2=α1+α3\alpha_2 = \alpha_1 + \alpha_3 turns the last term into −(α1+α3)2-(\alpha_1 + \alpha_3)^2, and the whole quadratic term becomes 3α12+3α32−2α1α33\alpha_1^2 + 3\alpha_3^2 - 2\alpha_1\alpha_3. Meanwhile ∑nαn=2(α1+α3)\sum_n \alpha_n = 2(\alpha_1 + \alpha_3). The objective function becomes

12(3α12+3α32−2α1α3)−2α1−2α3\frac{1}{2}\big(3\alpha_1^2 + 3\alpha_3^2 - 2\alpha_1\alpha_3\big) - 2\alpha_1 - 2\alpha_3

Set the partial derivatives with respect to α1\alpha_1 and α3\alpha_3 to 0: 3α1−α3−2=03\alpha_1 - \alpha_3 - 2 = 0 and 3α3−α1−2=03\alpha_3 - \alpha_1 - 2 = 0. Subtracting the two equations gives α1=α3\alpha_1 = \alpha_3; substituting back gives α1=α3=1\alpha_1 = \alpha_3 = 1, and therefore α2=2\alpha_2 = 2. All three are positive, so αn≥0\alpha_n \ge 0 is not violated:

α=(1,2,1),objective=12(3+3−2)−4=−2\boldsymbol{\alpha} = (1, 2, 1), \qquad \text{objective} = \frac{1}{2}(3 + 3 - 2) - 4 = -2

Step 3: compute bb. All three points are support vectors, so compute it from each:

  • From point 1: b=1−[(1)(+1)(4)+(2)(−1)(1)+(1)(+1)(0)]=1−2=−1b = 1 - \big[(1)(+1)(4) + (2)(-1)(1) + (1)(+1)(0)\big] = 1 - 2 = -1
  • From point 2: b=−1−[(1)(+1)(1)+(2)(−1)(1)+(1)(+1)(1)]=−1−0=−1b = -1 - \big[(1)(+1)(1) + (2)(-1)(1) + (1)(+1)(1)\big] = -1 - 0 = -1
  • From point 3: symmetric with point 1, b=−1b = -1

Step 4: write down the classifier.

gSVM(x)=sign((1−x)2−2⋅1+(1+x)2−1)=sign(2x2−1)g_{\text{SVM}}(x) = \text{sign}\big((1 - x)^2 - 2 \cdot 1 + (1 + x)^2 - 1\big) = \text{sign}\big(2x^2 - 1\big)

The −2⋅1-2 \cdot 1 in the middle comes from point 2: K(0,x)=1K(0, x) = 1 whatever xx is. The boundary is at x=±12≈±0.707x = \pm\frac{1}{\sqrt{2}} \approx \pm 0.707; the region between is classified −1-1 and the two sides +1+1. The scores of the three training points are 2−1=12 - 1 = 1, 0−1=−10 - 1 = -1, and 2−1=12 - 1 = 1, and each multiplied by its own yny_n is exactly 1, so all three points lie on the fat boundary and all four KKT conditions hold.

For comparison: really computing in the Z space. With Φ(x)=(1,2x,x2)\boldsymbol{\Phi}(x) = (1, \sqrt{2}x, x^2), the three points become z1=(1,−2,1)\mathbf{z}_1 = (1, -\sqrt{2}, 1), z2=(1,0,0)\mathbf{z}_2 = (1, 0, 0), and z3=(1,2,1)\mathbf{z}_3 = (1, \sqrt{2}, 1).

w=∑nαnynzn=(1,−2,1)−2(1,0,0)+(1,2,1)=(0,0,2)\mathbf{w} = \sum_n \alpha_n y_n\mathbf{z}_n = (1, -\sqrt{2}, 1) - 2(1, 0, 0) + (1, \sqrt{2}, 1) = (0, 0, 2)

w⊤Φ(x)+b=2x2−1\mathbf{w}^\top\boldsymbol{\Phi}(x) + b = 2x^2 - 1, exactly what the kernel route produced. The margin in the Z space is 1/∥w∥=121/\lVert\mathbf{w}\rVert = \frac{1}{2}.

What this result means. From start to finish, the kernel route used only the 9 kernel values between the xnx_n (really 6, because of symmetry). It never wrote down a single zn\mathbf{z}_n and never computed w\mathbf{w}, yet it arrived at the same boundary. In this example d~\tilde{d} is only 2 (not counting the constant), so little is saved; but with the same four steps, you can swap the kernel for a 10th-order polynomial or a Gaussian without changing one word of the procedure. This data set is used again in the next two sections.

Fun Time: two points whose inner product is 10

Question (page 6): Consider two examples x\mathbf{x} and x′\mathbf{x}' such that x⊤x′=10\mathbf{x}^\top\mathbf{x}' = 10. What is KΦ2(x,x′)K_{\boldsymbol{\Phi}_2}(\mathbf{x}, \mathbf{x}')?

  1. 11
  2. 1111
  3. 111111
  4. 11111111

Answer: 3. Plug into the formula: 1+10+102=1111 + 10 + 10^2 = 111. Choice 1 counts only the constant term, choice 2 drops the second-order term, and choice 4 adds an extra 10310^3, a term that only a third-order transform would have.

The question gives neither the dimension of x\mathbf{x} nor the values of x\mathbf{x} and x′\mathbf{x}'. The answer needs just one number, the inner product in the original space, and that is exactly what a kernel means: whether x\mathbf{x} has 3 dimensions or 3,000, and however many terms Φ2\boldsymbol{\Phi}_2 expands into, the answer is unaffected.

Same second-order transform, different coefficients, different kernel: the polynomial kernel

YouTube:Kernel Support Vector Machine :: Polynomial Kernel @ Machine Learning Techniques (機器學習技法)

(Video 00:00–03:50 adds scaling to the second-order transform, 03:50–05:48 looks at the results of three second-order kernels, 05:48–09:18 generalizes to order Q, 09:18–11:29 covers the linear kernel, and Fun Time follows.)

Adding scaling to the transform

Is the Φ2\boldsymbol{\Phi}_2 above the only second-order transform? In the lecture, Lin answers no: there are many second-order transforms, some of which lead to a kernel that is easy to compute and some of which do not. Page 7 of the handout lists three, differing only in the constants in front of the terms. The slide writes the second-order terms as x12,…,xd2x_1^2, \dots, x_d^2 without further explanation; since the kernel on the right of the same row contains (x⊤x′)2(\mathbf{x}^\top\mathbf{x}')^2, that must be shorthand for all d2d^2 products xixjx_ix_j (this is this article's reading).

The original. Φ2(x)=(1,x1,…,xd,x12,…,xd2)\boldsymbol{\Phi}_2(\mathbf{x}) = (1, x_1, \dots, x_d, x_1^2, \dots, x_d^2), corresponding to KΦ2(x,x′)=1+x⊤x′+(x⊤x′)2K_{\boldsymbol{\Phi}_2}(\mathbf{x}, \mathbf{x}') = 1 + \mathbf{x}^\top\mathbf{x}' + (\mathbf{x}^\top\mathbf{x}')^2.

First-order terms multiplied by 2\sqrt{2}. The transform is (1,2x1,…,2xd,x12,…,xd2)(1, \sqrt{2}x_1, \dots, \sqrt{2}x_d, x_1^2, \dots, x_d^2). In the inner product of two such vectors, the first-order part becomes ∑i(2xi)(2xi′)=2 x⊤x′\sum_i (\sqrt{2}x_i)(\sqrt{2}x_i') = 2\,\mathbf{x}^\top\mathbf{x}' and the rest is unchanged:

K2(x,x′)=1+2 x⊤x′+(x⊤x′)2=(1+x⊤x′)2K_2(\mathbf{x}, \mathbf{x}') = 1 + 2\,\mathbf{x}^\top\mathbf{x}' + (\mathbf{x}^\top\mathbf{x}')^2 = (1 + \mathbf{x}^\top\mathbf{x}')^2

The extra 2 is exactly what makes the three terms a perfect square. In the lecture, Lin says that is the benefit of multiplying by 2\sqrt{2}.

Adding a scaling parameter γ\gamma. The transform is (1,2γx1,…,2γxd,γx12,…,γxd2)(1, \sqrt{2\gamma}x_1, \dots, \sqrt{2\gamma}x_d, \gamma x_1^2, \dots, \gamma x_d^2). The inner product of the first-order terms is ∑i(2γxi)(2γxi′)=2γ x⊤x′\sum_i (\sqrt{2\gamma}x_i)(\sqrt{2\gamma}x_i') = 2\gamma\,\mathbf{x}^\top\mathbf{x}'; the inner product of the second-order terms is ∑i∑j(γxixj)(γxi′xj′)=γ2(x⊤x′)2\sum_i\sum_j (\gamma x_ix_j)(\gamma x_i'x_j') = \gamma^2(\mathbf{x}^\top\mathbf{x}')^2, where the last equality uses the same step that split the double sum in the kernel trick section. Together:

K2(x,x′)=1+2γ x⊤x′+γ2(x⊤x′)2=(1+γ x⊤x′)2,γ>0K_2(\mathbf{x}, \mathbf{x}') = 1 + 2\gamma\,\mathbf{x}^\top\mathbf{x}' + \gamma^2(\mathbf{x}^\top\mathbf{x}')^2 = (1 + \gamma\,\mathbf{x}^\top\mathbf{x}')^2, \qquad \gamma > 0

The handout's comment on this K2K_2 is that it is somewhat 'easier' to calculate than KΦ2K_{\boldsymbol{\Phi}_2}, and that it is the commonly used form; in the lecture Lin adds that it also extends more easily to higher orders.

On the slide, the original Φ2\boldsymbol{\Phi}_2 is blue, the one multiplied only by 2\sqrt{2} is red, and the one with γ\gamma is green; the handout's line about equivalent power compares the blue one and the green one. In the lecture, Lin asks on the students' behalf: what exactly is different between blue and green? The answer has two layers.

  • What is the same: both are second-order transforms and correspond to the same space. When you do linear operations in that space, these scalings end up absorbed by the linear coefficients w~\tilde{\mathbf{w}}, so the set of boundaries they can represent is the same. The handout's wording is equivalent power.
  • What is different: they define different inner products. Lin's chain of reasoning in the lecture: a different inner product means a different distance; SVM is an algorithm that decides the margin by distance; so the geometry differs, the computed margin differs, and the final boundary may differ. The handout writes it as different inner product ⟹ different geometry.

Changing the kernel changes the definition of margin

Page 8 of the handout runs SVM three times on the same two-dimensional data and puts the three plots side by side. The plots do not label which class is positive; following the course's usual drawing convention, blue circles are positive and red crosses are negative, and the points boxed by squares are the support vectors:

PlotKernelWhat the boundary looks like
Left(1+0.001 x⊤x′)2(1 + 0.001\,\mathbf{x}^\top\mathbf{x}')^2A single curve opening upward, separating the blue circles above from the red crosses below
Middle1+x⊤x′+(x⊤x′)21 + \mathbf{x}^\top\mathbf{x}' + (\mathbf{x}^\top\mathbf{x}')^2Besides the curve at the top, another region classified blue appears at the bottom of the plot, toward the right
Right(1+1000 x⊤x′)2(1 + 1000\,\mathbf{x}^\top\mathbf{x}')^2The curve at the top is close to the middle plot's, and the bottom again has an extra blue region, closer to the center than in the middle plot

All three are quadratic curves, and all three separate the data perfectly. In the lecture, Lin points out that the left plot's boundary looks simpler, without the extra piece at the bottom of the middle plot, and that the number of support vectors is not quite the same across the three plots (counting boxes on the plots, this article gets 4, 5, and 6 in order). The blue region at the bottom contains no training points at all; this article's interpretation is that it is the other branch of the same quadratic curve, and the made-up example below computes the same phenomenon. The handout's two conclusions:

  • gSVMg_{\text{SVM}} different, SVs different, and it is 'hard' to say which is better before learning. In the lecture, Lin explains that the three each have their own definition of margin, and under different definitions they are not easy to compare.
  • Change of kernel ⇔ change of margin definition. So the kernel needs selecting, just like selecting the transform Φ\boldsymbol{\Phi} before; the transform is now wrapped inside the kernel, and choosing a kernel is choosing a transform.

An example made up for this article: two points, and where γ pushes the boundary. The handout's plots give no data coordinates, so they cannot be reproduced by hand. Here the smallest possible data set is used to compute "same kernel family, different γ\gamma, different boundary". Two points in one dimension: x1=0x_1 = 0 labeled −1-1 and x2=1x_2 = 1 labeled +1+1; the kernel is K2(x,x′)=(1+γxx′)2K_2(x, x') = (1 + \gamma xx')^2.

The kernel values: K(x1,x1)=1K(x_1, x_1) = 1, K(x1,x2)=1K(x_1, x_2) = 1, and K(x2,x2)=(1+γ)2K(x_2, x_2) = (1 + \gamma)^2 (x1=0x_1 = 0 makes the first two values 1). The equality constraint −α1+α2=0-\alpha_1 + \alpha_2 = 0 gives α1=α2=α\alpha_1 = \alpha_2 = \alpha. The entries of QDQ_D are q1,1=1q_{1,1} = 1, q1,2=−1q_{1,2} = -1, and q2,2=(1+γ)2q_{2,2} = (1 + \gamma)^2, and the objective function is

12α2(1−2+(1+γ)2)−2α=12α2 γ(2+γ)−2α\frac{1}{2}\alpha^2\big(1 - 2 + (1 + \gamma)^2\big) - 2\alpha = \frac{1}{2}\alpha^2\,\gamma(2 + \gamma) - 2\alpha

Setting the derivative to 0 gives α=2γ(2+γ)\alpha = \dfrac{2}{\gamma(2 + \gamma)}. Compute bb from point 1: b=−1−α[(−1)(1)+(+1)(1)]=−1b = -1 - \alpha\big[(-1)(1) + (+1)(1)\big] = -1. The classifier is

gSVM(x)=sign(α[(1+γx)2−1]−1)g_{\text{SVM}}(x) = \text{sign}\Big(\alpha\big[(1 + \gamma x)^2 - 1\big] - 1\Big)

The boundary satisfies (1+γx)2−1=1α=γ(2+γ)2(1 + \gamma x)^2 - 1 = \frac{1}{\alpha} = \frac{\gamma(2 + \gamma)}{2}. Expanding the left side and dividing both sides by γ\gamma gives γx2+2x−(1+γ2)=0\gamma x^2 + 2x - \big(1 + \frac{\gamma}{2}\big) = 0, whose solutions are x=−1±1+γ(1+γ/2)γx = \dfrac{-1 \pm \sqrt{1 + \gamma(1 + \gamma/2)}}{\gamma}. Try a few values of γ\gamma:

γ\gammaα\alphaBoundary (two roots)Margin in the Z space
0.0010.001999.5999.50.50010.5001 and −2000.5-2000.50.0220.022
0.10.19.5249.5240.5120.512 and −20.5-20.50.2290.229
110.6670.6670.5810.581 and −2.581-2.5810.8660.866
10100.01670.01670.6810.681 and −0.881-0.8815.485.48
100010002.0×10−62.0 \times 10^{-6}0.70680.7068 and −0.7088-0.7088500.5500.5

Between the two roots the classifier outputs −1-1, and outside them +1+1. The margin column is 1/∥w∥1/\lVert\mathbf{w}\rVert, computed from ∥w∥2=α⊤QDα=2α\lVert\mathbf{w}\rVert^2 = \boldsymbol{\alpha}^\top Q_D\boldsymbol{\alpha} = 2\alpha.

Three things can be read off this table. First, when γ\gamma is very small, the left root runs off to −2000-2000, and near the data you see only the single cut at x≈0.5x \approx 0.5, the midpoint of the two points, so the behavior is almost that of a linear SVM; the reason is that for small γ\gamma the second-order term γx2\gamma x^2 in the transform is much smaller than the first-order term 2γx\sqrt{2\gamma}x. Second, when γ\gamma is very large, the boundary approaches ±12\pm\frac{1}{\sqrt{2}}, symmetric about the origin: the second-order term dominates, and a positive region is drawn even on the negative half-line far from the data. With the same second-order kernel and the same two points, x=0.6x = 0.6 is classified +1+1 when γ=1\gamma = 1 and −1-1 when γ=10\gamma = 10. Third, the margin goes from 0.022 to 500, and each of these numbers measures a distance in a different Z space, so they cannot be compared with one another; this is why Lin says in the lecture that it is not easy to tell which is better.

The general polynomial kernel

Page 9 of the handout adds one more parameter, ζ\zeta, and generalizes the order:

K2(x,x′)=(ζ+γ x⊤x′)2K3(x,x′)=(ζ+γ x⊤x′)3  ⋮KQ(x,x′)=(ζ+γ x⊤x′)Qγ>0, ζ≥0\begin{aligned} K_2(\mathbf{x}, \mathbf{x}') &= (\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}')^2 \\ K_3(\mathbf{x}, \mathbf{x}') &= (\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}')^3 \\ &\ \ \vdots \\ K_Q(\mathbf{x}, \mathbf{x}') &= (\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}')^Q \end{aligned} \qquad \gamma > 0,\ \zeta \ge 0

Each of the three parameters controls one thing. As Lin explains in the lecture, γ\gamma controls the scaling of the original inner product; ζ\zeta controls the constant term and how it combines with the coefficients of the terms of each order; and QQ is the order of the polynomial. The handout says this kernel embeds a QQ-th order transform ΦQ\boldsymbol{\Phi}_Q specially, with the specific coefficients determined by the parameters (γ,ζ)(\gamma, \zeta). Exactly how ζ\zeta enters the coefficients of the transform is the Fun Time question of this section.

The point is the cost. QQ can be 2, 3, or 10, and in the lecture Lin even says 100 or 1000 would be fine: the effort is always one inner product in the original space plus a little arithmetic. This article converts with the formula from Foundations Lecture 12: with d=10d = 10 input features and a 10th-order polynomial transform, the dimension after removing duplicates is (2010)−1=184,755\binom{20}{10} - 1 = 184{,}755; with the kernel it is still one 10-dimensional inner product, plus one addition, one multiplication, and one 10th power. As Lin puts it in the lecture, a 10th-order transform is now almost as easy as a 2nd-order one.

Will such a high order overfit? Lin's answer in the lecture is that it might, but in SVM there is also the large margin to help control complexity. The plot on the right of page 9 is an example: on a two-dimensional data set, the boundary from a 10th-order polynomial kernel is a curve that bends a few times, captioned 10-th order polynomial with margin 0.1. Lin's comment in the lecture is that although it corresponds to a 10th-order transform, this boundary is not bad, and there are not too many support vectors either. The handout calls SVM with the polynomial kernel the polynomial SVM.

Special case: the linear kernel

Page 10 of the handout goes back to the simplest case: Q=1Q = 1, γ=1\gamma = 1, ζ=0\zeta = 0.

K1(x,x′)=(0+1⋅x⊤x′)1=x⊤x′K_1(\mathbf{x}, \mathbf{x}') = (0 + 1 \cdot \mathbf{x}^\top\mathbf{x}')^1 = \mathbf{x}^\top\mathbf{x}'

This is just the usual inner product in the original space, called the linear kernel, and it corresponds to doing no transform at all. In the lecture, Lin explains why the two parameters can be set this way: γ\gamma only scales the original data, and if the data are already properly prepared, 1 will do; the role of the constant term is already handled by the SVM's bb, so ζ\zeta is 0.

The handout says this case is 'even easier': it can be solved efficiently, often in primal form, without necessarily going through the dual. On the right of the slide is a straight line with a gray fat boundary, which Lin says in the lecture is the picture from the very start of the SVM lectures: just go back and solve the original problem of finding the fattest boundary; and if you really want the dual, plugging in the linear kernel works too.

The bottom line of this page is linear first, remember? In the lecture, Lin turns it into an order of operations: try the linear SVM on the data first; if it does well enough, there is no need to try polynomials; if it does not, then move on to polynomials. This is a reminder the course has repeated ever since Foundations.

Fun Time: which transform gives the general second-order kernel

Question (page 11): Consider the general 2-nd polynomial kernel K2(x,x′)=(ζ+γ x⊤x′)2K_2(\mathbf{x}, \mathbf{x}') = (\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}')^2. Which of the following transform can be used to derive this kernel?

  1. Φ(x)=(1,2γx1,…,2γxd,γx12,…,γxd2)\boldsymbol{\Phi}(\mathbf{x}) = (1, \sqrt{2\gamma}x_1, \dots, \sqrt{2\gamma}x_d, \gamma x_1^2, \dots, \gamma x_d^2)
  2. Φ(x)=(ζ,2γx1,…,2γxd,x12,…,xd2)\boldsymbol{\Phi}(\mathbf{x}) = (\zeta, \sqrt{2\gamma}x_1, \dots, \sqrt{2\gamma}x_d, x_1^2, \dots, x_d^2)
  3. Φ(x)=(ζ,2γζx1,…,2γζxd,x12,…,xd2)\boldsymbol{\Phi}(\mathbf{x}) = (\zeta, \sqrt{2\gamma\zeta}x_1, \dots, \sqrt{2\gamma\zeta}x_d, x_1^2, \dots, x_d^2)
  4. Φ(x)=(ζ,2γζx1,…,2γζxd,γx12,…,γxd2)\boldsymbol{\Phi}(\mathbf{x}) = (\zeta, \sqrt{2\gamma\zeta}x_1, \dots, \sqrt{2\gamma\zeta}x_d, \gamma x_1^2, \dots, \gamma x_d^2)

Answer: 4. Expand the target first:

(ζ+γ x⊤x′)2=ζ2+2γζ x⊤x′+γ2(x⊤x′)2(\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}')^2 = \zeta^2 + 2\gamma\zeta\,\mathbf{x}^\top\mathbf{x}' + \gamma^2(\mathbf{x}^\top\mathbf{x}')^2

The handout's answer says we need ζ2\zeta^2 from the 0-th order terms, 2γζ x⊤x′2\gamma\zeta\,\mathbf{x}^\top\mathbf{x}' from the 1-st order terms, and γ2(x⊤x′)2\gamma^2(\mathbf{x}^\top\mathbf{x}')^2 from the 2-nd order terms. If a segment of the transform is multiplied by a constant cc, the inner product of that segment is multiplied by c2c^2, so the constants of the three segments must be ζ\zeta, 2γζ\sqrt{2\gamma\zeta}, and γ\gamma in order. Computing the inner product for each of the four choices:

ChoiceInner productCompared with the target
11+2γ x⊤x′+γ2(x⊤x′)21 + 2\gamma\,\mathbf{x}^\top\mathbf{x}' + \gamma^2(\mathbf{x}^\top\mathbf{x}')^2Equal only when ζ=1\zeta = 1
2ζ2+2γ x⊤x′+(x⊤x′)2\zeta^2 + 2\gamma\,\mathbf{x}^\top\mathbf{x}' + (\mathbf{x}^\top\mathbf{x}')^2The first-order term is missing ζ\zeta, and the second-order term is missing γ2\gamma^2
3ζ2+2γζ x⊤x′+(x⊤x′)2\zeta^2 + 2\gamma\zeta\,\mathbf{x}^\top\mathbf{x}' + (\mathbf{x}^\top\mathbf{x}')^2The second-order term is missing γ2\gamma^2
4ζ2+2γζ x⊤x′+γ2(x⊤x′)2\zeta^2 + 2\gamma\zeta\,\mathbf{x}^\top\mathbf{x}' + \gamma^2(\mathbf{x}^\top\mathbf{x}')^2Equal

This article expanded all four transforms numerically with γ=0.7\gamma = 0.7, ζ=1.3\zeta = 1.3, and two random three-dimensional vectors; only choice 4's inner product equals (ζ+γ x⊤x′)2(\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}')^2.

An addition in this article: this question also explains why the handout requires ζ≥0\zeta \ge 0. The first-order coefficient in choice 4 is 2γζ\sqrt{2\gamma\zeta}; with γ>0\gamma > 0, a negative ζ\zeta has no real square root, so the transform cannot be written with real numbers. The lecture does not derive this condition; Lin only says that going back through the derivation gives it. What is said aloud is that ζ\zeta must be greater than 0, while the slide writes ζ≥0\zeta \ge 0. This article follows the slide, because with ζ=0\zeta = 0 the transform in choice 4 can still be written down (the constant and first-order terms become 0), and ζ=0\zeta = 0 is exactly what the linear kernel uses. What goes wrong when ζ\zeta is negative gets a concrete example in the Fun Time of the section comparing kernels.

An infinite-dimensional transform still has a computable inner product: the Gaussian kernel

YouTube:Kernel Support Vector Machine :: Gaussian Kernel @ Machine Learning Techniques (機器學習技法)

(Video 00:00–07:21 derives the infinite-dimensional transform, 07:21–09:27 covers the hypothesis of the Gaussian SVM and the name RBF, 09:27–11:27 reviews the support vector mechanism, 11:27–13:59 looks at the results for three values of γ, and Fun Time follows.)

The derivation in one dimension

In the lecture, Lin opens with what is described there as a very crazy idea: since a kernel lets us pretend we did the transform and the inner product in the Z space, can this shortcut be pushed to the limit and used with an infinite-dimensional transform? Back when every component in the Z space had to be computed before taking the inner product, infinitely many dimensions were unthinkable. Page 12 of the handout answers: yes, if K(x,x′)K(\mathbf{x}, \mathbf{x}') is efficiently computable.

To see it clearly, let the input have just one dimension, x=(x)\mathbf{x} = (x), and consider this function:

K(x,x′)=exp⁡(−(x−x′)2)K(x, x') = \exp\big(-(x - x')^2\big)

It is a Gaussian function. This time the direction is the reverse of the polynomial kernel: there the transform came first and the kernel was worked out from it; here the function comes first, and the task is to find which transform it is the inner product of. The derivation has four steps.

Step 1: expand the square. −(x−x′)2=−x2−x′2+2xx′-(x - x')^2 = -x^2 - x'^2 + 2xx', and adding exponents means multiplying:

K(x,x′)=exp⁡(−x2) exp⁡(−x′2) exp⁡(2xx′)K(x, x') = \exp(-x^2)\,\exp(-x'^2)\,\exp(2xx')

Of the first two factors, one contains only xx and the other only x′x', so they are already separated. The slide marks the parts containing only xx in blue and the parts containing only x′x' in red. The third factor, exp⁡(2xx′)\exp(2xx'), mixes the two; in the lecture Lin calls it purple, and the remaining work is to split the purple into blue and red.

Step 2: Taylor expansion. The expansion of the exponential function is exp⁡(a)=∑i=0∞aii!\exp(a) = \sum_{i=0}^{\infty} \frac{a^i}{i!}. Take a=2xx′a = 2xx':

exp⁡(2xx′)=∑i=0∞(2xx′)ii!=∑i=0∞2ii! xi x′i\exp(2xx') = \sum_{i=0}^{\infty}\frac{(2xx')^i}{i!} = \sum_{i=0}^{\infty}\frac{2^i}{i!}\,x^i\,x'^i

Step 3: split the coefficient evenly between the two sides. In each term xix^i goes to blue and x′ix'^i goes to red, and the remaining coefficient 2ii!\frac{2^i}{i!} belongs to neither. In the lecture, Lin treats it like dividing up a family's property: to split a number evenly between two sides, give each its square root. This is the same thing as setting 2\sqrt{2} for the polynomial kernel, only in the opposite direction: there the transform's coefficients were chosen and then the kernel was examined; here the coefficient is already present and has to be divided back between the two sides. Move the first two factors inside the sum as well:

K(x,x′)=∑i=0∞(exp⁡(−x2)2ii! xi)(exp⁡(−x′2)2ii! x′i)K(x, x') = \sum_{i=0}^{\infty}\Big(\exp(-x^2)\sqrt{\frac{2^i}{i!}}\,x^i\Big)\Big(\exp(-x'^2)\sqrt{\frac{2^i}{i!}}\,x'^i\Big)

Step 4: recognize the inner product. The two parentheses look exactly alike, except that one holds xx and the other x′x', which means they are the ii-th component of the same transform. Summing over ii from 0 to infinity is the inner product of two infinite-dimensional vectors:

K(x,x′)=Φ(x)⊤Φ(x′),Φ(x)=exp⁡(−x2)⋅(1, 21! x, 222! x2, …)K(x, x') = \boldsymbol{\Phi}(x)^\top\boldsymbol{\Phi}(x'), \qquad \boldsymbol{\Phi}(x) = \exp(-x^2)\cdot\Big(1,\ \sqrt{\frac{2}{1!}}\,x,\ \sqrt{\frac{2^2}{2!}}\,x^2,\ \dots\Big)

The components of Φ(x)\boldsymbol{\Phi}(x) are xix^i of every order, each multiplied by a coefficient, and all of them multiplied by a common exp⁡(−x2)\exp(-x^2) in front. So a Gaussian function hides an infinite-dimensional transform, and computing it takes one subtraction, one square, and one exponential.

This article's numerical check (not from the slides). Take x=1x = 1 and x′=0.5x' = 0.5, so K=exp⁡(−0.25)=0.778801K = \exp(-0.25) = 0.778801. Multiply the components of Φ(1)\boldsymbol{\Phi}(1) and Φ(0.5)\boldsymbol{\Phi}(0.5) one pair at a time and accumulate:

Up to term iiComponent ii of Φ(1)\boldsymbol{\Phi}(1)Component ii of Φ(0.5)\boldsymbol{\Phi}(0.5)Accumulated inner product
00.36790.36790.77880.77880.28650.2865
10.52030.52030.55070.55070.57300.5730
20.52030.52030.27530.27530.71630.7163
40.30040.30040.03970.03970.77600.7760
60.10970.10970.00360.00360.778740.77874
80.02930.02930.00020.00020.778800.77880

With only the first 3 components the inner product is 0.7163, still 8% short of the true value; it takes up to term 8 to match to five decimal places. Every dimension of the infinite-dimensional vector really does contribute, just less and less further out.

The last line of the handout says "more generally": the version with multi-dimensional input and a scaling parameter is the Gaussian kernel:

K(x,x′)=exp⁡(−γ∥x−x′∥2),γ>0K(\mathbf{x}, \mathbf{x}') = \exp\big(-\gamma\lVert\mathbf{x} - \mathbf{x}'\rVert^2\big), \qquad \gamma > 0

The handout does not derive this general form, and in the lecture Lin only says it can be proved that an infinite-dimensional transform is buried in it as well. An addition in this article, sketching the derivation. In one dimension with γ\gamma, the four steps are identical, except that 2xx′2xx' becomes 2γxx′2\gamma xx', giving Φ(x)=exp⁡(−γx2)(1,2γ1!x,(2γ)22!x2,…)\boldsymbol{\Phi}(x) = \exp(-\gamma x^2)\big(1, \sqrt{\frac{2\gamma}{1!}}x, \sqrt{\frac{(2\gamma)^2}{2!}}x^2, \dots\big). In several dimensions, ∥x−x′∥2=x⊤x−2 x⊤x′+x′⊤x′\lVert\mathbf{x} - \mathbf{x}'\rVert^2 = \mathbf{x}^\top\mathbf{x} - 2\,\mathbf{x}^\top\mathbf{x}' + \mathbf{x}'^\top\mathbf{x}', and the mixed factor becomes exp⁡(2γ x⊤x′)=∑i(2γ)ii!(x⊤x′)i\exp(2\gamma\,\mathbf{x}^\top\mathbf{x}') = \sum_i \frac{(2\gamma)^i}{i!}(\mathbf{x}^\top\mathbf{x}')^i. Here (x⊤x′)i(\mathbf{x}^\top\mathbf{x}')^i is the ii-th order polynomial kernel with ζ=0\zeta = 0 and γ=1\gamma = 1, which is itself the inner product of the vectors made of all monomials of order ii; multiply the vector for each ii by (2γ)i/i!\sqrt{(2\gamma)^i/i!} and concatenate them all into one long list, and that is the infinite-dimensional Φ\boldsymbol{\Phi}.

What the hypothesis of the Gaussian SVM looks like

With the kernel in hand, drop it straight into the four-step algorithm from the kernel trick section. Page 13 of the handout writes out the final classifier:

gSVM(x)=sign(∑SVαnynK(xn,x)+b)=sign(∑SVαnynexp⁡(−γ∥x−xn∥2)+b)g_{\text{SVM}}(\mathbf{x}) = \text{sign}\Big(\sum_{\text{SV}}\alpha_n y_n K(\mathbf{x}_n, \mathbf{x}) + b\Big) = \text{sign}\Big(\sum_{\text{SV}}\alpha_n y_n \exp\big(-\gamma\lVert\mathbf{x} - \mathbf{x}_n\rVert^2\big) + b\Big)

Viewed as a function of x\mathbf{x}, exp⁡(−γ∥x−xn∥2)\exp(-\gamma\lVert\mathbf{x} - \mathbf{x}_n\rVert^2) is a Gaussian centered at xn\mathbf{x}_n. So gSVMg_{\text{SVM}} is a linear combination of Gaussians, each centered at a support vector, with coefficient αnyn\alpha_n y_n.

The Gaussian kernel's other name, the Radial Basis Function (RBF) kernel, comes from this picture. In the lecture, Lin takes the name apart: radial refers to a function, like the Gaussian, that spreads outward from some center; basis function refers to a function used in a linear combination. Lin also mentions that the RBF kernel you see in some SVM software means this one.

The same classifier can therefore be read in two ways, and the lecture gives both:

  • From the process: map the data into an infinite-dimensional space and find the fattest hyperplane there.
  • From the result: SVM decides for you which Gaussians to use (centered at which support vectors) and how to combine them (αn\alpha_n; yny_n comes with the data).

The handout's summary puts the two in one sentence: Gaussian SVM finds αn\alpha_n to combine Gaussians centered at xn\mathbf{x}_n and achieves large margin in infinite-dimensional space.

The support vector mechanism is now complete

Page 14 of the handout looks back at what the first three Techniques lectures achieve together. The table has only two rows:

large-margin hyperplanes + higher-order transforms with kernel trick
Number of boundariesnot many
Shape of the boundarysophisticated

The large margin leaves fewer boundaries to choose from, and the transform makes the boundaries sophisticated enough. Page 26 of the Techniques Lecture 1 handout already listed this combination as a new possibility (large-margin hyperplanes plus numerous feature transforms: not many, with sophisticated boundaries); the table on this page is the same column of that table, with the transform part rewritten as higher-order transforms with kernel trick. What was missing then was computational feasibility, and two replacements now supply it:

  • The transformed vector z=Φ(x)\mathbf{z} = \boldsymbol{\Phi}(\mathbf{x}) is replaced by an efficient kernel K(x,x′)K(\mathbf{x}, \mathbf{x}').
  • Storing the optimal w\mathbf{w} is replaced by storing a few support vectors and their αn\alpha_n.

As Lin puts it in the lecture, we stop insisting on two things: we no longer insist on computing z\mathbf{z}, and we no longer insist on storing the w\mathbf{w} of the Z space, only its representation. The handout says the Gaussian SVM brings a new possibility: infinite-dimensional linear classification, with generalization 'guarded by' large-margin.

What happens when γ is too large

Is the story really that rosy? In the lecture, Lin asks this question and then turns to the three plots on page 15 of the handout. Same data, same Gaussian kernel, only γ\gamma changes:

γ\gammaWhat the boundary looks likeHow Lin describes it in the lecture
11A smooth curve, with all the red crosses below itLooks pretty good; the large margin is doing its job
1010The boundary grows several bumps and dents, and the red region puts out tentaclesLike an amoeba
100100Each red cross is wrapped in its own small loop (a few that sit very close merge into one), and everything else is blueEvery cross is a small island, each going its own way

Why does this happen? Lin's explanation in the lecture: a larger γ\gamma amounts to a smaller standard deviation for the Gaussian, so the Gaussian gets sharper; the classifier is a linear combination of Gaussians, and once they are all sharp, the combination comes out as one small island after another. The handout writes large γ\gamma ⟹ sharp Gaussians ⟹ 'overfit'?, and the next line, in red, is warning: SVM can still overfit.

In the lecture, Lin is blunt about this: in the infinite-dimensional space, SVM feels good about itself and believes it has found the fattest boundary, but that fatness, mapped back to the original space, is not necessarily enough. Whether the protection of the large margin is strong enough also depends on the transform itself; the two work together. So the handout's conclusion is that the Gaussian SVM needs careful selection of γ\gamma, and Lin's advice in the lecture is that you usually should not use too large a γ\gamma.

An example made up for this article: islands on three points. Go back to the three points from the kernel trick section (−1-1, 00, 11, with labels +1+1, −1-1, +1+1), change the kernel to exp⁡(−γ(x−x′)2)\exp(-\gamma(x - x')^2), and keep the four steps as they are. Adjacent points are at distance 1, and their kernel value is written k=e−γk = e^{-\gamma}; the two end points are at distance 2, written m=e−4γm = e^{-4\gamma}; a point with itself gives 1.

QD=[1−km−k1−km−k1]Q_D = \begin{bmatrix} 1 & -k & m \\ -k & 1 & -k \\ m & -k & 1 \end{bmatrix}

Again eliminate α2\alpha_2 with α2=α1+α3\alpha_2 = \alpha_1 + \alpha_3 and write s=α1+α3s = \alpha_1 + \alpha_3; the objective function is 12(α12+α32+2mα1α3+(1−2k)s2)−2s\frac{1}{2}\big(\alpha_1^2 + \alpha_3^2 + 2m\alpha_1\alpha_3 + (1 - 2k)s^2\big) - 2s. Take partial derivatives with respect to α1\alpha_1 and α3\alpha_3:

α1+mα3+(1−2k)s−2=0,α3+mα1+(1−2k)s−2=0\alpha_1 + m\alpha_3 + (1 - 2k)s - 2 = 0, \qquad \alpha_3 + m\alpha_1 + (1 - 2k)s - 2 = 0

Subtracting the two equations gives (1−m)(α1−α3)=0(1 - m)(\alpha_1 - \alpha_3) = 0, so α1=α3=a\alpha_1 = \alpha_3 = a and s=2as = 2a. Substituting back: a (1+m+2−4k)=2a\,(1 + m + 2 - 4k) = 2,

α1=α3=23−4k+m,α2=2α1\alpha_1 = \alpha_3 = \frac{2}{3 - 4k + m}, \qquad \alpha_2 = 2\alpha_1

Compute bb from point 2: b=−1−[ak−2a+ak]=−1+2a(1−k)b = -1 - \big[a k - 2a + a k\big] = -1 + 2a(1 - k). The classifier is

gSVM(x)=sign(a e−γ(x+1)2−2a e−γx2+a e−γ(x−1)2+b)g_{\text{SVM}}(x) = \text{sign}\Big(a\,e^{-\gamma(x+1)^2} - 2a\,e^{-\gamma x^2} + a\,e^{-\gamma(x-1)^2} + b\Big)

Three Gaussians, each centered at a support vector, with a negative coefficient on the middle one. Try three values of γ\gamma:

γ\gammak=e−γk = e^{-\gamma}m=e−4γm = e^{-4\gamma}α1=α3\alpha_1 = \alpha_3α2\alpha_2bbRegion classified −1-1
110.36790.36790.01830.01831.29301.29302.58602.58600.63470.6347∣x∣<0.588\lvert x\rvert < 0.588
10104.5×10−54.5 \times 10^{-5}4.2×10−184.2 \times 10^{-18}0.66670.66671.33341.33340.33340.3334∣x∣<0.367\lvert x\rvert < 0.367
1001003.7×10−443.7 \times 10^{-44}about 000.66670.66671.33331.33330.33330.3333∣x∣<0.118\lvert x\rvert < 0.118

All three values of γ\gamma classify the training data perfectly, and yn×y_n \times score is 1 for all three training points (this article checked each one). The difference is the size of the region around the negative example: it is 1.18 wide when γ=1\gamma = 1 and only 0.24 wide when γ=100\gamma = 100. To the γ=100\gamma = 100 classifier, only points hugging x=0x = 0 are negative. x=0.2x = 0.2 is 0.2 away from the negative example and 0.8 away from the nearest positive one, yet it is classified +1+1, because all three Gaussians have decayed to almost 0 there and the score is just b=13b = \frac{1}{3}. This is the one-dimensional version of the islands in the handout's plot: a short distance away from every training point, a single constant is all that is left to decide the classification. By comparison, the polynomial kernel gives a negative region of ∣x∣<0.707\lvert x\rvert < 0.707 on the same data.

With only three points, there is no saying which γ\gamma is right. What this example shows is the mechanism: the larger γ\gamma is, the smaller the range each support vector can influence.

Fun Time: the kernel as γ goes to infinity

Question (page 16): Consider the Gaussian kernel K(x,x′)=exp⁡(−γ∥x−x′∥2)K(\mathbf{x}, \mathbf{x}') = \exp(-\gamma\lVert\mathbf{x} - \mathbf{x}'\rVert^2). What function does the kernel converge to if γ→∞\gamma \to \infty? ([ ⁣[⋅] ⁣][\![\cdot]\!] is the indicator function: 1 when the condition holds and 0 when it does not.)

  1. Klim⁡(x,x′)=0K_{\lim}(\mathbf{x}, \mathbf{x}') = 0
  2. Klim⁡(x,x′)=[ ⁣[x=x′] ⁣]K_{\lim}(\mathbf{x}, \mathbf{x}') = [\![\mathbf{x} = \mathbf{x}']\!]
  3. Klim⁡(x,x′)=[ ⁣[x≠x′] ⁣]K_{\lim}(\mathbf{x}, \mathbf{x}') = [\![\mathbf{x} \ne \mathbf{x}']\!]
  4. Klim⁡(x,x′)=1K_{\lim}(\mathbf{x}, \mathbf{x}') = 1

Answer: 2. There are two cases. When x=x′\mathbf{x} = \mathbf{x}' the distance is 0 and exp⁡(0)=1\exp(0) = 1, regardless of γ\gamma. When x≠x′\mathbf{x} \ne \mathbf{x}' the squared distance is some positive number cc, and exp⁡(−γc)\exp(-\gamma c) goes to 0 as γ→∞\gamma \to \infty. The handout's answer says this limit is an impulse function, an extreme case of how the Gaussian gets sharper as γ\gamma grows.

Choice 1 misses the case x=x′\mathbf{x} = \mathbf{x}'; choice 3 has the two cases reversed; choice 4 is the limit in the other direction, since the kernel approaches 1 everywhere only when γ→0\gamma \to 0.

Lin's comment on this limit in the lecture is that the kernel is merely recognizing whether each example is itself, and you can imagine how little tolerance it has for measurement error. The three-point example above shows this limit directly: kk and mm both become 0, QDQ_D becomes the identity matrix, and the solution is α=(23,43,23)\boldsymbol{\alpha} = (\frac{2}{3}, \frac{4}{3}, \frac{2}{3}) with b=13b = \frac{1}{3}; the γ=100\gamma = 100 row of the table is already nearly identical to it. At that point the classifier gives 11, −1-1, 11 only at the three training points themselves and outputs sign(13)=+1\text{sign}(\frac{1}{3}) = +1 everywhere else: the training data are all correct, but the only thing it has learned is to memorize these three points.

How to choose among three kernels, and which functions count as kernels

YouTube:Kernel Support Vector Machine :: Comparison of Kernels @ Machine Learning Techniques (機器學習技法)

(Video 00:00–01:51 covers the linear kernel, 01:51–04:47 the polynomial kernel, 04:47–06:53 the Gaussian kernel, 06:53–11:16 other valid kernels and Mercer's condition, 11:16–11:52 is Fun Time, and the summary of the whole lecture follows.)

Pros and cons of the three kernels

Pages 17 to 19 of the handout each spend one page listing the Cons and Pros of one kernel. Here they are merged into one table first, followed by what the lecture adds.

KernelConsProsThe handout's one-liner
linear: x⊤x′\mathbf{x}^\top\mathbf{x}'restricted, the data are not always linearly separablesafe; fast, with a special QP solver in primal; very explainable, w\mathbf{w} and the SVs say somethingan important basic tool
polynomial: (ζ+γ x⊤x′)Q(\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}')^Qnumerical difficulty for large QQ; three parameters (γ,ζ,Q)(\gamma, \zeta, Q), more difficult to selectless restricted than linear; QQ has a clear physical meaningperhaps small-QQ only
Gaussian: exp⁡(−γ∥x−x′∥2)\exp(-\gamma\lVert\mathbf{x} - \mathbf{x}'\rVert^2)mysterious, with no w\mathbf{w}; slower than linear; possibly too powerfulmore powerful than the other two; bounded, with less numerical difficulty than polynomial; one parameter onlyone of most popular but shall be used with care

The linear kernel. The lecture goes into more detail on why it is fast: because no transform is involved, a special QP solver can be designed for the primal problem, and that is usually faster than going through the dual, where computing QDQ_D alone takes a lot of effort. Explainable means that each component of w\mathbf{w} is the weight of the corresponding feature, while the support vectors tell you which points the machine considers important. For the Cons cell, the slide shows a plot: red crosses spread around the outside and blue circles in the middle, which no straight line can separate. Lin's positioning in the lecture: if you are going to use SVM, linear is the tool you should try first.

The polynomial kernel. The handout gives the source of the numerical difficulty in two lines: when ∣ζ+γ x⊤x′∣<1\lvert\zeta + \gamma\,\mathbf{x}^\top\mathbf{x}'\rvert < 1, K→0K \to 0, and when it is greater than 1, KK becomes big. The lecture uses the 20th power as its example; this article works it out: 0.520≈9.5×10−70.5^{20} \approx 9.5 \times 10^{-7} and 220=1,048,5762^{20} = 1{,}048{,}576, so bases that differ by only a factor of 4 give kernel values 12 orders of magnitude apart. Lin says in the lecture that such a wide numerical range is hard work for a general QP solver and makes inaccurate solutions more likely. The example of physical meaning: if you believe the relationship among features is at most cubic, you can express that picture directly with Q=3Q = 3. The price of having many parameters is that trying all the combinations of (γ,ζ,Q)(\gamma, \zeta, Q) takes a lot of time.

The footer of the handout page has a sentence that is easy to skip: the polynomial kernel is sometimes efficiently done by linear on ΦQ(x)\boldsymbol{\Phi}_Q(\mathbf{x}). Lin expands on it in the lecture: when the order is very low (second or third, say) and d~\tilde{d} is not too large yet, expanding the transform directly and solving with a special solver for the primal linear SVM is sometimes faster; there is no need to assume a polynomial transform must go through a kernel.

The Gaussian kernel. It is bounded because the value of a Gaussian function lies between 0 and 1, so it does not blow up the way a polynomial does. For why it is hard to explain, Lin sets a scene in the lecture: your boss asks how this model classifies, and you answer that it first goes to an infinite-dimensional space, finds the fattest thing there, and then comes back; the boss has zoned out by the second sentence. It is slower than linear because the dual problem has to be solved and QDQ_D has to be computed. Too powerful refers to the islands in the Gaussian kernel section. Lin's summary in the lecture is that it is probably one of the most commonly used SVM kernels, at the price that the parameter has to be chosen carefully, the computation takes waiting, and you do not fully know how the classification is being made.

Page 22 of the handout wraps this section into one phrase: linear for efficiency or Gaussian for power.

Can other functions serve as kernels: Mercer's condition

Are these three the only options? Page 20 of the handout starts from what a kernel means: a kernel represents a special similarity, namely Φ(x)⊤Φ(x′)\boldsymbol{\Phi}(\mathbf{x})^\top\boldsymbol{\Phi}(\mathbf{x}'). In the lecture, Lin explains why an inner product is a similarity: when two vectors are perpendicular the inner product is 0, and you can say they are very dissimilar; when they point in opposite directions the inner product is negative.

So can you take whatever similarity function you like and feed it to SVM as a kernel? The handout's answer is not really. A kernel is a similarity, but it is the specific similarity "inner product of vectors in the Z space", and not every similarity can be written as an inner product in some space. Lin says in the lecture that if you want to use your own kernel, you have to find a way to show why it is a kernel.

Start with the necessary conditions: if KK really is the inner product of some Φ\boldsymbol{\Phi}, what properties must it have?

Symmetric. The inner product of vectors is symmetric, Φ(x)⊤Φ(x′)=Φ(x′)⊤Φ(x)\boldsymbol{\Phi}(\mathbf{x})^\top\boldsymbol{\Phi}(\mathbf{x}') = \boldsymbol{\Phi}(\mathbf{x}')^\top\boldsymbol{\Phi}(\mathbf{x}), so K(x,x′)=K(x′,x)K(\mathbf{x}, \mathbf{x}') = K(\mathbf{x}', \mathbf{x}).

The kernel matrix is positive semi-definite. Take NN points x1,…,xN\mathbf{x}_1, \dots, \mathbf{x}_N of your choice and arrange kij=K(xi,xj)k_{ij} = K(\mathbf{x}_i, \mathbf{x}_j) into an N×NN \times N matrix; the handout also writes this matrix as KK:

K=[Φ(x1)⊤Φ(x1)Φ(x1)⊤Φ(x2)⋯Φ(x1)⊤Φ(xN)Φ(x2)⊤Φ(x1)Φ(x2)⊤Φ(x2)⋯Φ(x2)⊤Φ(xN)⋮⋮⋱⋮Φ(xN)⊤Φ(x1)Φ(xN)⊤Φ(x2)⋯Φ(xN)⊤Φ(xN)]K = \begin{bmatrix} \boldsymbol{\Phi}(\mathbf{x}_1)^\top\boldsymbol{\Phi}(\mathbf{x}_1) & \boldsymbol{\Phi}(\mathbf{x}_1)^\top\boldsymbol{\Phi}(\mathbf{x}_2) & \cdots & \boldsymbol{\Phi}(\mathbf{x}_1)^\top\boldsymbol{\Phi}(\mathbf{x}_N) \\ \boldsymbol{\Phi}(\mathbf{x}_2)^\top\boldsymbol{\Phi}(\mathbf{x}_1) & \boldsymbol{\Phi}(\mathbf{x}_2)^\top\boldsymbol{\Phi}(\mathbf{x}_2) & \cdots & \boldsymbol{\Phi}(\mathbf{x}_2)^\top\boldsymbol{\Phi}(\mathbf{x}_N) \\ \vdots & \vdots & \ddots & \vdots \\ \boldsymbol{\Phi}(\mathbf{x}_N)^\top\boldsymbol{\Phi}(\mathbf{x}_1) & \boldsymbol{\Phi}(\mathbf{x}_N)^\top\boldsymbol{\Phi}(\mathbf{x}_2) & \cdots & \boldsymbol{\Phi}(\mathbf{x}_N)^\top\boldsymbol{\Phi}(\mathbf{x}_N) \end{bmatrix}

In each entry the left factor depends only on the row and the right factor only on the column (blue and red respectively on the slide). Treat each zn=Φ(xn)\mathbf{z}_n = \boldsymbol{\Phi}(\mathbf{x}_n) as a row and stack them into a matrix ZZ, and the whole matrix is

K=[z1z2⋯zN]⊤[z1z2⋯zN]=ZZ⊤K = \begin{bmatrix} \mathbf{z}_1 & \mathbf{z}_2 & \cdots & \mathbf{z}_N \end{bmatrix}^\top \begin{bmatrix} \mathbf{z}_1 & \mathbf{z}_2 & \cdots & \mathbf{z}_N \end{bmatrix} = ZZ^\top

The handout says ZZ⊤ZZ^\top must always be positive semi-definite, and in the lecture Lin says this is a very basic result in linear algebra. This article fills in the step: for every vector v∈RN\mathbf{v} \in \mathbb{R}^N,

v⊤Kv=v⊤ZZ⊤v=(Z⊤v)⊤(Z⊤v)=∥∑n=1Nvnzn∥2≥0\mathbf{v}^\top K\mathbf{v} = \mathbf{v}^\top ZZ^\top\mathbf{v} = (Z^\top\mathbf{v})^\top(Z^\top\mathbf{v}) = \Big\lVert\sum_{n=1}^{N} v_n\mathbf{z}_n\Big\rVert^2 \ge 0

The squared length of a vector cannot be negative, and that is the definition of positive semi-definite.

Together these two are called Mercer's condition. The handout writes necessary & sufficient: they are not only necessary conditions but sufficient ones too. For a function that satisfies both, there is guaranteed to be some Φ\boldsymbol{\Phi} that makes it equal to Φ(x)⊤Φ(x′)\boldsymbol{\Phi}(\mathbf{x})^\top\boldsymbol{\Phi}(\mathbf{x}'). In the lecture, Lin notes that sufficiency was proved by mathematicians, is harder, and is not covered in this course.

The condition can be used in two directions, and they differ greatly in difficulty. To reject a function, it is enough to find one data set whose matrix is not positive semi-definite, which is what the Fun Time below does. To accept a function, you must prove that the matrix is positive semi-definite for every data set and every NN, and Lin says in the lecture that this is really not that easy. The handout's comment: define your own kernel: possible, but hard. Lin's advice in the lecture is to keep this condition in mind if the day comes when you really do want to define your own kernel.

An addition in this article on how this relates to SVM. As an addition in the article on Techniques Lecture 2: Dual SVM noted, the dual problem is convex because α⊤QDα=∥∑nαnynzn∥2≥0\boldsymbol{\alpha}^\top Q_D\boldsymbol{\alpha} = \lVert\sum_n \alpha_n y_n\mathbf{z}_n\rVert^2 \ge 0. With a kernel, α⊤QDα=∑n∑m(αnyn)(αmym)K(xn,xm)\boldsymbol{\alpha}^\top Q_D\boldsymbol{\alpha} = \sum_n\sum_m (\alpha_n y_n)(\alpha_m y_m)K(\mathbf{x}_n, \mathbf{x}_m), which is v⊤Kv\mathbf{v}^\top K\mathbf{v} with vn=αnynv_n = \alpha_n y_n. Only when the kernel matrix is positive semi-definite is QDQ_D positive semi-definite, and only then is the QP convex. Feed in an invalid function and there is no Z space behind it, and what gets handed to the QP solver may no longer be a convex problem.

Fun Time: which one is not a valid kernel

Question (page 21): Which of the following is not a valid kernel? (Hint: Consider two 1-dimensional vectors x1=(1)\mathbf{x}_1 = (1) and x2=(−1)\mathbf{x}_2 = (-1) and check Mercer's condition.)

  1. K(x,x′)=(−1+x⊤x′)2K(\mathbf{x}, \mathbf{x}') = (-1 + \mathbf{x}^\top\mathbf{x}')^2
  2. K(x,x′)=(0+x⊤x′)2K(\mathbf{x}, \mathbf{x}') = (0 + \mathbf{x}^\top\mathbf{x}')^2
  3. K(x,x′)=(1+x⊤x′)2K(\mathbf{x}, \mathbf{x}') = (1 + \mathbf{x}^\top\mathbf{x}')^2
  4. K(x,x′)=(−1−x⊤x′)2K(\mathbf{x}, \mathbf{x}') = (-1 - \mathbf{x}^\top\mathbf{x}')^2

Answer: 1. Follow the hint and compute the four 2×2 matrices. The inner products of the two points are x1⊤x1=1\mathbf{x}_1^\top\mathbf{x}_1 = 1, x1⊤x2=−1\mathbf{x}_1^\top\mathbf{x}_2 = -1, and x2⊤x2=1\mathbf{x}_2^\top\mathbf{x}_2 = 1.

ChoiceDiagonal: K(x1,x1)=K(x2,x2)K(\mathbf{x}_1, \mathbf{x}_1) = K(\mathbf{x}_2, \mathbf{x}_2)Off-diagonal: K(x1,x2)K(\mathbf{x}_1, \mathbf{x}_2)Eigenvalues
1(−1+1)2=0(-1 + 1)^2 = 0(−1−1)2=4(-1 - 1)^2 = 444 and −4-4
2(0+1)2=1(0 + 1)^2 = 1(0−1)2=1(0 - 1)^2 = 122 and 00
3(1+1)2=4(1 + 1)^2 = 4(1−1)2=0(1 - 1)^2 = 044 and 44
4(−1−1)2=4(-1 - 1)^2 = 4(−1+1)2=0(-1 + 1)^2 = 044 and 44

For a 2×2 symmetric matrix whose diagonal entries are both pp and whose off-diagonal entries are both qq, the eigenvalues are p+qp + q and p−qp - q (with eigenvectors (1,1)(1, 1) and (1,−1)(1, -1)). The matrix for choice 1 is

K=[0440]K = \begin{bmatrix} 0 & 4 \\ 4 & 0 \end{bmatrix}

It has a negative eigenvalue, so it is not positive semi-definite. A direct check works too: take v=(1,−1)\mathbf{v} = (1, -1), and v⊤Kv=0−4−4+0=−8<0\mathbf{v}^\top K\mathbf{v} = 0 - 4 - 4 + 0 = -8 < 0. One data set is enough to reject it.

For the other three choices, the matrix from these two points is positive semi-definite, but that only shows they passed this one test and cannot be taken as proof of validity. The handout's answer gives the real reasons: choices 2 and 3 are just polynomial kernels (ζ=0\zeta = 0 and ζ=1\zeta = 1, with γ=1\gamma = 1 and Q=2Q = 2), which have explicit transforms; choice 4 is the same function as choice 3, because (−1−t)2=(1+t)2(-1 - t)^2 = (1 + t)^2.

Two additions in this article. First, choice 1 is the "polynomial kernel" with ζ=−1\zeta = -1, which violates the handout's requirement ζ≥0\zeta \ge 0 and picks up the question left open by the Fun Time in the polynomial kernel section. Second, the contradiction is visible even without eigenvalues: K(x1,x1)=0K(\mathbf{x}_1, \mathbf{x}_1) = 0 means ∥Φ(x1)∥2=0\lVert\boldsymbol{\Phi}(\mathbf{x}_1)\rVert^2 = 0, so Φ(x1)\boldsymbol{\Phi}(\mathbf{x}_1) can only be the zero vector; the inner product of the zero vector with every vector is 0, yet K(x1,x2)K(\mathbf{x}_1, \mathbf{x}_2) is 4.

What happens if you force it into SVM anyway? This article's trial calculation: label x1\mathbf{x}_1 as +1+1 and x2\mathbf{x}_2 as −1-1; the diagonal of QDQ_D is 0 and the off-diagonal is (+1)(−1)(4)=−4(+1)(-1)(4) = -4. The equality constraint gives α1=α2=a\alpha_1 = \alpha_2 = a, and the objective function is 12(−8a2)−2a=−4a2−2a\frac{1}{2}(-8a^2) - 2a = -4a^2 - 2a, which keeps decreasing as aa grows and has no minimum. The dual problem simply breaks.

Summary of this lecture and the next one

Page 22 of the handout wraps each of the four sections into one line: the kernel is a shortcut of transform + inner product; the polynomial kernel embeds a specially-scaled polynomial transform; the Gaussian kernel embeds an infinite dimensional transform; choose linear for efficiency or Gaussian for power.

At the end of the lecture, Lin points out one thing that has not been relaxed so far: from Techniques Lecture 1 until now, we have insisted that the data be linearly separable in the Z space. That insistence can make SVM fit the noise, and the overfitting seen in the Gaussian kernel section is related to it. The handout announces what the next lecture deals with: avoiding overfitting in Gaussian (and other kernels).

Self-check

1. Let x=(1,2)\mathbf{x} = (1, 2) and x′=(2,0)\mathbf{x}' = (2, 0). Compute four kernel values: KΦ2K_{\boldsymbol{\Phi}_2}; K2=(1+γ x⊤x′)2K_2 = (1 + \gamma\,\mathbf{x}^\top\mathbf{x}')^2 with γ=1\gamma = 1; the polynomial kernel with ζ=2\zeta = 2, γ=0.5\gamma = 0.5, Q=3Q = 3; and the Gaussian kernel with γ=0.5\gamma = 0.5.

Answer: x⊤x′=2\mathbf{x}^\top\mathbf{x}' = 2 and ∥x−x′∥2=(−1)2+22=5\lVert\mathbf{x} - \mathbf{x}'\rVert^2 = (-1)^2 + 2^2 = 5. In order: 1+2+4=71 + 2 + 4 = 7; (1+2)2=9(1 + 2)^2 = 9; (2+0.5×2)3=27(2 + 0.5 \times 2)^3 = 27; exp⁡(−2.5)≈0.0821\exp(-2.5) \approx 0.0821. All four numbers use only the inner product or the distance in the original space.

2. In the three-point example (polynomial kernel K2=(1+xx′)2K_2 = (1 + xx')^2, α=(1,2,1)\boldsymbol{\alpha} = (1, 2, 1), b=−1b = -1), do not use the simplified formula 2x2−12x^2 - 1; predict the label of x=0.6x = 0.6 using kernel values only.

Answer: K(−1,0.6)=(1−0.6)2=0.16K(-1, 0.6) = (1 - 0.6)^2 = 0.16, K(0,0.6)=1K(0, 0.6) = 1, and K(1,0.6)=(1+0.6)2=2.56K(1, 0.6) = (1 + 0.6)^2 = 2.56. The score is (1)(+1)(0.16)+(2)(−1)(1)+(1)(+1)(2.56)−1=−0.28(1)(+1)(0.16) + (2)(-1)(1) + (1)(+1)(2.56) - 1 = -0.28, so the prediction is −1-1. Checking with 2×0.36−1=−0.282 \times 0.36 - 1 = -0.28 gives the same value.

3. After training, a kernel SVM has no w\mathbf{w}. So how is bb computed? And how is w⊤Φ(x)\mathbf{w}^\top\boldsymbol{\Phi}(\mathbf{x}) computed at prediction time?

Criterion: the answer must say that w=∑nαnynzn\mathbf{w} = \sum_n \alpha_n y_n\mathbf{z}_n is a linear combination of the support vectors; that w\mathbf{w} only appears in the form of an inner product with some z\mathbf{z}, which after substitution becomes ∑nαnyn zn⊤z\sum_n \alpha_n y_n\,\mathbf{z}_n^\top\mathbf{z}, where every term is an inner product of two z\mathbf{z} vectors and can be replaced by K(xn,x)K(\mathbf{x}_n, \mathbf{x}); and that therefore b=ys−∑SVαnynK(xn,xs)b = y_s - \sum_{\text{SV}}\alpha_n y_n K(\mathbf{x}_n, \mathbf{x}_s) and prediction uses ∑SVαnynK(xn,x)+b\sum_{\text{SV}}\alpha_n y_n K(\mathbf{x}_n, \mathbf{x}) + b. An answer that only says "use the kernel" without mentioning that w\mathbf{w} is a linear combination does not count as correct.

4. Is K(x,x′)=−x⊤x′K(\mathbf{x}, \mathbf{x}') = -\mathbf{x}^\top\mathbf{x}' a valid kernel? What about K(x,x′)=1+x⊤x′K(\mathbf{x}, \mathbf{x}') = 1 + \mathbf{x}^\top\mathbf{x}'?

Answer: the first is not. It is symmetric, but take just one point x1=(1)\mathbf{x}_1 = (1) and the 1×1 matrix is [−1][-1], which is not positive semi-definite; put another way, K(x,x)K(\mathbf{x}, \mathbf{x}) is ∥Φ(x)∥2\lVert\boldsymbol{\Phi}(\mathbf{x})\rVert^2 and cannot be negative. The second is: it is the polynomial kernel with ζ=1\zeta = 1, γ=1\gamma = 1, Q=1Q = 1, and its transform, Φ(x)=(1,x1,…,xd)\boldsymbol{\Phi}(\mathbf{x}) = (1, x_1, \dots, x_d), can be written down directly.

5. You have N=10,000N = 10{,}000 examples and d=100d = 100 features, and you want to train an SVM with a 10th-order polynomial transform. How many variables does the primal problem have to face? If you go through the kernel, roughly how much computation does preparing QDQ_D take?

Answer: the dimension of the 10th-order polynomial transform after removing duplicates is (11010)−1≈4.7×1013\binom{110}{10} - 1 \approx 4.7 \times 10^{13}, and the number of variables in the primal problem is that number plus 1; not even a single zn\mathbf{z}_n can be stored. Through the kernel: QDQ_D has 10810^8 entries (about half that with symmetry), each one a 100-dimensional inner product plus a few operations, for about 101010^{10} multiplications in total, which an ordinary computer can do. The price is that QDQ_D itself has 10810^8 entries to store, and that at prediction time every new example needs one kernel evaluation against each support vector.

Exercises and corresponding reading

The corresponding weeks in two semesters. The Fall 2024 course page puts kernel SVM and soft-margin SVM in the same week (W10, 11/04), and the slide code for this lecture is 203u. The Fall 2026 course page likewise puts it in W10 (11/11), with the two lectures taught together. The Fall 2026 203u_handout.pdf link returned 404 when opened on 2026-09-30, so this article uses only the MOOC version of the handout.

Textbook. Both course pages map this lecture to e-8.3 of Learning from Data (LFD). e-Chapter 8 is an online chapter of LFD and is not in the printed book.

Homework. The related problems are in Fall 2024 HW6 (released 2024-11-18, due 12-02), which has no official solutions:

ProblemContentWhere it connects to this lecture
Q1Apply the kernel trick to PLA: maintain α\boldsymbol{\alpha} instead of w\mathbf{w}The step "w\mathbf{w} is a linear combination of the zn\mathbf{z}_n" used when computing bb and predicting
Q7With a large enough γ\gamma, the Gaussian kernel can achieve Ein=0E_{\text{in}} = 0The Fun Time of the Gaussian kernel section and the three-point example
Q8Prove that exp⁡(2cos⁡(x−x′)−2)\exp(2\cos(x - x') - 2) is a valid kernelMercer's condition and the Gaussian kernel
Q10MNIST 3 versus 7, polynomial kernel, comparing the number of support vectors for different CC and QQThe polynomial kernel (CC is a parameter from the next lecture)
Q11The same data, Gaussian kernel, comparing the margin for different CC and γ\gammaThe Gaussian kernel (CC is a parameter from the next lecture)

Q10 and Q11 use the soft-margin parameter CC, so do them after reading the next lecture. The data for the programming problems is mnist.scale from the LIBSVM website. The problem set recommends LIBSVM and warns about two things: do not let the package rescale the data automatically, or you have in effect changed the kernel; and confirm for yourself that the package solves the dual form from the lecture, with enough numerical precision. The first point connects directly to this lecture (the following is this article's explanation): multiplying all features by the same constant cc multiplies both x⊤x′\mathbf{x}^\top\mathbf{x}' and ∥x−x′∥2\lVert\mathbf{x} - \mathbf{x}'\rVert^2 by c2c^2, which has the same effect as replacing γ\gamma with c2γc^2\gamma; scaling each feature by a different factor amounts to switching to another kernel. The guide to the whole assignment is in Techniques Homework and Final Project.

Hands-on. Redo the three-point example with a different kernel: use KΦ2(x,x′)=1+xx′+(xx′)2K_{\boldsymbol{\Phi}_2}(x, x') = 1 + xx' + (xx')^2 and follow the four steps to solve for α\boldsymbol{\alpha}, bb, and the boundary. This article has worked it out, and the answer is still α=(1,2,1)\boldsymbol{\alpha} = (1, 2, 1), b=−1b = -1, and the boundary 2x2−1=02x^2 - 1 = 0. Think about why the two kernels give the same answer on this data (hint: the two transforms differ only in the coefficient of the first-order term, and what is the first-order component of the optimal w\mathbf{w}?).

Further reading. Chapter 5 of the CS229 main notes introduces kernel methods in a different order and can be read side by side with this lecture's presentation. Caltech Learning from Data is the English-language course for the same textbook.

Next lecture. Techniques Lecture 4: Soft-Margin SVM relaxes the requirement that the data be perfectly separated and deals with the overfitting this lecture saw with the Gaussian kernel.

Course video sources

Original videos: Kernel Trick, Polynomial Kernel, Gaussian Kernel, Comparison of Kernels

Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist on that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.

Update Log

  • 2026-10-13: Originally combined with Lecture 4 in one article; changed to one article per lecture and rewritten from the handout and the video transcripts.
  • 2026-10-10: Rechecked the video status. The embedded videos match the lecture numbering of the official playlist.
  • 2026-10-10: Marked the video status and verified the recording sources and how they were obtained.

References