🌏 中文版
Video status: Videos included. This article is written from the official MOOC handout 204_handout.pdf and transcripts of this lecture's 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 4 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 in the first, Embedding Numerous Features: Kernel Models. The same page sums up the previous lecture, Techniques Lecture 3: Kernel SVM, this way: the kernel is a shortcut to (transform + inner product), so the SVM no longer depends on the transformed dimension and can range from simple linear models all the way to the infinite-dimensional Gaussian kernel. The previous lecture left a warning when it covered the Gaussian kernel: SVM can still overfit. This lecture deals with exactly that.
The prerequisites are the whole derivation in Techniques Lecture 2: Dual SVM (the Lagrange function, the partial derivatives with respect to and , the KKT conditions, complementary slackness), the kernel function from the previous lecture, and cross-validation from Foundations Lecture 15: Validation. After reading, you should be able to write down the soft-margin SVM primal problem and derive its dual on your own, say where the upper bound on comes from, tell from a solved where each point lies and which points to compute from, and explain why the number of support vectors can serve as a safety check in model selection.
The four sections of this lecture answer four questions in order:
- How to rewrite the problem when the data cannot be separated, or should not be forced apart.
- What the dual problem looks like after the rewrite.
- How to compute once is solved, and what the value of means.
- How to choose the extra parameter (and the kernel's parameters).
Why allow mistakes: the cost of insisting on separation, the violation ξ, and the parameter C
(Video 00:00–01:12 reviews the previous lecture, 01:12–03:00 covers what is wrong with hard-margin, 03:00–06:39 combines pocket with hard-margin, 06:39–08:54 covers the two problems with the combination, 08:54–13:50 covers the violation and the parameter , and Fun Time follows.)
What is wrong with hard-margin SVM
Page 2 of the handout splits the previous lecture's warning into two causes. SVM overfits partly because the transform is too powerful, and partly because hard-margin insists that the data be perfectly separable. On the first cause, Lin says in the lecture that some transforms are so powerful that even a large margin cannot rescue the result.
The page shows two figures drawn from the same data. The left one is labeled : a straight line roughly separates the circles from the crosses, with a few points on the wrong side. The right one is labeled : after a fourth-order polynomial transform, a boundary that winds back and forth classifies every point correctly. Lin's comment in the lecture is that the left figure looks like a reasonable hypothesis, one line and a few mistakes; if you insist on getting every circle and cross right, you end up with the right figure, and although its boundary is a large-margin one, it is not necessarily the most reasonable explanation of this data.
The handout puts the conclusion in one line: if always insisting on separable (which implies shatter), you have power to overfit to noise. In the lecture, Lin fills in the middle step: being able to separate the data perfectly no matter what combination of circles and crosses arrives is exactly the shatter of Foundations Lecture 5: Training versus Testing; a model that can produce every labeling takes in the noise as well.
Combining pocket with hard-margin SVM
Page 3 of the handout is titled Give Up on Some Examples: we want to give up on some noisy examples. In the lecture, Lin looks back at how noise was handled before and finds pocket from Foundations Lecture 2: Perceptrons and PLA: when no line separates the data, look for the line with the fewest mistakes instead and let the points it gets wrong go. The two old methods side by side:
| What it minimizes | Constraints | |
|---|---|---|
| the number of misclassified points | none | |
| hard-margin SVM | for every |
is notation used in Foundations: its value is 1 when the statement inside holds and 0 when it does not. The handout combines the two into one problem:
Correctly classified points must stay far enough from the boundary, as before. Misclassified points are written as "greater than or equal to negative infinity", which Lin explains in the lecture as no condition at all: leave them alone. Relaxing the constraints is not enough. Lin points out that if mistakes cost nothing, the machine might as well get everything wrong, so the objective has to add the number of mistakes the way pocket does, and avoid mistakes wherever it can.
The objective now has two terms: one wants short (a fat boundary), the other wants few mistakes, and the parameter sets their relative importance. In the handout's words, : trade-off of large margin & noise tolerance. A large cares more about not making mistakes, and a small cares more about a wide margin for the points that are classified correctly. In the lecture, Lin calls this new problem the combination of hard-margin SVM and pocket.
Two problems with the combination
Page 4 of the handout first merges the two constraints into one:
The right-hand side is 1 for a correct point and negative infinity for an incorrect one. The page then lists two things wrong with this formulation.
First, it is not a QP. is a "right or wrong" Boolean operation, not a linear one. Techniques Lecture 1: Linear SVM said that quadratic programming requires a quadratic objective and linear constraints; now both sides contain something that is neither linear nor quadratic. Lin says in the lecture that this means none of the machinery derived so far, the dual and the kernel, can be used. Lin also mentions that pocket itself is an NP-hard problem, and this combination is just as hard to solve.
Second, it cannot tell small errors from large ones. The handout writes small error (slightly away from fat boundary) and large error (a...w...a...y... from fat boundary). The numbers Lin gives in the lecture: one point that should be a circle has score , almost correct; another that should also be a circle has score , very far from the boundary. In the formula above, each of these counts as just "one mistake".
Recording margin violations instead: soft-margin SVM
The fix is to give every point one more variable that records its margin violation. Lin's definition in the lecture: we want times the score to be at least 1, and records how far this point falls short of that 1. Short by a little is a small error; short by a lot is a large one. Then the thing being penalized changes from "how many are wrong" to "how much is violated in total", which the handout calls penalize with margin violation instead of error count:
This is soft-margin SVM. Lin explains the name in the lecture: the requirement on the margin is no longer hard, rigid and not open to change, but soft, allowing some violation. Both problems are taken care of:
- The constraints are linear. is linear in , and so is .
- The objective is quadratic. As Lin puts it in the lecture, there was already a quadratic in ; adding a linear term in gives a quadratic in . So it is still a QP.
- Large and small errors can be told apart. Lin says in the lecture that errors are no longer black or white; a gray zone is allowed, and large and small errors are each measured and then added up as the penalty.
The figure on page 5 of the handout draws a green fat boundary with one blue point inside the band, labeled violation, along with the segment from the point to the edge of the band. Lin says in the lecture that this point lies inside the fat boundary, so it is in violation, and its distance to the edge of the fat boundary is the amount of the violation. An addition in this article: strictly speaking, is measured in units of the score, and the geometric distance is ; the two differ by a fixed factor.
The same page restates what the parameter means, now as trade-off of large margin & margin violation:
- Large : want less margin violation. As Lin puts it in the lecture, better a thinner margin than places that violate it.
- Small : want large margin. If many points violate it, so be it.
Finally, count the size of this QP. The variables are , the components of , and the values , for in total. There are two constraints per point, in total; Lin says in the lecture that the extra are the , because a violation is recorded only when there is one and cannot be negative. The variable count still contains , so the next step is the same as in the previous two lectures: derive the dual and get rid of .
Numerical example: six points that cannot be separated
The following is an example made up for this article, not from the slides; the next three sections keep using it. The data is one-dimensional with no feature transform, :
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
Roughly, negatives are on the left and positives on the right, but points 3 and 4 have swapped places: the positive is at 4 and the negative at 6.
Hard-margin SVM has no solution. Point 3 requires and point 4 requires ; subtracting gives , that is, . Point 2 requires and point 5 requires ; subtracting gives , that is, . The two requirements contradict each other, so no satisfies all the constraints.
How the soft-margin objective scores a candidate. For a given , each must satisfy and , and the objective wants it as small as possible, so takes the larger of the two lower bounds (this is also the point of this section's Fun Time). Work it out for four candidates:
| Candidate | Boundary | Two edges of the fat boundary | Misclassified | |||
|---|---|---|---|---|---|---|
| I | , | 2 | ||||
| II | , | 2 | ||||
| III | , | 2 | ||||
| IV | , | 1 |
Take point 3 under candidate I: , which is short of 1 by , so . Point 2 under candidate III: , correctly classified but inside the fat boundary, . A point whose violation is below 1 is still on the correct side; a point whose violation is above 1 has been misclassified.
Now compute :
| Candidate | Objective at | Objective at | |
|---|---|---|---|
| I | |||
| II | |||
| III | |||
| IV |
What this result means.
- At candidate I has the lowest objective, and at candidate III does. The half-width of III's fat boundary is , while I's is only 2; III's total violation is more than I's 3. A smaller buys a fatter boundary at the cost of more violation, the same direction page 5 of the handout describes. These two really are the optimal solutions in their respective settings: this article swept the objective over a grid with spacing in each of and , and the later passages "Numerical example: the dual problem for the six points" and "Numerical example: b and KKT for the six points" confirm it with the dual solution and the KKT conditions.
- Candidate IV misclassifies only one point, the fewest mistakes possible on this data (with the positive at 4 and the negative at 6, a threshold that predicts negative on the left and positive on the right gets at least one of them wrong wherever it is placed; with positive on the left and negative on the right instead, the four points at the two ends would all be wrong). Yet under soft-margin SVM's scoring it is the worst at both values of : its margin is very thin, and point 4 is very far from where the candidate wants it, . Raising to 100 changes nothing: I is and IV is . Soft-margin SVM minimizes the sum of the violations, and that is a different goal from "the fewest misclassified points".
- Compare with the combination on page 3 of the handout, the one that counts mistakes: I's objective is and IV's is . At , I is better ( versus ); at , IV is better ( versus ). The two formulations pick different lines on the same data.
- Candidates I and II both have their boundary at ; II's fat boundary is thinner, yet its violations are larger (2 versus ). The reason is that is measured in units of the score: the larger is, the larger the score difference that the same distance converts into.
Fun Time: how large is the violation
Question (page 6): At the optimal solution of the soft-margin SVM primal problem, assume that . What is the corresponding ?
Answer: 2. The handout's answer: is simply when . Lin's version in the lecture is shorter: we want 1, we have , and the gap is 11.
To complete the reasoning: the constraints require and . The coefficient of in the objective is , and the optimal solution will not make it larger than necessary, so . The general form is . Option 1 does not satisfy the constraint (); options 3 and 4 satisfy it but pay an extra penalty of and for nothing, so they cannot be optimal.
Deriving the dual: one more set of multipliers, and the result gains only an upper bound
(Video 00:00–02:02 writes the Lagrange function, 02:02–04:55 eliminates and , 04:55–06:10 covers the other simplifications, 06:10–07:10 covers the standard dual problem, and Fun Time follows.)
Two sets of constraints, two sets of multipliers
The approach is exactly that of Techniques Lecture 2: write the Lagrange function first, with one multiplier for each constraint. There are now two sets of constraints (page 7 of the handout marks them in red and blue), so there are two sets of multipliers as well:
- is paired with . Rearranged into the "less than or equal to 0" form, it is .
- is paired with . Rearranged, it is .
In the lecture, Lin makes a point of watching the signs: every constraint has to be moved to the less-than-or-equal-to-0 side first. The Lagrange function is the objective plus "multiplier times its constraint":
The Lagrange dual problem to solve is
An addition in this article: the handout simply writes "want: Lagrange dual" here and does not repeat why solving the dual instead is allowed. The reason is the same as in Techniques Lecture 2, and it needs the three conditions for strong duality: the primal is convex, the constraints are linear, and the primal is feasible. The first two hold at a glance. The third required separable data in the hard-margin case, but for soft-margin it always holds: take , , and every , and all the constraints are satisfied. This is why page 12 of the handout says the soft-margin primal and dual are always solvable.
Eliminating ξ and β first
The inner puts no constraint on , so the partial derivatives are 0 at the minimum. Page 8 of the handout starts with the new variable . One particular appears in three places in , and Lin finds them one by one in the lecture: in the objective, in the term, and in the term. So
That is, at the optimal solution and add up to exactly . The handout goes on to say there is no loss of optimality if solving with the implicit constraint , so can be replaced altogether. Before replacing it, confirm that the original requirements on the two sets of multipliers survive:
- : unchanged.
- : becomes , that is, .
So requiring automatically makes greater than or equal to 0, and Lin says in the lecture that from here on never needs to be considered again. This is where the upper bound on comes from: it is " cannot be negative" written another way.
An addition in this article, a way of seeing why adding this constraint loses no optimality. Collecting the terms of that contain gives , which is linear in . Whenever the coefficient is not 0, the inner min can push off to infinity in the right direction and send to . The outer level is looking for a maximum and will not pick such an , so only the case where the coefficient is 0 needs to be considered. This is the same reasoning Techniques Lecture 2 used for .
After substituting , the terms containing are
The slide crosses this whole term out. Lin says in the lecture that this elimination does two things: is gone and, more importantly, every is gone too, just as was eliminated in the hard-margin case. The remaining problem is
The rest is identical to hard-margin
Page 9 of the handout writes "familiar? :-)" next to this expression. The inner problem in the parentheses is exactly the inner problem of hard-margin in Techniques Lecture 2; the only difference is the upper bound on outside. Lin says in the lecture that these steps were all done two lectures ago, so the details are not redone, and anyone unsure should go back to Lecture 2. Here the steps are filled in.
Partial derivative with respect to . The part of containing is , so
Once the constraint is added, the term containing disappears.
Partial derivative with respect to . For the -th component: differentiating gives , and differentiating gives , so
Substitute back. The term is already gone, leaving , and the inner minimum is
The standard soft-margin dual
Expand the squared norm into a double sum, then multiply by to turn the max into a min, and you have the standard soft-margin SVM dual on page 10 of the handout:
Two more relations hold implicitly. They are not constraints on , but they are used later: , and .
Below the formula the handout writes: only difference to hard-margin: upper bound on . As Lin puts it in the lecture, if you can solve the hard-margin dual you can solve this one too; it is just that is no longer unbounded. This is still a convex QP with variables; counting the constraints the way Lin does in the lecture, there is one equality, plus one lower bound 0 and one upper bound for each , for in total. Neither the number of variables nor the number of constraints depends on , and can be replaced by a kernel as in the previous lecture.
Side by side with the primal:
| Primal problem | Dual problem | |
|---|---|---|
| Variables | ||
| Constraints | ||
| Where appears | the coefficient of in the objective | the upper bound on |
| a variable | eliminated, computed afterwards |
Numerical example: the dual problem for the six points
Continue with the six points from earlier (an example made up for this article). In one dimension, is a single number:
The dual problem is to minimize subject to and .
First see what happens without the upper bound. Take with . The equality constraint: , which holds. . The objective is . The larger is, the lower the objective, with no lower bound. The hard-margin primal has no solution on this data, and in the dual that shows up as an objective that can fall without limit. With added, can go no higher than , and this direction is blocked.
Solve . Solving a six-variable QP directly by hand is tedious, so the approach here is "guess the pattern, solve, then verify". Candidate I on the primal side suggests it: points 1 and 6 are far from the fat boundary, points 3 and 4 violate it, and points 2 and 5 sit exactly on its edges. Guess and (at the upper bound; why a violating point ends up at the upper bound is explained later, in the passage "The value of α sorts the data into three kinds"). The equality constraint becomes , so ; call it . Then
Differentiate with respect to : , , which lies between 0 and 1. So
Verify that this really is optimal. Written back in the max form of the dual, this has value . Weak duality from Techniques Lecture 2 says that the value given by a feasible never exceeds the optimal value of the primal; and candidate I, , has a primal objective of exactly as well. A lower bound equals a value that is actually attained, so candidate I is the optimal solution of the primal, this is the optimal solution of the dual, and strong duality is visible in this example.
Note that , computed from , is , which agrees with candidate I; has not appeared yet, because the dual problem does not contain it.
Fun Time: C increases by 2, how does the dual change
Question (page 11): In the soft-margin SVM, assume that we want to increase the parameter by 2. How shall the corresponding dual problem be changed?
- the upper bound of shall be halved
- the upper bound of shall be decreased by 2
- the upper bound of shall be increased by 2
- the upper bound of shall be doubled
Answer: 3. The handout's answer: because is exactly the upper bound of , increasing by 2 in the primal problem is equivalent to increasing the upper bound by 2 in the dual problem. The example Lin gives in the lecture is going from 10 to 12, with the upper limit of going from 10 to 12 as well.
From the derivation, the upper bound comes from , where appears on the right-hand side untouched, neither multiplied nor divided, so whatever is added to it is added to the bound. Options 1 and 2 go in the wrong direction. Option 4 happens to be right only when goes from exactly 2 to 4. Neither the dual objective nor the equality constraint contains , so they need no change.
After solving for α: computing b, and what each point's α reveals
(Video 00:00–01:55 covers the kernel soft-margin SVM algorithm, 01:55–05:47 computes , 05:47–08:28 looks at Gaussian SVM with different , 08:28–12:32 covers the physical meaning of , and Fun Time follows.)
The kernel soft-margin SVM algorithm
Page 12 of the handout changes one thing in the previous lecture's kernel hard-margin SVM algorithm:
- Compute , , and the that express three kinds of constraints: equality, lower-bound, and upper-bound.
- .
- Return the support vectors, their , and . For a new ,
What step 1 adds is the set of upper-bound constraints, which Lin says in the lecture comes from the parameter . An addition in this article: in the format of the QP solver from Techniques Lecture 1, which accepts only "", the upper bound has to be written as .
The handout makes two remarks on this algorithm. It is almost the same as hard-margin; and it is more flexible, since the primal and dual are always solvable. As Lin puts it in the lecture: hard-margin has a solution only when the data is separable, which is no longer required, so you always get a solution, and lets you adjust whether you care more about a fat boundary or about fewer mistakes; soft-margin is the form of SVM more commonly used in practice. For the rest of this section, Lin simply calls soft-margin SVM "SVM" in the lecture.
The remaining question is step 3. The QP has changed and the KKT conditions have changed with it, so can still be computed the old way?
Computing b takes a free SV
Start by listing complementary slackness. The rule from Techniques Lecture 2 is that for each constraint and its multiplier, at least one is 0 (in the lecture, Lin keeps the analogy that Harry Potter and Voldemort cannot both be alive). Hard-margin has just one condition:
Soft-margin has two sets of constraints, so it has two (page 13 of the handout):
The second was originally . Lin explains in the lecture that is already known to be and the minus sign does not affect "the product is 0", so it is written directly as .
Following the hard-margin approach, find a support vector with . The first condition forces the parenthesis to be 0:
Multiply both sides by (): , so
Compared with hard-margin there is one more term, . The trouble is that is a violation, and you need before you can compute it. Lin says in the lecture that this turns into a chicken-and-egg problem that never finishes.
The way out is to make that term 0. The second complementary slackness condition says that whenever , that is, , we have . A support vector that also satisfies is called a free SV; Lin explains in the lecture that free means unconstrained, not tied down by . For a free SV,
This is the same formula as in hard-margin, and the inner product is still computed by the kernel. Lin says in the lecture that every free SV gives the same .
What if there is no free SV? The handout has only one phrase, "range of otherwise". Lin adds in the lecture that this case is rare but does happen; then can only be bounded by a set of inequalities and may have more than one answer, and whichever satisfies all the KKT conditions will do. The lecture does not go into the details; the example below runs into this case once.
Numerical example: b and KKT for the six points
(An example made up for this article, continuing with the six points.) At , and . The free SVs are points 2 and 5:
- Using point 2: .
- Using point 5: .
The two free SVs give the same . If you mistakenly plug point 3, which has , into the hard-margin formula, you get , which is wrong; the full formula is right, but it needs to be known first.
With in hand, every point's can be computed, and the two complementary slackness conditions can be checked point by point:
| Kind | ||||||||
|---|---|---|---|---|---|---|---|---|
| 1 | non-support vector | |||||||
| 2 | free SV | |||||||
| 3 | bounded SV | |||||||
| 4 | bounded SV | |||||||
| 5 | free SV | |||||||
| 6 | non-support vector |
The other conditions hold too: , every lies between 0 and 1, and every point satisfies . The classifier is , with its boundary at and the fat boundary running from 3 to 7.
Try a few other values of . The same approach (guess the pattern, solve, verify KKT; the numbers were separately checked by a program using exact fractions) gives:
| Half-width of the fat boundary | free SV | bounded SV | |||||
|---|---|---|---|---|---|---|---|
| points 1, 6 | points 2, 3, 4, 5 | ||||||
| to | none | points 2, 3, 4, 5 | |||||
| points 2, 5 | points 3, 4 | ||||||
| points 2, 5 | points 3, 4 |
Three things to read from this table:
- As grows, the margin thins and the violation drops. As goes from to 1, the half-width of the fat boundary shrinks from 5 to 2, the total violation falls from to 3, and the bounded SVs drop from four to two.
- has no free SV. Solving with the pattern gives , which exceeds the upper bound , so points 2 and 5 hit the upper bound as well and all four support vectors are bounded. Now is constrained only by inequalities: point 2 is bounded and requires , that is, , ; point 5 requires , . Every in this range has objective , and every one is optimal. This is the handout's "range of otherwise".
- As goes from 1 to 100, the classifier does not change. Points 3 and 4 have swapped places and no line can rescue them, so a larger only makes larger along with it. In this example, as long as , stays within the upper bound, the solution is always , , and raising further never produces a different classifier. The figures that come next in the handout use the Gaussian kernel, whose boundary can keep bending, and things are different there.
Soft-margin Gaussian SVM with different C
Page 14 of the handout shows three figures for , , and , on the same data with the Gaussian kernel. In the lecture, Lin first explains how to read them: the black line is the boundary, with the blue region above it and the red region below; the gray area is the margin, that is, what the fat boundary in space looks like when mapped back to space.
- : the boundary is gentle and the gray area is wide. Lin points out in the lecture that this figure has quite a few violations: some red points fall in the blue region (misclassified), and some blue points are on the correct side but inside the gray area.
- : the boundary starts to bend, the gray area narrows, and there are fewer violations. Lin picks out a blue point that was misclassified a moment ago and is now on the correct side, though still inside the gray area: a large error has become a small one.
- : the boundary is more convoluted still, and at the top of the figure it even closes off a small loop, which gets an isolated cross right as well.
The handout's note is large ⟹ less noise tolerance ⟹ 'overfit'?, and it writes once more warning: SVM can still overfit. Lin's question about the figure in the lecture: does it need to be this complicated, and might that cross at the top be nothing but noise? If is not tuned well, soft-margin SVM does not tolerate noise enough and overfits all the same. So the handout concludes that soft-margin Gaussian SVM needs careful selection of ; Lin adds in the lecture that the polynomial kernel has even more parameters to choose.
The value of α sorts the data into three kinds
Page 15 of the handout is titled Physical Meaning of . The same two complementary slackness conditions give three cases according to the value of :
| Kind | What complementary slackness gives | Position | Mark in the figure | ||
|---|---|---|---|---|---|
| non-support vector | , so the second condition forces | outside the fat boundary, a few exactly on its edge | none | ||
| free SV | the second condition gives ; the first gives | exactly on the edge of the fat boundary | square | ||
| bounded SV | , so the first condition gives | the violation amount | violating the fat boundary, a few exactly on its edge | triangle |
A note on each of the three rows:
- Non-support vector. together with the primal constraint gives , so there is no violation. The handout writes 'away from'/on fat boundary; Lin says in the lecture that most of these points are some distance away from the fat boundary and a very few fall exactly on its edge.
- Free SV. The handout writes on fat boundary, locates . These are the points used to compute .
- Bounded SV. records exactly how much this point violates. In the lecture, Lin divides violations into two kinds: a small violation does not cross the line in the middle and causes no classification error; a large violation crosses to the other side and is a serious error. Both kinds have . Lin also mentions that a very few points exactly on the edge can have too, but in most cases seeing tells you that the point violates the fat boundary.
This also answers the guess made in the earlier example: for a point with a violation (), the second complementary slackness condition forces , so must be at the upper bound.
The handout's conclusion is that can be used for data analysis. As Lin puts it in the lecture, after running SVM you can look at the values of and sort the points you have into three kinds: which may be noisy, which are helpful, and which hardly matter. In the six-point example, the points at the upper bound when are exactly points 3 and 4, the ones that swapped places.
Fun Time: estimating E_in from the number of support vectors
Question (page 16): For a data set of size 10000, after solving SVM, assume that there are 1126 support vectors, and 1000 of those support vectors are bounded. What is the possible range of in terms of 0/1 error?
Answer: 1. The handout's answer: the bounded support vectors are the only ones that could violate the fat boundary. A violation with causes a 0/1 error; a violation with does not. In the lecture, Lin argues from the best and worst cases: in the best case the 1000 bounded SVs all happen to lie exactly on the fat boundary and there is no error at all, ; in the worst case all 1000 have crossed the boundary, .
Putting the reasoning together: non-support vectors and free SVs have , so and they are always classified correctly. The misclassified points can only be among the 1000 bounded SVs, so the upper limit is . The lower limit is 0, because every bounded SV's can be below 1. Option 2 sets the lower limit to , which amounts to assuming every bounded SV is misclassified, and also counts the 126 free SVs as possible errors. The lower limit in options 3 and 4 assumes every support vector is misclassified, which is even further off.
In the six-point example, at there are 2 bounded SVs and , right at the upper limit; at there are 4 bounded SVs, of which points 2 and 5 have and are not misclassified, so is still , below the upper limit .
Choosing C and γ: cross-validation, and the bound given by the number of support vectors
(Video 00:00–02:08 covers choosing parameters with cross-validation, 02:08–05:30 covers the relation between leave-one-out and the number of support vectors, 05:30–07:28 covers selection by the number of support vectors, 07:28–08:10 is Fun Time, and the summary follows.)
More parameters mean model selection
Page 17 of the handout shows a three-by-three grid: nine figures, all with the Gaussian kernel, each with a different . Lin explains in the lecture that the horizontal axis is different and the vertical axis is different . The nine boundaries differ a great deal: the top row bends sharply and the bottom row is close to straight lines. The handout says it is complicated even for of Gaussian SVM, and there are more combinations if other kernels or parameters are included.
How to select? The handout writes a single word: validation. Lin says in the lecture that this is the only definite answer Foundations ever gave, and the simplest and most useful tool is validation.
Selecting by cross-validation
Page 18 of the handout marks the cross-validation error of each cell on the same grid:
| Left | Middle | Right | |
|---|---|---|---|
| Top row | |||
| Middle row | |||
| Bottom row |
The smallest is in the lower-left corner, which the slide marks in orange. Lin points out in the lecture that the boundaries in the top row, the ones that look heavily overfit, do have larger .
The handout makes two points:
- is a 'non-smooth' function of and difficult to optimize directly. As Lin puts it in the lecture, each you feed in gives back one value, but you cannot optimize over and the way gradient descent would; usually all you can do is feed in a few different values and compare.
- So in practice you run -fold cross-validation on a few grid values of and pick the one with the smallest .
The handout's remark is that is a very popular criterion for soft-margin SVM.
The leave-one-out error never exceeds the fraction of support vectors
Page 19 of the handout first recalls that is with folds: each time one example is held out for validation and the rest are used for training. It then claims a result special to SVM:
The numbers Lin gives in the lecture: with 100 support vectors among 10,000 examples, the leave-one-out error cannot exceed .
The proof has three steps.
Step 1: remove a non-support vector, and the remaining is still optimal. Suppose that in the optimal solution computed from all examples, the -th has . Remove and solve the SVM with only the remaining examples. The handout claims that is still optimal for this smaller problem, and hints that the key is to ask: what if there is a better one? In the lecture, Lin finishes the proof by contradiction: if the smaller problem had a better set of values of , then adding the removed point back with would give a better solution to the original -example problem, contradicting "the original one is already optimal".
This article fills in the checks that are skipped in between. The original is feasible in the smaller problem and its objective value is unchanged, because the term with contributes nothing to the equality constraint , to the double sum, or to . Conversely, every feasible solution of the smaller problem, extended with , is a feasible solution of the larger problem with the same objective value. So if the smaller problem had a solution with a lower objective, the larger problem would too, and that is where the contradiction comes from.
Step 2: . Write for the classifier learned after removing that example. has not changed, so and the computed from a free SV have not changed either, and is the same as the original . The handout writes this as SVM: when leaving out non-SV. Lin says in the lecture that this is the optimization-side explanation of the earlier observation about hard-margin (only SV needed, in the Techniques Lecture 2 handout): removing a non-support vector does not affect the fat boundary at all. The right side of page 19 also shows the figure with the four hard-margin points, annotated motivation from hard-margin SVM: only SVs needed.
An addition in this article: this step relies on two premises the handout does not state. One is that the smaller problem has only this one optimal , or that the solver happens to return this one; the other is that is uniquely determined by a free SV. Without a free SV, is determined only up to a range, and removing a point means a few constraints fewer, so that range can only stay the same or widen, and is then not necessarily identical to . Neither the handout nor the lecture deals with these corner cases, so these three steps are the main idea of the proof and not yet a rigorous proof covering every case.
Step 3: compute the error point by point. The contribution of the -th example to is .
- Non-support vectors: . The last equality holds because a non-support vector has , does not violate the fat boundary, and is always classified correctly by .
- Support vectors: after removal may change, and the point may be classified correctly or incorrectly; all we know is .
Averaging,
The handout's summary is scaled #SV bounds leave-one-out CV error.
Numerical example: leave-one-out on the six points
(An example made up for this article, continuing with the six points, .) There are 4 support vectors, so the upper bound is . Actually remove each point in turn and re-solve with the remaining five:
| Point removed | Its kind in the full problem | Solution on the remaining five | Score on the removed point | |
|---|---|---|---|---|
| Point 1 (, ) | non-support vector | , , unchanged | , predicted | |
| Point 2 (, ) | free SV | , | , predicted | |
| Point 3 (, ) | bounded SV | , between and | between and , predicted | |
| Point 4 (, ) | bounded SV | , between and | between and , predicted | |
| Point 5 (, ) | free SV | , | , predicted | |
| Point 6 (, ) | non-support vector | , , unchanged | , predicted |
The row that removes point 2 can be checked by hand. What remains is : guess that and are free SVs, and are at the upper bound 1, and is 0. The two free SVs give and , so and . The equality constraint makes the of the two free SVs equal; call it : , , which is between 0 and 1. Then check the other three points: has ; has ; has , each consistent with its kind.
The row that removes point 3 has no free SV: the remaining data becomes separable, but the hard-margin solution needs , which exceeds the upper bound, so the two points and both sit at and is determined only up to a range. Every in the range predicts for , so is not in doubt.
What this result means. , which indeed does not exceed . Removing either of the two non-support vectors leaves the solution completely unchanged and contributes 0, in line with steps 1 and 2 of the proof. Removing each of the four support vectors does change the solution, but only two of them end up misclassified as a result, and the other two still contribute 0. The upper bound counts every support vector as "will be wrong", so it is usually looser than the actual .
Using the number of support vectors as a safety check
Page 20 of the handout relabels each cell of the same grid with its number of support vectors, nSV:
| Left | Middle | Right | |
|---|---|---|---|
| Top row | |||
| Middle row | |||
| Bottom row |
Lin points out in the lecture that the three worse examples at the top have many support vectors and the simpler ones below have relatively few. The handout makes three points:
- is likewise a non-smooth function and difficult to optimize.
- It is just an upper bound! As Lin puts it in the lecture, we do not know what the actual leave-one-out error is and can only estimate an upper limit from the number of support vectors; optimizing the upper limit may only make the upper limit as good as possible, which does not mean making the true error as good as possible.
- So its use is to rule out dangerous models: look at nSV on a few grid values and rule out the ones with especially many support vectors.
This article compares the two tables: the smallest nSV is in the right cell of the middle row (17, marked in orange on the slide), whose is , sixth smallest among the nine cells; the lower-left corner with the smallest () has nSV 21. Picking by nSV and picking by choose different cells. On the other hand, the three top-row cells with the largest nSV (38, 37, 37) are also exactly the three cells with the largest . The two tables happen to demonstrate the handout's usage: nSV is for ruling out, not for making the final decision.
The handout's conclusion is that nSV is often used as a safety check if computing is too time-consuming. Lin adds two points in the lecture: the number of support vectors is available as soon as training finishes, with no extra computation; and after ruling out the dangerous combinations, you can run cross-validation on the ones that remain to select more suitable parameters.
Fun Time: which value cannot be the leave-one-out error
Question (page 21): For a data set of size 10000, after solving SVM on some parameters, assume that there are 1126 support vectors, and 1000 of those support vectors are bounded. Which of the following cannot be with those parameters?
Answer: 4. The handout's answer is one sentence: note that the upper bound of is . It comes from . The first three options do not exceed , so the upper bound cannot rule them out; exceeds it and is impossible.
An addition in this article: this question uses the same numbers as the Fun Time "estimating E_in from the number of support vectors", yet the upper limit is different. The upper limit on is the fraction of bounded SVs, , and the upper limit on is the fraction of all support vectors, . The difference lies in the free SVs: under they sit exactly on the fat boundary and are always classified correctly; but once a free SV is removed and the model retrained, may change, and there is no guarantee it still gets that point right. So the information "1000 of those support vectors are bounded" is not needed in this question.
Summary of this lecture
Page 22 of the handout reduces each of the four sections to one line: the primal problem adds margin violations ; the dual problem upper-bounds by ; bounded and free SVs can be used for data analysis; model selection uses cross-validation, or approximately nSV. Lin says in the lecture that, going from hard-margin through the dual and the kernel to soft-margin, these four lectures are a complete introduction to the most popular form, soft-margin SVM. The handout's preview of the next lecture is other kernel models for soft binary classification.
Self-check
1. The classifier is , that is, and . Four points: labeled , labeled , labeled , and labeled . What is each point's ? Which points violate the fat boundary, and which are misclassified?
Answer: the values of for the four points are , , , in order, so the are , , , in order, summing to . The second point violates the fat boundary but is not misclassified (); the third point violates it and is misclassified (); the first point is exactly on the edge; the fourth point is outside the fat boundary.
2. Two one-dimensional examples: labeled and labeled . Solve the soft-margin dual for and for , and find , , and .
Answer: the equality constraint gives , , and the objective is , whose minimum without an upper bound is at . At , , both points are free SVs, , and , the same as the hard-margin solution. At , exceeds the upper bound and the objective is decreasing on , so , both points are bounded SVs, and . With no free SV, can only be pinned down to a range: gives , and gives . Within the range the primal objective is always , which equals the dual's .
3. Explain in your own words: which step produces the constraint ? If some , what is the inner ?
Criteria: the answer should mention that is linear in with coefficient ; setting the partial derivative with respect to to 0 gives , and is exactly . If , then because the coefficient must be negative, which pushes toward positive infinity; the inner min is , and the outer max will not choose it.
4. With , solving the SVM gives for 8 points, in order, . Which points can be used to compute ? What are the largest possible values of and ?
Answer: points 4 and 5 are free SVs () and can be used to compute . The first three points are non-support vectors and the last three are bounded SVs. Only bounded SVs can be misclassified, so . There are 5 support vectors in total, so .
5. In the two grids on pages 18 and 20 of the handout, the cell with the smallest nSV has , and the cell with the smallest has nSV 21. Someone claims: "just pick the parameters with the smallest nSV and skip cross-validation." What is wrong with this claim? How should nSV be used?
Criteria: the answer should say that is only an upper bound on , that the smallest upper bound does not mean the smallest error, and that these two tables are a counterexample; nSV is for ruling out dangerous combinations with especially many support vectors (the top row's 38, 37, 37, for example), and the rest are then selected by cross-validation. Better still if it goes on to say "a support vector's contribution may be 0 or 1, and the upper bound counts them all as 1".
Exercises and corresponding reading
Hands-on. Use LIBSVM or scikit-learn's SVC to run a grid of on a binary classification data set of your choice, recording both the 5-fold cross-validation error and . See whether the grid points with the largest also have high cross-validation error; then see whether the grid point that minimizes each is the same one. This is redoing the two tables on pages 18 and 20 of the handout yourself.
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 204u. The Fall 2026 course page also puts them in W10 (11/11). This article uses only the MOOC version of the handout.
Textbook. Both course pages mark this week as e-8.3 and e-8.4 of Learning from Data (LFD), with soft margin corresponding to e-8.4. e-Chapter 8 is an online chapter of LFD.
Homework. The questions related to this lecture in Fall 2024 HW6 (released 2024-11-18, due 12-02, 12 questions plus 1 bonus, no official solutions):
| Question | Topic | Where it connects to this lecture |
|---|---|---|
| Q2 | the smallest when all points are bounded SVs | computing takes a free SV; without one, is a range |
| Q5 | pad a constant 1 into and solve soft-margin SVM again: is the solution unchanged | the primal and dual problems |
| Q6 | one-class SVM anchored at the origin: derive the dual | the four steps of the dual derivation |
| Q10 | MNIST 3 versus 7, polynomial kernel, the number of support vectors for and | and nSV |
| Q11 | same data, Gaussian kernel, and each in , compute the margin | Gaussian SVM with different |
| Q12 | fix , randomly draw 200 examples as validation data to choose , repeat 128 times and plot the frequencies | choosing parameters by validation |
| Q13 (bonus) | derive the dual of the soft-margin SVM dual problem and compare it with the primal | the dual derivation |
The data for the programming questions is mnist.scale from the LIBSVM website. The assignment recommends LIBSVM and gives two reminders: do not let the package scale 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. For Q10 to Q12 you can cross-check the number of support vectors and the margin with two packages (LIBSVM and scikit-learn's SVC, for example; the latter is also built on LIBSVM). Before doing Q2, work through the case of self-check question 2 yourself. The guide to the whole assignment is in Techniques Homework and Final Project.
Further reading. Chapter 6 of the CS229 main notes covers support vector machines in a different notation and can be read against this lecture. Caltech Learning from Data is the English-language course for the same textbook.
Next lecture. Techniques Lecture 5: Kernel Logistic Regression starts from this lecture's and rereads soft-margin SVM as a regularized model.
Course video sources
Original videos: Motivation and Primal Problem, Dual Problem, Messages behind Soft-Margin SVM, Model Selection
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: Changed to one article per lecture. This article covers only Techniques Lecture 4 and was rewritten from the handout and the video transcripts.
References
- Techniques Lecture 4 handout, 204_handout.pdf: the main source for this article. The derivations in the four sections, the four Fun Time questions, and the numbers in the two grids on pages 18 and 20 all come from this handout.
- 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, the slide code, and the LFD chapter.
- Machine Learning, Fall 2026 course page: the corresponding week and LFD chapter.
- Fall 2024 Homework 6 (hw6_red.pdf): the content of Q2, Q5, Q6, and Q10 to Q13, and the assignment's reminders.
- LIBSVM: the SVM package the assignment recommends, and the tool for the hands-on exercise.
- LIBSVM Data: mnist.scale: the data for the HW6 programming questions.
- Learning from Data textbook website: the source of e-8.4.
- CS229 main notes, main_notes.pdf: further reading, support vector machines in Chapter 6.
- 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.
Loading...