AIML · Lecture 15 · Classification · Part 5Course homeOpen slide deck  
Artificial Intelligence and Machine Learning · Lecture 15 of 17

Classification · Part 5

A straight line cannot separate every class pattern. Map the inputs to a new feature space, then use the kernel trick to compute the inner products needed by an SVM.

24 reading sections · 3 interactive examples · Lecture 15 of 17. Use the contents to navigate. Printing retains the worked steps and lab previews.

6.4 · Nonlinear SVMWhat if no straight line separates the classes?

Part 4 learned a wide linear margin. Now one class surrounds the other. We extend the model by changing the geometry of the feature space.

6.4 · Nonlinear SVM

Map the predictors into a higher-dimensional space and fit a linear SVM there.

6.4.1 · The kernel trick

When the mapped vectors become too large, compute their inner products directly.

Open the corresponding slide ↗

6.4 · Nonlinear SVMWhy a line cannot isolate the center from the ring

Inspect the inner cluster and outer ring. The outside class surrounds the inside class.

These 300 observations form an inner cluster surrounded by a ring. The classes cannot be separated by a line in the original two input coordinates. We can add a feature that measures squared distance from the center.

Interactive example · Why a line cannot isolate the center from the ring
Initial state of the interactive example: Some class arrangements need a curved boundary
Change the controls and inspect the result. The example pauses when you move away.
Open the corresponding slide ↗

6.4 · Nonlinear SVMWhy a straight line cannot separate the ring

A line cannot put the center on one side and the whole ring on the other. The two points on opposite sides of the ring are joined by a segment that passes through the center.

Open the corresponding slide ↗

6.4 · Nonlinear SVMMap the observations to a higher-dimensional space

To separate the center from the ring, map each observation to a cup-shaped surface. Keep its two coordinates and add a third for height.

xi↦ψ(xi)(xi1,xi2)↦(xi1,xi2,xi12+xi22).
xᵢ = observation ixᵢ₁, xᵢ₂ = its two coordinatesψ = feature map
Open the corresponding slide ↗

6.4 · Nonlinear SVMSee what the new height measures

The center stays at height zero. A ring point gets a positive height. The farther a point lies from the center, the higher it lands on the surface.

Open the corresponding slide ↗

6.4 · Nonlinear SVMRotate the lifted points and inspect the separating plane

A gold diamond marks the selected point. Drag to rotate, or toggle the plane to see the gap between the two height ranges.

The chart preserves each original point and adds x₁² + x₂² as its height. The displayed plane is placed in the gap between the observed height ranges. A linear SVM in the lifted space can learn a separator, which appears as a nonlinear boundary in the input plane.

Interactive example · Rotate the lifted points and inspect the separating plane
Initial state of the interactive example: Rotate the original data in the lifted space
Change the controls and inspect the result. The example pauses when you move away.
Open the corresponding slide ↗

6.4 · Nonlinear SVMA horizontal plane separates the lifted points

The inner points sit below the plane and the outer points sit above it. In the original two-dimensional view, the points at the plane’s height trace a circle.

Open the corresponding slide ↗

6.4 · Nonlinear SVMA feature map can make a linear separator possible

Circular data

Height z Height cut

All blue points are below the cut. All orange points are above it.

When the heights overlap

Height z Height cut

The height ranges overlap. No single height cut separates these classes.

The added plane in the demonstration is a visual example. Fit a linear SVM in the mapped space to choose the weights and bias by the margin objective.

Open the corresponding slide ↗

6.4 · Nonlinear SVMRecap · How linear SVM learns the weights

We choose the weights and bias that give the widest margin. Minimizing the squared weight norm does this while the constraints keep every training point on or beyond its own margin edge.

𝐰^,w0^=arg min𝐰,w0∈ℝm+112‖𝐰‖2subject to yi(∑j=1mwjxij+w0)≥1∀i=1,…,n.
xᵢⱼ = original feature j of observation im = number of original features𝐰 = feature weights, w₀ = biasyᵢ = signed class label, n = number of observations
Open the corresponding slide ↗

