Numerical Solution
Everything so far has been a derivation. Section 12.5 asks the practical question: given all these equivalent formulations, which one do you hand to a solver, and what does it cost?
The hinge has one kink, and Equation 12.54 covers it
Section titled “The hinge has one kink, and Equation 12.54 covers it”Equation 12.31 is convex and unconstrained, so gradient descent is the obvious method — except the hinge is not differentiable at . The fix is a subgradient:
A number is a subgradient of at when for every — geometrically, when the line through with slope stays below the loss everywhere. At the kink a whole interval of slopes qualifies:
import numpy as np
ts = np.linspace(-6, 6, 240001)
lt = np.maximum(0.0, 1 - ts) # the hinge loss
print(f"{'candidate g':>13} {'valid at t0 = 1?':>18} {'worst violation':>17}")
for g in (-1.5, -1.0, -0.7, -0.5, -0.25, 0.0, 0.25):
viol = float(np.max(0.0 + g * (ts - 1.0) - lt)) # l(t0) = 0 at t0 = 1
print(f"{g:>13.2f} {str(viol <= 1e-12):>18} {max(viol, 0.0):>17.3e}") candidate g valid at t0 = 1? worst violation
-1.50 False 3.500e+00
-1.00 True 0.000e+00
-0.70 True 0.000e+00
-0.50 True 0.000e+00
-0.25 True 0.000e+00
0.00 True 0.000e+00
0.25 False 1.250e+00Everything in qualifies and nothing outside it does. Away from the kink the subgradient is unique — scanning candidate slopes at , , and leaves exactly one valid in each case. That is why Equation 12.54 gives a set at one point and a number everywhere else.
What a subgradient costs
Section titled “What a subgradient costs”def subgrad(XX, YY, C, iters, eta0=0.5, seed=0):
d = XX.shape[1]
rg = np.random.default_rng(seed)
w, b = rg.normal(0, 0.3, d), float(rg.normal(0, 0.3))
best = obj(w, b, XX, YY, C)
for k in range(1, iters + 1):
t = YY * (XX @ w + b)
act = t < 1.0 # 12.54: g = -1 exactly here
gw = w - C * (YY[act] @ XX[act]) if act.any() else w.copy()
gb = -C * YY[act].sum() if act.any() else 0.0
eta = eta0 / k
w, b = w - eta * gw, b - eta * gb
best = min(best, obj(w, b, XX, YY, C)) # note: BEST, not last
return best iterations best objective gap to the QP
10 3.9235861529 4.859e-02
100 3.8882244562 1.322e-02
1000 3.8791122429 4.112e-03
10000 3.8762946193 1.295e-03
100000 3.8754091186 4.091e-04A hundred thousand iterations to reach four decimal places. Each factor of ten in iterations buys roughly a factor of three in accuracy — the rate that is characteristic of subgradient methods, against the linear or quadratic convergence a smooth problem would allow.
Note the best rather than the last iterate. A subgradient step can increase the objective, so
the running minimum is what converges; tracking only the current point gives a sequence that wobbles.
Both formulations as standard-form quadratic programs
Section titled “Both formulations as standard-form quadratic programs”The book writes each SVM in the standard form of §7.3.2, subject to .
The primal, with variables :
The dual, with variables :
Both built and solved on the nine-point example, against the reference optimum :
| form | variables | constraint rows | solved value | gap |
|---|---|---|---|---|
| primal, 12.56 | ||||
| dual, 12.57 |
Which formulation is smaller?
Section titled “Which formulation is smaller?”The primal’s size grows with , the dual’s does not:
| setting | primal variables | dual variables | ||
|---|---|---|---|---|
| the running example | ||||
| digits, vs | ||||
| MNIST, two classes | ||||
| text, bag of words | ||||
| genomics | ||||
| with an RBF kernel | infinite |
The last row is the one that matters. An RBF kernel’s feature space has no finite dimension, so the primal has no finite variable count at all — the dual is not merely smaller there, it is the only one of the two that exists. That is the practical payoff of page 1206’s derivation, and page 1208’s rank measurement is the same fact seen from another angle.
“Not often used in practice”
Section titled ““Not often used in practice””The section ends with an admission:
The approach presented here, expressing the SVM problem in standard convex optimization form, is not often used in practice.
It is worth knowing how large “not often” is. Timing the generic SLSQP solve of Equation 12.57 against LIBSVM — the solver the book cites as one of the two main implementations — on the same data:
N = 200 : LIBSVM 2.8 ms, generic SLSQP 249.3 ms, ratio 89.2x
N = 800 : LIBSVM 2.4 ms, generic SLSQP 29415.5 ms, ratio 12349.1x
N = 2000 : LIBSVM 10.2 ms, generic SLSQP 504152.8 ms, ratio 49548.2xEight minutes against ten milliseconds at , and the gap widens with every doubling. The generic solver treats as an arbitrary dense quadratic form and maintains an approximation to its inverse; a specialised SVM solver exploits three things the generic one cannot know:
| what a specialised solver uses | where it comes from |
|---|---|
| the constraints are box constraints plus one equality | Equation 12.41 |
| the solution is sparse — most are zero | page 1206 |
| sub-problems of size have a closed form | the structure of the box |
Sequential minimal optimisation updates two multipliers at a time, keeps satisfied by construction, and never forms an inverse.
-
pch.quizShowAnswer
B — That the line through (1, 0) with slope g stays below the loss for every t — measured, every g in [-1, 0] satisfies this and nothing outside it does
-
pch.quizShowAnswer
C — Because a subgradient is not guaranteed to be a descent direction, so the method converges at roughly one over the square root of the iteration count rather than linearly
-
pch.quizShowAnswer
B — Because the standard form accepts only inequalities, so Equation 12.58 writes the single equality y'alpha = 0 twice, once in each direction
-
pch.quizShowAnswer
C — Infinite — the RBF feature space has no finite dimension, so the primal does not exist as a finite program and the dual is the only usable formulation
Exercises
Section titled “Exercises”Exercise 1 – Test which slopes are subgradients
Section titled “Exercise 1 – Test which slopes are subgradients”Exercise 2 – Run subgradient descent and watch the rate
Section titled “Exercise 2 – Run subgradient descent and watch the rate”Exercise 3 – Build Equation 12.57’s constraint matrix
Section titled “Exercise 3 – Build Equation 12.57’s constraint matrix”Recall card
Section titled “Recall card”- Equation 12.31 is convex and unconstrained but not differentiable, because the hinge has a kink at t equal to one.
- A subgradient is any slope whose supporting line stays below the loss everywhere. At the kink that is the whole interval from minus one to zero — measured, every value in it works and nothing outside it does.
- Away from the kink the subgradient is unique and equals the ordinary derivative, which is why Equation 12.54 lists a set at one point and a number elsewhere.
- Subgradient descent converges at about one over the square root of the iteration count. A hundred thousand steps reached four decimal places; each factor of ten in iterations bought about a factor of three.
- A subgradient step can increase the objective, so the running minimum is what converges, not the current iterate.
- The primal quadratic program has D plus one plus N variables, with the identity block for w and zeros for b and the slacks — which is the matrix form of b being unregularised.
- The dual has N variables and no dependence on D at all.
- Equation 12.58 writes each equality as two inequalities, which is why the dual’s constraint matrix has two rows more than twice N. Real solvers accept equalities directly.
- Both standard forms were built and solved, agreeing with the reference optimum to 7.1e-15 and 2.1e-14.
- With an RBF kernel the primal has infinitely many variables and the dual has N. The dual is not merely smaller there; it is the only one of the two that exists.
- The book’s aside that this approach is ‘not often used in practice’ is an understatement. A generic solver took 504 seconds where LIBSVM took 10 milliseconds at N equal to 2000 — a factor of 49,548, and growing with N.
- What the specialised solvers exploit is structure the generic one cannot see: box constraints plus one equality, a sparse solution, and two-variable sub-problems with a closed form.
Next: Chapter 12 Worked Problems — problems built from the chapter’s own claims, this module’s own.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading