Skip to content

Hsuan-Tien Lin's ML Techniques T3–T4: Kernel Trick and Soft-Margin SVM — Computing an Infinite-Dimensional Classifier and Keeping It from Overfitting

Sep 30, 20261 min
TL;DRLecture 3 of Machine Learning Techniques merges "feature transform + inner product" into a single kernel function K(x, x′). Training and prediction in the dual SVM only need K, so d̃ can be infinite: the Gaussian kernel corresponds to an infinite-dimensional transform. Lecture 4 admits the SVM can still overfit and introduces violations ξₙ and a parameter C, giving the soft-margin SVM. Its dual differs from the hard-margin one in exactly one way: αₙ gets an upper bound C. The value of αₙ sorts the data into non-SVs, free SVs, and bounded SVs, and the fraction #SV/N upper-bounds the leave-one-out error, a cheap way to rule out dangerous (C, γ).

🌏 中文版

This is part 10 of Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. It covers Lecture 3, Kernel Support Vector Machine, and Lecture 4, Soft-Margin Support Vector Machine, of Machine Learning Techniques.

Sources: the MOOC slides 203_handout.pdf and 204_handout.pdf, videos 10–17 of the Techniques YouTube playlist, the Fall 2024 and Fall 2026 course pages, and Fall 2024 HW6, all opened and checked on 2026-09-30. The textbook sections are LFD e-8.3 (kernels) and e-8.4 (soft margin), as listed on both course pages; I did not open the chapter itself.

Access level: the MOOC materials are A2. Adding the public Fall 2024 HW6 PDF, LIBSVM, and the MNIST data brings these two lectures to A3 (minus grading): there are no official solutions, and Gradescope grading is for enrolled students only.

Where the last post stopped

The previous post turned the hard-margin SVM into a dual problem with N variables and N + 1 constraints, which looks independent of the transformed dimension d̃. But the dual's quadratic coefficients q_{n,m} = yₙyₘzₙᵀzₘ are still d̃-dimensional inner products, O(d̃) each if computed naively. T3 solves that. T4 solves a different problem: even with a fat boundary, the hard-margin SVM can still overfit.

T3: Kernel SVM

Transform and inner product in one step

The slides demonstrate with the second-order polynomial transform Φ₂(x). For simplicity it includes both x₁x₂ and x₂x₁. Expanding Φ₂(x)ᵀΦ₂(x′) and regrouping gives:

Φ₂(x)ᵀΦ₂(x′) = 1 + xᵀx′ + (xᵀx′)²

The right-hand side needs one d-dimensional inner product, so it takes O(d) time, with no need to expand into O(d²) dimensions first. This shortcut for "transform + inner product" is called a kernel function, written K_Φ(x, x′) ≡ Φ(x)ᵀΦ(x′).

Every place the dual SVM uses z can switch to K:

  • Quadratic coefficients: q_{n,m} = yₙyₘK(xₙ, xₘ)
  • Intercept: from any support vector (x_s, y_s), b = y_s − Σ αₙyₙK(xₙ, x_s)
  • Prediction: g_SVM(x) = sign(Σ αₙyₙK(xₙ, x) + b)