6.4 · Nonlinear SVMFit a linear SVM in the mapped space

Feature mapψ:ℝm→ℝd
Input
m original features
Output
d mapped features

The objective stays the same. In the margin constraint, replace xᵢⱼ with ψⱼ(xᵢ).

𝐰^,w0^=arg min𝐰,w0∈ℝd+112‖𝐰‖2subject to yi(∑j=1dwjψj(xi)+w0)≥1∀i=1,…,n.

In the mapped space, 𝐰∈ℝd. The +1 counts the bias w₀. The feature map itself has m inputs and d outputs. If we fold the bias into the sum, it multiplies the fixed coordinate ψ0(xi)=1.

ψⱼ(xᵢ) = mapped feature j of observation im = number of original features, d = number of mapped features𝐰 = feature weights, w₀ = biasyᵢ = signed class label, n = number of observations
Open the corresponding slide ↗

6.4.1 · The kernel trickThe Kernel Trick

Watch on YouTube
Open the corresponding slide ↗

6.4.1 · The kernel trickWhy do we need the Kernel Trick?

Suppose we only need the dot product of two mapped points. Try this quadratic map, which uses the two squares and their scaled product.

ψ(xi)=(xi12, 2xi1xi2, xi22)

Original pair

xi=(1,2) xi′=(3,4)

Map to three coordinates, then take the dot product

ψ(xi)=(1,22,4),ψ(xi′)=(9,122,16)
ψ(xi)⊤ψ(xi′)=1·9+(22)(122)+4·16=9+48+64

Take the original dot product, then square it

κ(xi,xi′)=(xi⊤xi′)2=(1·3+2·4)2=112

Same dot product

121
With 100 original features, this quadratic map would produce 5,050 coordinates. The kernel still uses the original 100 per point.
Open the corresponding slide ↗

6.4.1 · The kernel trickA polynomial map can create too many features

A real dataset may need a much larger feature space. Consider a full degree-10 polynomial map for observations with 100 features. Powers and products quickly make the mapped vector too large to build.

(xi1,…,xi100)↦(xi1,…,xi100,xi12,xi1xi2,…,xi99xi100,xi1002,xi13,xi12xi2,xi1xi2xi3,……)
xᵢⱼ = feature j of observation i
Open the corresponding slide ↗

6.4.1 · The kernel trickThe Lagrange Multipliers Visualized

Watch on YouTube
Open the corresponding slide ↗

6.4.1 · The kernel trickWhat are Lagrange multipliers?

Give each training point one nonnegative parameter, called a Lagrange multiplier. Its value weights that point’s margin constraint.

𝒂=(a1,…,an)ai≥0for everyi

From multipliers to a separator

In the dual problem, we solve for 𝒂 first. The optimal multipliers let us recover the weights 𝐰 and the bias w0 that solve the original SVM problem, called the primal.

Why subtract each constraint term?

The margin rule is yisi−1≥0. A point that falls short has a negative value inside the parentheses. Subtracting ai(yisi−1) then raises the Lagrangian when ai>0.

yi = class label of training point isi = score of training point in = number of training points
Open the corresponding slide ↗

6.4.1 · The kernel trickWrite the SVM problem using Lagrange multipliers

Use one multiplier per training point to bring the margin constraints into the objective.

𝒂^=arg max𝒂∈ℝ≥0nmin𝐰,w0∈ℝd+1ℒ(𝐰,w0,𝒂)=arg max𝒂∈ℝ≥0nmin𝐰,w0∈ℝd+1[12‖𝐰‖2−∑i=1nai(yisi−1)]=arg max𝒂∈ℝ≥0nmin𝐰,w0∈ℝd+1[12‖𝐰‖2−∑i=1nai(yi(∑j=1dwjψj(xi)+w0)−1)]

Here si=∑j=1dwjψj(xi)+w0 is the score of training point i.

The weight term favors a wider margin. The sum adds a cost when a point falls short of its margin edge and its multiplier is positive.

