🌏 中文版
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 ". 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 and from , 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:
- Find the bottleneck: the dual problem's still needs -dimensional inner products.
- A shortcut for the second-order transform: computing the transform and the inner product together takes only .
- The kernel trick: the three places in the dual SVM that use an inner product are all replaced by a kernel function.
- The polynomial kernel: adding scaling to the transform gives .
- The Gaussian kernel: the same move pushed to infinitely many dimensions.
- 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
(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:
There are variables and constraints, and neither number depends on . is hiding in the entries of :
This is an inner product in . 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: must be calculated faster than .
In the lecture, Lin then looks at this inner product as two actions. Seen from the X space, means transforming each of the two 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 is written slightly differently from the one in Foundations Lecture 12:
and 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, has components: one constant, first-order terms, and second-order terms (one for every pair ).
Now take two points and and, following the definition, multiply the transformed vectors component by component and add everything up:
The troublesome part is the double sum at the end, which has terms. What Lin does in the lecture is collect the factors that involve on one side and the factors that involve on the other. Each term is a product of four numbers, and does not contain , so it can be pulled out of the inner sum:
The second equality holds because the sum in parentheses does not contain , so it is a constant as far as the outer sum is concerned. Both parentheses are the inner product in the original space, , so
To evaluate the right-hand side, spend computing once, then square and add. The handout's conclusion is that for , transform + inner product can be carefully done in instead of . 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 , , and .
- By definition: and , each with components. The inner product is .
- By the shortcut: , and .
The two agree. With the difference is invisible, but it becomes obvious once is 100: the definition requires expanding each point into 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 corresponds to a kernel function:
The result above is written , with the subscript saying which transform it corresponds to. In the lecture, Lin stresses that a kernel function takes the original 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:
Second, computing . The previous lecture picks a support vector and uses . What appears here is the inner product of and , and is a -dimensional vector. In the lecture, Lin voices the student's doubt first: at first glance this is not an inner product of two vectors, so a kernel cannot be used. The way out is another KKT condition from the previous lecture, : is itself a linear combination of the . Substitute it in, and the inner product distributes over the sum:
Third, prediction. For a new input , the classifier is . is just another , so the same move applies:
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. is never computed at all. It only ever appears in the form of an inner product with some , 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 .
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 :
- Compute and , then prepare from the equality constraint and the bound constraints .
- .
- Pick a support vector and set .
- Return the support vectors, their , and . For a new , .
The sums in steps 3 and 4 run over the support vectors only, because terms with 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 from each, and average, which is numerically more stable.
The handout lists the cost of each step:
| Step | Cost | Relation to |
|---|---|---|
| 1 | kernel evaluations | none, if the kernel is fast to compute |
| 2 | a QP with variables and constraints | none |
| 3, 4 | kernel evaluations | none, if the kernel is fast to compute |
The handout's summary: kernel SVM uses a computational shortcut to avoid 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 . The kernel SVM has no to store; it stores the support vectors themselves, their (and ), and . 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:
| 1 | ||
| 2 | ||
| 3 |
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 , which is only introduced formally in the next section (treat it as a given function for now; its transform is , because ).
Step 1: compute the kernel matrix and . For example, , , and . Compute them all, then multiply by :
The entries of that change sign are the four at , , , and , because point 2 has the opposite label from the other two.
Step 2: solve the dual problem. The equality constraint is , so . Expand :
Substituting turns the last term into , and the whole quadratic term becomes . Meanwhile . The objective function becomes
Set the partial derivatives with respect to and to 0: and . Subtracting the two equations gives ; substituting back gives , and therefore . All three are positive, so is not violated:
Step 3: compute . All three points are support vectors, so compute it from each:
- From point 1:
- From point 2:
- From point 3: symmetric with point 1,
Step 4: write down the classifier.
The in the middle comes from point 2: whatever is. The boundary is at ; the region between is classified and the two sides . The scores of the three training points are , , and , and each multiplied by its own 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 , the three points become , , and .
, exactly what the kernel route produced. The margin in the Z space is .
What this result means. From start to finish, the kernel route used only the 9 kernel values between the (really 6, because of symmetry). It never wrote down a single and never computed , yet it arrived at the same boundary. In this example 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 and such that . What is ?
Answer: 3. Plug into the formula: . Choice 1 counts only the constant term, choice 2 drops the second-order term, and choice 4 adds an extra , a term that only a third-order transform would have.
The question gives neither the dimension of nor the values of and . The answer needs just one number, the inner product in the original space, and that is exactly what a kernel means: whether has 3 dimensions or 3,000, and however many terms expands into, the answer is unaffected.
Same second-order transform, different coefficients, different kernel: the polynomial kernel
(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 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 without further explanation; since the kernel on the right of the same row contains , that must be shorthand for all products (this is this article's reading).
The original. , corresponding to .
First-order terms multiplied by . The transform is . In the inner product of two such vectors, the first-order part becomes and the rest is unchanged:
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 .
Adding a scaling parameter . The transform is . The inner product of the first-order terms is ; the inner product of the second-order terms is , where the last equality uses the same step that split the double sum in the kernel trick section. Together:
The handout's comment on this is that it is somewhat 'easier' to calculate than , 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 is blue, the one multiplied only by is red, and the one with 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 , 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:
| Plot | Kernel | What the boundary looks like |
|---|---|---|
| Left | A single curve opening upward, separating the blue circles above from the red crosses below | |
| Middle | Besides the curve at the top, another region classified blue appears at the bottom of the plot, toward the right | |
| Right | The 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:
- 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 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 , different boundary". Two points in one dimension: labeled and labeled ; the kernel is .
The kernel values: , , and ( makes the first two values 1). The equality constraint gives . The entries of are , , and , and the objective function is
Setting the derivative to 0 gives . Compute from point 1: . The classifier is
The boundary satisfies . Expanding the left side and dividing both sides by gives , whose solutions are . Try a few values of :
| Boundary (two roots) | Margin in the Z space | ||
|---|---|---|---|
| and | |||
| and | |||
| and | |||
| and | |||
| and |
Between the two roots the classifier outputs , and outside them . The margin column is , computed from .
Three things can be read off this table. First, when is very small, the left root runs off to , and near the data you see only the single cut at , the midpoint of the two points, so the behavior is almost that of a linear SVM; the reason is that for small the second-order term in the transform is much smaller than the first-order term . Second, when is very large, the boundary approaches , 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, is classified when and when . 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, , and generalizes the order:
Each of the three parameters controls one thing. As Lin explains in the lecture, controls the scaling of the original inner product; controls the constant term and how it combines with the coefficients of the terms of each order; and is the order of the polynomial. The handout says this kernel embeds a -th order transform specially, with the specific coefficients determined by the parameters . Exactly how enters the coefficients of the transform is the Fun Time question of this section.
The point is the cost. 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 input features and a 10th-order polynomial transform, the dimension after removing duplicates is ; 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: , , .
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: 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 , so 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 . Which of the following transform can be used to derive this kernel?
Answer: 4. Expand the target first:
The handout's answer says we need from the 0-th order terms, from the 1-st order terms, and from the 2-nd order terms. If a segment of the transform is multiplied by a constant , the inner product of that segment is multiplied by , so the constants of the three segments must be , , and in order. Computing the inner product for each of the four choices:
| Choice | Inner product | Compared with the target |
|---|---|---|
| 1 | Equal only when | |
| 2 | The first-order term is missing , and the second-order term is missing | |
| 3 | The second-order term is missing | |
| 4 | Equal |
This article expanded all four transforms numerically with , , and two random three-dimensional vectors; only choice 4's inner product equals .
An addition in this article: this question also explains why the handout requires . The first-order coefficient in choice 4 is ; with , a negative 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 must be greater than 0, while the slide writes . This article follows the slide, because with the transform in choice 4 can still be written down (the constant and first-order terms become 0), and is exactly what the linear kernel uses. What goes wrong when 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
(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 is efficiently computable.
To see it clearly, let the input have just one dimension, , and consider this function:
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. , and adding exponents means multiplying:
Of the first two factors, one contains only and the other only , so they are already separated. The slide marks the parts containing only in blue and the parts containing only in red. The third factor, , 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 . Take :
Step 3: split the coefficient evenly between the two sides. In each term goes to blue and goes to red, and the remaining coefficient 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 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:
Step 4: recognize the inner product. The two parentheses look exactly alike, except that one holds and the other , which means they are the -th component of the same transform. Summing over from 0 to infinity is the inner product of two infinite-dimensional vectors:
The components of are of every order, each multiplied by a coefficient, and all of them multiplied by a common 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 and , so . Multiply the components of and one pair at a time and accumulate:
| Up to term | Component of | Component of | Accumulated inner product |
|---|---|---|---|
| 0 | |||
| 1 | |||
| 2 | |||
| 4 | |||
| 6 | |||
| 8 |
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:
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 , the four steps are identical, except that becomes , giving . In several dimensions, , and the mixed factor becomes . Here is the -th order polynomial kernel with and , which is itself the inner product of the vectors made of all monomials of order ; multiply the vector for each by and concatenate them all into one long list, and that is the infinite-dimensional .
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:
Viewed as a function of , is a Gaussian centered at . So is a linear combination of Gaussians, each centered at a support vector, with coefficient .
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 (; comes with the data).
The handout's summary puts the two in one sentence: Gaussian SVM finds to combine Gaussians centered at 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 boundaries | not many |
| Shape of the boundary | sophisticated |
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 is replaced by an efficient kernel .
- Storing the optimal is replaced by storing a few support vectors and their .
As Lin puts it in the lecture, we stop insisting on two things: we no longer insist on computing , and we no longer insist on storing the 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 changes:
| What the boundary looks like | How Lin describes it in the lecture | |
|---|---|---|
| A smooth curve, with all the red crosses below it | Looks pretty good; the large margin is doing its job | |
| The boundary grows several bumps and dents, and the red region puts out tentacles | Like an amoeba | |
| Each red cross is wrapped in its own small loop (a few that sit very close merge into one), and everything else is blue | Every cross is a small island, each going its own way |
Why does this happen? Lin's explanation in the lecture: a larger 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 ⟹ 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 , and Lin's advice in the lecture is that you usually should not use too large a .
An example made up for this article: islands on three points. Go back to the three points from the kernel trick section (, , , with labels , , ), change the kernel to , and keep the four steps as they are. Adjacent points are at distance 1, and their kernel value is written ; the two end points are at distance 2, written ; a point with itself gives 1.
Again eliminate with and write ; the objective function is . Take partial derivatives with respect to and :
Subtracting the two equations gives , so and . Substituting back: ,
Compute from point 2: . The classifier is
Three Gaussians, each centered at a support vector, with a negative coefficient on the middle one. Try three values of :
| Region classified | ||||||
|---|---|---|---|---|---|---|
| about |
All three values of classify the training data perfectly, and 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 and only 0.24 wide when . To the classifier, only points hugging are negative. is 0.2 away from the negative example and 0.8 away from the nearest positive one, yet it is classified , because all three Gaussians have decayed to almost 0 there and the score is just . 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 on the same data.
With only three points, there is no saying which is right. What this example shows is the mechanism: the larger 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 . What function does the kernel converge to if ? ( is the indicator function: 1 when the condition holds and 0 when it does not.)
Answer: 2. There are two cases. When the distance is 0 and , regardless of . When the squared distance is some positive number , and goes to 0 as . The handout's answer says this limit is an impulse function, an extreme case of how the Gaussian gets sharper as grows.
Choice 1 misses the case ; choice 3 has the two cases reversed; choice 4 is the limit in the other direction, since the kernel approaches 1 everywhere only when .
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: and both become 0, becomes the identity matrix, and the solution is with ; the row of the table is already nearly identical to it. At that point the classifier gives , , only at the three training points themselves and outputs 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
(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.
| Kernel | Cons | Pros | The handout's one-liner |
|---|---|---|---|
| linear: | restricted, the data are not always linearly separable | safe; fast, with a special QP solver in primal; very explainable, and the SVs say something | an important basic tool |
| polynomial: | numerical difficulty for large ; three parameters , more difficult to select | less restricted than linear; has a clear physical meaning | perhaps small- only |
| Gaussian: | mysterious, with no ; slower than linear; possibly too powerful | more powerful than the other two; bounded, with less numerical difficulty than polynomial; one parameter only | one 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 alone takes a lot of effort. Explainable means that each component of 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 , , and when it is greater than 1, becomes big. The lecture uses the 20th power as its example; this article works it out: and , 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 . The price of having many parameters is that trying all the combinations of 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 . Lin expands on it in the lecture: when the order is very low (second or third, say) and 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 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 . 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 really is the inner product of some , what properties must it have?
Symmetric. The inner product of vectors is symmetric, , so .
The kernel matrix is positive semi-definite. Take points of your choice and arrange into an matrix; the handout also writes this matrix as :
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 as a row and stack them into a matrix , and the whole matrix is
The handout says 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 ,
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 that makes it equal to . 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 , 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 . With a kernel, , which is with . Only when the kernel matrix is positive semi-definite is 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 and and check Mercer's condition.)
Answer: 1. Follow the hint and compute the four 2×2 matrices. The inner products of the two points are , , and .
| Choice | Diagonal: | Off-diagonal: | Eigenvalues |
|---|---|---|---|
| 1 | and | ||
| 2 | and | ||
| 3 | and | ||
| 4 | and |
For a 2×2 symmetric matrix whose diagonal entries are both and whose off-diagonal entries are both , the eigenvalues are and (with eigenvectors and ). The matrix for choice 1 is
It has a negative eigenvalue, so it is not positive semi-definite. A direct check works too: take , and . 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 ( and , with and ), which have explicit transforms; choice 4 is the same function as choice 3, because .
Two additions in this article. First, choice 1 is the "polynomial kernel" with , which violates the handout's requirement and picks up the question left open by the Fun Time in the polynomial kernel section. Second, the contradiction is visible even without eigenvalues: means , so can only be the zero vector; the inner product of the zero vector with every vector is 0, yet is 4.
What happens if you force it into SVM anyway? This article's trial calculation: label as and as ; the diagonal of is 0 and the off-diagonal is . The equality constraint gives , and the objective function is , which keeps decreasing as 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 and . Compute four kernel values: ; with ; the polynomial kernel with , , ; and the Gaussian kernel with .
Answer: and . In order: ; ; ; . All four numbers use only the inner product or the distance in the original space.
2. In the three-point example (polynomial kernel , , ), do not use the simplified formula ; predict the label of using kernel values only.
Answer: , , and . The score is , so the prediction is . Checking with gives the same value.
3. After training, a kernel SVM has no . So how is computed? And how is computed at prediction time?
Criterion: the answer must say that is a linear combination of the support vectors; that only appears in the form of an inner product with some , which after substitution becomes , where every term is an inner product of two vectors and can be replaced by ; and that therefore and prediction uses . An answer that only says "use the kernel" without mentioning that is a linear combination does not count as correct.
4. Is a valid kernel? What about ?
Answer: the first is not. It is symmetric, but take just one point and the 1×1 matrix is , which is not positive semi-definite; put another way, is and cannot be negative. The second is: it is the polynomial kernel with , , , and its transform, , can be written down directly.
5. You have examples and 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 take?
Answer: the dimension of the 10th-order polynomial transform after removing duplicates is , and the number of variables in the primal problem is that number plus 1; not even a single can be stored. Through the kernel: has entries (about half that with symmetry), each one a 100-dimensional inner product plus a few operations, for about multiplications in total, which an ordinary computer can do. The price is that itself has 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:
| Problem | Content | Where it connects to this lecture |
|---|---|---|
| Q1 | Apply the kernel trick to PLA: maintain instead of | The step " is a linear combination of the " used when computing and predicting |
| Q7 | With a large enough , the Gaussian kernel can achieve | The Fun Time of the Gaussian kernel section and the three-point example |
| Q8 | Prove that is a valid kernel | Mercer's condition and the Gaussian kernel |
| Q10 | MNIST 3 versus 7, polynomial kernel, comparing the number of support vectors for different and | The polynomial kernel ( is a parameter from the next lecture) |
| Q11 | The same data, Gaussian kernel, comparing the margin for different and | The Gaussian kernel ( is a parameter from the next lecture) |
Q10 and Q11 use the soft-margin parameter , 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 multiplies both and by , which has the same effect as replacing with ; 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 and follow the four steps to solve for , , and the boundary. This article has worked it out, and the answer is still , , and the boundary . 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 ?).
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
- Techniques Lecture 3 handout, 203_handout.pdf: the main source for this article. The derivations in the four sections, the four Fun Time questions, the plots on pages 8, 9, and 15, and the pros and cons of the three kernels all come from this handout.
- Techniques Lecture 2: Dual SVM: the dual problem, the KKT conditions, and the formulas that recover w and b from α, which this lecture uses directly.
- Foundations Lecture 12: Nonlinear Transformation: the dimension formula for the Q-th order polynomial transform, used to convert to d̃.
- Machine Learning Techniques YouTube playlist: the IDs and titles of the four videos; what the text reports as said "in the lecture" is paraphrased from machine transcripts of these four videos.
- Hsuan-Tien Lin > MOOCs: the official entry for handouts and recordings.
- Machine Learning, Fall 2024 course page: the corresponding week and LFD chapter.
- Machine Learning, Fall 2026 course page: the corresponding week and LFD chapter.
- Fall 2024 Homework 6 (hw6_red.pdf): the content of Q1, Q7, Q8, Q10, and Q11.
- LIBSVM: the package recommended for the HW6 programming problems.
- LIBSVM Data: mnist.scale: the data for the HW6 programming problems.
- Learning from Data textbook website: the source of e-8.3.
- CS229 main notes, main_notes.pdf: further reading, the kernel methods in Chapter 5.
- Caltech Learning from Data telecourse: further reading, the English-language course for the same textbook.
- Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques: Course Overview: where this lecture sits in the whole course and the reading routes.