The Kernel Hard-Margin SVM algorithm on the slides has four steps. Building Q takes O(N²) kernel evaluations, then you solve a QP with N variables, and computing b and predicting take only O(#SV) kernel evaluations. Neither training nor prediction ever touches w itself, so nothing depends on d̃.

Polynomial kernels

Putting different coefficients in front of the terms of the same quadratic transform gives different kernels. The slides generalize to:

K_Q(x, x′) = (ζ + γ xᵀx′)^Q,  γ > 0, ζ ≥ 0

These quadratic kernels have equal power, but a different inner product means different geometry, and the margin changes meaning with it. The slides show three plots with γ = 0.001, 1, and 1000: the boundaries differ, and so do the support vectors, and it is hard to say in advance which is better. Changing the kernel changes the definition of the margin, so choosing a kernel is model selection, just like choosing Φ.

With Q = 1, ζ = 0, γ = 1 you get the plain inner product, the linear kernel. Lin repeats his usual advice: linear first. Linear problems can often be solved efficiently in the primal.

The Gaussian kernel: an infinite-dimensional transform

If all you need is a computable K, can Φ be infinite-dimensional? The slides use the 1D kernel K(x, x′) = exp(−(x − x′)²): split it into three exponentials, expand exp(2xx′) as a Taylor series, and it becomes an inner product of two infinite-dimensional vectors. The general form is:

K(x, x′) = exp(−γ‖x − x′‖²),  γ > 0

This is the Gaussian kernel, also called the RBF kernel. Plugged into g_SVM, the predictor is a linear combination of Gaussians centered at the support vectors. The slide describes it as linear classification in an infinite-dimensional space, with generalization guarded by the large margin.

With a large γ, though, the Gaussians get sharp. In the three plots on the slides (γ = 1, 10, 100), the γ = 100 boundary wraps tightly around each point. The caption reads: warning: SVM can still overfit :-(.

Trade-offs among the three kernels

kernelconspros
linearrestricted; data may not be separablesafe, fast (special primal solvers), w and support vectors are explainable
polynomialnumerical trouble for large Q (a base below 1 goes to 0, above 1 blows up); three parameters (γ, ζ, Q) are hard to selectmore flexible than linear; Q directly controls the degree
Gaussianno w to inspect; slower than linear; possibly too powerfulmost powerful; bounded values, so fewer numerical problems than polynomial; only one parameter

The slides conclude: linear for efficiency, Gaussian for power, and polynomial perhaps only for small Q.

Designing your own kernel

A kernel is a special kind of similarity, but not every similarity is a kernel. The necessary and sufficient condition is Mercer's condition: K is symmetric, and the matrix K built from any data set (entries kᵢⱼ = K(xᵢ, xⱼ)) is always positive semi-definite, because it can be written as ZZᵀ. The slide's verdict: defining your own kernel is possible, but hard.

Try this: work the last Fun Time question on the slides. Use the two points x₁ = (1) and x₂ = (−1) to write out the 2×2 matrix for each of the four candidate kernels and see which one is not positive semi-definite. This is the cheapest way to sanity-check a custom kernel.

T4: Soft-Margin SVM

The problem with hard margins

The SVM overfits partly because Φ is powerful and partly because it insists on separating the data perfectly. Insisting on separability means being able to shatter, and that gives it the power to fit noise.

The slides start by combining two older methods. The pocket algorithm tolerates mistakes and minimizes their count. The hard-margin SVM wants a large margin but tolerates no mistakes. Combined: minimize ½wᵀw plus C times the number of mistakes, where correctly classified points still satisfy the margin constraint and misclassified points are left alone. C controls the trade-off between a large margin and noise tolerance.

That version has two problems. The mistake count is nonlinear, so it is no longer a QP. And it cannot tell "slightly off" from "badly wrong". The fix is to record each point's margin violation ξₙ, how far it crosses the fat boundary, and penalize the total violation instead of the number of mistakes:

min_{b,w,ξ}  ½ wᵀw + C Σₙ ξₙ
subject to   yₙ(wᵀzₙ + b) ≥ 1 − ξₙ,  ξₙ ≥ 0

A large C means less tolerance for violations; a small C means you would rather have a fatter boundary. This is a QP with d̃ + 1 + N variables and 2N constraints.

The dual: αₙ gets an upper bound C

The derivation is nearly identical to T2. This time there are two sets of multipliers: αₙ for the margin constraints and βₙ for ξₙ ≥ 0. Setting the derivative with respect to ξₙ to zero gives C − αₙ − βₙ = 0, so βₙ can be replaced by C − αₙ, which also yields 0 ≤ αₙ ≤ C; ξₙ drops out along the way. What remains is exactly the hard-margin inner problem. The result:

min_α  ½ Σₙ Σₘ αₙαₘ yₙyₘ K(xₙ, xₘ) − Σₙ αₙ
subject to  Σₙ yₙαₙ = 0;  0 ≤ αₙ ≤ C

The only difference from the hard-margin dual is the upper bound C on αₙ. N variables, 2N + 1 constraints.

Computing b

With hard margins, any support vector gives you b. With soft margins there are two complementary slackness conditions:

  • αₙ(1 − ξₙ − yₙ(wᵀzₙ + b)) = 0
  • (C − αₙ)ξₙ = 0

So you need a free SV (0 < α_s < C). The second condition forces ξ_s = 0, and the first then gives b = y_s − Σ αₙyₙK(xₙ, x_s). If there is no free SV at all, b is only pinned down to a range. Fall 2024 HW6 Q2 tests exactly this no-free-SV case.

The physical meaning of αₙ

The same two conditions sort every training point into three groups:

groupαₙξₙlocation
non-SV00outside the fat boundary, or exactly on it
free SV0 < αₙ < C0exactly on the fat boundary; used to compute b
bounded SVCthe violationviolating the fat boundary, or exactly on it

The slides say αₙ can be used for data analysis: bounded SVs are points that may be misclassified or sit too close to the boundary. A 0/1 error only happens when ξₙ ≥ 1, so the fraction of bounded SVs upper-bounds E_in.

Model selection

A Gaussian soft-margin SVM has at least two parameters, (C, γ), and the three plots on the slides (C = 1, 10, 100) show that a large C overfits too. How do you choose? The answer is validation from Foundations L15; see part 8.

  • Cross validation: E_cv(C, γ) is not smooth in the parameters and is hard to optimize directly, so in practice you run V-fold CV on a few grid values. This is the most popular criterion.
  • #SV as a bound: the slides claim E_loocv ≤ #SV / N. The reason: leaving out a non-SV leaves the remaining α optimal, so the leave-one-out model equals the original and that point's error is 0; a support vector's leave-one-out error is at most 1. It is only an upper bound, but it is cheap and can rule out dangerous parameter settings. The slides suggest using #SV as a safety check when computing E_cv takes too long.

Try this: with LIBSVM or scikit-learn's SVC, run a 3×3 grid over (C, γ) on any binary classification data set and record both the 5-fold CV error and #SV/N. Check whether the grid points with the largest #SV/N also have high CV error.

Videos and slides

SectionVideoSlides
Kernel TrickT3-1203
Polynomial KernelT3-2203
Gaussian KernelT3-3203
Comparison of KernelsT3-4203
Motivation and PrimalT4-1204
Dual ProblemT4-2204
MessagesT4-3204
Model SelectionT4-4204

How the two semesters schedule it

  • Fall 2024: W10 (11/04) covers kernel SVM and soft-margin SVM in the same week, with slides 203u and 204u.
  • Fall 2026: scheduled for W10 (11/11), with LFD e-8.3 and e-8.4 listed. The 203u_handout.pdf link returned 404 on 2026-09-30, so this post uses the MOOC version only.

Practice: Fall 2024 HW6

HW6 was released 2024-11-18 and due 12-02. It has 12 problems plus 1 bonus; Q1–4 are auto-graded and the rest are graded by TAs. The problems related to this post:

ProblemTopicMaps to
Q1Apply the kernel trick to PLA: maintain α instead of wT3 kernel trick; T2 "w represented by data"
Q2When every point is a bounded SV, what is the smallest b*?T4 computing b
Q5Pad the constant 1 into xₙ and solve the soft-margin SVM: is the solution unchanged?T1 pulling out b; T4 primal and dual
Q6A one-class SVM anchored at the origin: derive the dualT4 dual derivation
Q7For large enough γ, the Gaussian-kernel ĥ achieves E_in = 0T3 Gaussian kernel
Q8Prove exp(2cos(x − x′) − 2) is a valid kernelT3 Mercer's condition and the Gaussian kernel
Q10MNIST 3 vs 7, polynomial kernel: #SV for C ∈ {0.1, 1, 10} and Q ∈ {2, 3, 4}T3 polynomial kernel; T4
Q11Same data, Gaussian kernel with C and γ each in {0.1, 1, 10}: compute the margin 1/‖w‖T3, T4
Q12Fix C = 1, randomly hold out 200 examples for validation to choose γ, repeat 128 times, plot the selection frequencyT4 model selection

The data for the programming problems is mnist.scale from the LIBSVM site. The handout recommends LIBSVM and adds two warnings: do not let the package scale the data automatically, since that changes the effective kernel; and check for yourself that the package solves the dual formulation taught in class with enough numerical precision.

There are no official solutions. For Q10–Q12, cross-check #SV and the margin with two different packages (for example LIBSVM and scikit-learn's SVC, which is itself built on LIBSVM), and only write conclusions once the numbers agree.

Further reading

Series navigation: previous, Linear SVM and dual SVM | next, Kernel logistic regression and support vector regression | series overview

References