Try the sum with one point

Signed score y₁s₁(+1) × 0.5 = 0.5
Second term of ℒ−1 × (0.5 − 1) = +0.5
𝒂 = vector of nonnegative multipliers, aᵢ = multiplier for point iℒ = Lagrangian𝐰 = feature weights, w₀ = biasψⱼ(xᵢ) = mapped feature j, d = number of mapped featuresyᵢ = signed class label, n = number of observationssi = score of training point i
Open the corresponding slide ↗

6.4.1 · The kernel trickPrimal and dual solve the same SVM

Both formulations give the same widest separating strip for this hard-margin SVM. They search using different variables.

Primal · Move the boundary

Choose 𝐰 and w₀ → Get a candidate separator

Choose how the line tilts and where it sits. Find the widest margin while keeping every point on the correct side of its margin edge.

Dual · Weight the training points

Choose a₁, …, aₙ → Build 𝐰 → Recover w₀

Give each training point a multiplier. The weighted, signed points add up to the normal. Points with multiplier zero contribute nothing. Use the margin points to locate the line.

The next step explains where the weighted sum and the condition on the multipliers come from.
Open the corresponding slide ↗

6.4.1 · The kernel trickDifferentiate the Lagrangian, then set the derivatives to zero

Differentiate ℒ with respect to the weights and the bias.

∂ℒ∂𝐰=𝐰−∑i=1naiyiψ(xi)∂ℒ∂w0=−∑i=1naiyi.

Set both derivatives to zero to obtain the weight vector and the balance condition.

𝐰=∑i=1naiyiψ(xi)0=∑i=1naiyi.
𝐰 = feature weights, w₀ = biasaᵢ = Lagrange multiplieryᵢ = signed class label, ψ(xᵢ) = mapped observationn = number of observations
Open the corresponding slide ↗

6.4.1 · The kernel trickThe dual chooses one multiplier per training point

1. Expand the Lagrangian. Start with the constrained-margin expression, then distribute the minus sign and collect terms.
ℒ(𝐰,w0,𝒂)=12‖𝐰‖2−∑iai[yi(𝐰⊤ψ(xi)+w0)−1]=12‖𝐰‖2−𝐰⊤∑iaiyiψ(xi)−w0∑iaiyi+∑iai
2. Use the results from the previous slide. Substitute 𝐰=∑iaiyiψ(xi) into the second term to get −𝐰⊤𝐰. The bias term is zero because ∑iaiyi=0.
ℒ=(12−1)‖𝐰‖2+∑iai=∑iai−12‖∑iaiyiψ(xi)‖2

3. Expand the squared norm. Take the inner product of the weighted sum with itself. This gives one term per pair of mapped points. Maximize the result over the allowed multipliers.

𝒂^=arg max𝒂∈ℝ≥0n∑i=1nai−12∑i=1n∑i′=1naiai′yiyi′ψ(xi)⊤ψ(xi′)subject to ai≥0∀i=1,…,n ∑i=1naiyi=0,
𝒂 = vector of Lagrange multipliersi and i′ = indices of training observationsyᵢ = signed class labelψ(xᵢ) = mapped observationn = number of observationsThe kernel trick replaces the highlighted inner product with κ(xi,xi′), evaluated on the original inputs.
Open the corresponding slide ↗

6.4.1 · The kernel trickUnderstanding the SVM Dual Formulation Geometrically

Follow three points with the identity feature map, from their original coordinates to the maximum-margin separator.

Open the corresponding slide ↗

6.4.1 · The kernel trickThe Kernel Trick replaces the mapped inner product

Lifting the points made it possible to separate the ring from its center. To use that geometry, the dual needs the dot product of each pair of lifted points.

ψ(xi)⊤ψ(xi′)=κ(xi,xi′)

For the feature map from the ring example, compute each term using the two original coordinates of each point.

κ(xi,xi′)=xi1xi′1+xi2xi′2⏟Original-coordinate dot product+(xi12+xi22)(xi′12+xi′22)⏟Product of the two heights

Original pair

xi=(1,0) xi′=(0,2)

Map both points, then take their dot product

Reuse the ring example’s feature map. Keep both coordinates and add the height.

(1,0,1)⊤(0,2,4)=1·0+0·2+1·4

Evaluate the kernel on the original pair

Dot product of the original coordinates 1·0+0·2=0
Product of the two heights (12+02)⏟1·(02+22)⏟4=4
κ((1,0),(0,2))=0+1·4=4

Same value

4 for the SVM dual
ψ maps a point into the lifted space. κ computes the corresponding dot product from the original inputs. For a large feature map, a kernel that is cheap to evaluate can save work.
Open the corresponding slide ↗

6.4.1 · The kernel trickThe quadratic kernel saves work

For a point with m original features, the quadratic map includes every squared feature and every scaled product of two distinct features.

Quadratic feature map

xi↦(xi12, 2xi1xi2, …, 2xi1xim,xi22, 2xi2xi3, …,…, 2xi,m−1xim, xim2)

Build m(m+1)2 coordinates per point, then take the dot product of the expanded vectors.

Equivalent quadratic kernel

κ(xi,xi′)=(xi⊤xi′)2

Take the dot product of the original inputs and square it. That gives the same result in 2m operations per pair, using the original m coordinates.

Optional derivation · Not required for study

The two √2 factors give 2 for every distinct feature pair. Grouping those terms gives the square of the original dot product.

ψ(xi)⊤ψ(xi′)=∑j=1mxij2xi′j2+∑j=1m∑k>j(2xijxik)(2xi′jxi′k)=∑j=1mxij2xi′j2+2∑j=1m∑k>jxijxi′jxikxi′k=(xi⊤xi′)2=κ(xi,xi′)
Gaussian kernel
κ(xi,xi′)=exp(−γ‖xi−xi′‖2)
It computes a dot product in an infinite-dimensional feature space from the original points’ squared distance. The parameter γ > 0 controls how quickly the value falls with distance.
Open the corresponding slide ↗

Check your understandingFour questions about nonlinear SVMs

  1. The inner class reaches radius 1.2. The ring starts at radius 1.5. Choose a height threshold z = t for z = x₁² + x₂² and justify it.
  2. Two training points have labels −1 and +1. Their dual multipliers are 0.4 and 0.6, respectively. If the first stays fixed, what must the second be to satisfy the bias condition?
  3. The feature map gives ψ(x) = (2, 1) and ψ(z) = (−1, 3). What must a kernel matching this map return for κ(x, z)?
  4. Where does the kernel enter the dual? Which parts of the optimization stay the same?
Open the corresponding slide ↗

Check your understandingWork through the four answers

  1. The possible threshold range is 1.44 < t < 2.25. For example, t = 2 puts the highest inner point below the plane and the lowest ring point above it.
  2. The bias condition is −0.4 + a₂ = 0, so a₂ = 0.4.
  3. Compute the inner product of the mapped coordinates. κ(x, z) = 2 × (−1) + 1 × 3 = 1.
  4. Replace each mapped inner product with the kernel value. The multipliers, labels and constraints stay the same.
Open the corresponding slide ↗

6.4.1 · The kernel trickFrom a feature map to the kernel trick

Map the features

A linear separator in the mapped space can describe a nonlinear boundary in the original inputs.

Learn the dual

Use one multiplier per training observation. The data appear through mapped inner products.

Evaluate a kernel

Compute each mapped inner product directly from the original inputs.

Open the corresponding slide ↗

Further readingBishop on classification

Read chapters 4 and 7 for linear discriminants, perceptron learning, and support vector machines.

Open the corresponding slide ↗

Lecture recapWhat to take away

  • Explain how a feature map can separate the center from a surrounding ring.
  • Follow the mapped objective, Lagrangian and dual using the same notation.
  • Use a kernel to compute the mapped inner products in the dual.
Open the corresponding slide ↗
Continue learning