The SVM Dual & Support Vectors
KKT conditions, sparsity of the solution and SMO.
The dual turns the SVM into a quadratic program with one variable per training point, a box and a single equality. Its optimality conditions sort every point into one of three groups, its solution is sparse, it is where kernels enter, and it is solved two variables at a time by SMO. It also gives one of the oldest generalization bounds in machine learning: the leave-one-out error is at most the fraction of support vectors.
What you’ll learn
- Derive the soft-margin dual from the Lagrangian, prove strong duality, and recover and from .
- State the KKT conditions and classify each training point by whether , or .
- Derive SMO's analytic two-variable update with clipping, and its maximal-violating-pair selection and stopping rule.
- Prove the leave-one-out bound .
- Explain the computational limits of kernel SVMs and how linear SVMs avoid them.
From primal to dual
Labels are . Start from the soft-margin primal with a feature map (the identity for a linear SVM), and attach multipliers to the margin constraints and to :
Set the derivatives with respect to the primal variables to zero:
becomes . Substituting, the terms cancel, the term vanishes, and with what remains is a function of alone:
In matrix form, with , that is for , which is positive semidefinite whenever the Gram matrix is: a concave maximization over a polytope.
Strong duality. The primal is convex with affine constraints and is always feasible (, , ), so Slater's condition in its refined form for affine constraints holds and the optimal values agree (Lagrange Duality & KKT Conditions). The primal optimum is , unique because the objective is strictly convex in ; the optimal need not be unique.
The decision function never needs explicitly:
Training needs the matrix (or its entries on demand), prediction needs one kernel evaluation per support vector, and never appears. That is the precise sense of the kernel trick for SVMs.
KKT conditions: three kinds of points
At an optimum, complementary slackness gives and . Combining them with feasibility sorts every training point:
| Multiplier | Slack | Margin | Where the point is |
|---|---|---|---|
| on or outside its margin hyperplane: not a support vector | |||
| exactly on the margin: a free (unbounded) support vector | |||
| on or inside the margin, or misclassified: a bounded support vector |
Recovering . Every free support vector satisfies , so . Solvers average this over all free support vectors for numerical stability. If there are none, the KKT conditions only confine to an interval, and LIBSVM takes its midpoint.
Sparsity, and stability of the solution. A non-support vector can be moved anywhere that keeps : the old still satisfies every KKT condition, so it stays optimal and does not change at all. A quirk follows for linear kernels in the plane: when three free support vectors pin down through their three equations , moving a bounded support vector inside the margin is often absorbed by the free 's, leaving the boundary in place until some multiplier reaches or .
At the optimum, a training point has . What can you conclude?
- It is misclassified
- It lies exactly on the margin
- : it is on or inside the margin, possibly misclassified, with slack
- It has no influence on the solution
Show the answer
C. : it is on or inside the margin, possibly misclassified, with slack
With the constraint is tight, , and . Whether it is misclassified depends on whether ; a bounded support vector can also sit inside the street on its correct side, or (degenerately) exactly on the margin with .
SMO: two variables at a time
General-purpose quadratic programming solvers need the dense matrix and roughly cubic time. Sequential minimal optimization (Platt, 1998) instead updates the smallest possible set of variables, two, and does so in closed form. Two is the minimum: changing a single would violate .
Write the dual as the minimization of , and let be the current prediction error on point .
Move along the direction that keeps fixed: and . Using and , the objective along this line is a one-dimensional quadratic:
For its minimizer gives Platt's update (the bias cancels in ):
Clip to the segment of the line inside the box , then restore the equality constraint:
Each step lowers (or leaves it unchanged), costs two kernel columns to update every , and never needs in full. When , two identical feature vectors, the objective is linear along the line and the minimum sits at an endpoint, or .
SMO(K, y, C, eps) # LIBSVM-style working-set selection
alpha = 0; G = -1 (gradient of D at alpha = 0)
loop:
I_up = {t : (y_t = +1 and alpha_t < C) or (y_t = -1 and alpha_t > 0)}
I_low = {t : (y_t = -1 and alpha_t < C) or (y_t = +1 and alpha_t > 0)}
i = argmax over I_up of -y_t * G_t
j = argmin over I_low of -y_t * G_t # or the second-order choice
if (-y_i * G_i) - (-y_j * G_j) <= eps: stop # KKT conditions hold to eps
analytic update of (alpha_i, alpha_j) with clipping
G += Q[:, i] * d_alpha_i + Q[:, j] * d_alpha_j
b from the free support vectorsThe pair is the maximal violating pair: the KKT conditions hold exactly when . Keerthi, Shevade, Bhattacharyya and Murthy (2001) introduced this two-threshold view; Fan, Chen and Lin (2005) choose by second-order information (the largest guaranteed decrease of ) and proved linear convergence of the resulting method. LIBSVM's default tolerance is .
An SMO step on a pair with , , , current , , , and errors , . What is the new ?
Show the answer
0.35
and . With the feasible segment is , so no clipping is needed. Then , and is preserved.
The leave-one-out bound
Take with . Drop it: the remaining multipliers still satisfy the box and , since . The decision function is unchanged, because contributed .
Every KKT condition for the smaller problem involves only the multipliers and , which are unchanged, so it still holds with the same . For a convex problem, KKT points are optimal: .
implies , so classifies correctly. Leave-one-out errors can therefore occur only at support vectors:
For the expectation: the -th leave-one-out term is the error of a rule trained on i.i.d. examples, tested on an independent one, so each term has mean .
The bound costs nothing to evaluate after training, and it is meaningful when the solution is sparse: 50 support vectors among 1,000 examples cap the leave-one-out error at 5%, an almost unbiased estimate of the error of training on 999 examples. It is crude on noisy problems: every margin violator is a support vector, and with a universal kernel the fraction of support vectors stays at least about the Bayes error as grows (Steinwart, 2003), so the bound cannot certify anything better than that.
Solvers at scale
Kernel SVMs. SMO-type decomposition methods made SVMs practical, but they still touch a large share of the kernel entries, caching the most used columns. This is not just a weakness of current algorithms: Backurs, Indyk and Schmidt (2017) showed that, under the Strong Exponential Time Hypothesis, no algorithm solves kernel SVM training (or kernel ridge regression) to high accuracy in subquadratic time. Large-scale kernel methods therefore approximate the kernel itself, with Nyström or random-feature methods (Scaling Kernel Methods).
Linear SVMs. With the identity and no bias term (or the bias folded into a constant feature), the dual has only box constraints, and coordinate descent on it is cheap: maintain , so the partial derivative costs one sparse dot product.
import numpy as np
def linear_svm_dcd(X, y, C=1.0, epochs=10, seed=0):
"""Dual coordinate descent for min 1/2||w||^2 + C sum hinge (no bias)."""
n, d = X.shape
alpha, w = np.zeros(n), np.zeros(d)
sq_norms = np.einsum('ij,ij->i', X, X)
rng = np.random.default_rng(seed)
for _ in range(epochs):
for i in rng.permutation(n):
if sq_norms[i] == 0:
continue
g = y[i] * (w @ X[i]) - 1.0 # partial derivative of the dual
new = min(max(alpha[i] - g / sq_norms[i], 0.0), C)
w += (new - alpha[i]) * y[i] * X[i] # keep w in sync
alpha[i] = new
return w, alphaEach update minimizes the dual exactly in one coordinate and clips to . Hsieh, Chang, Lin, Keerthi and Sundararajan (2008) showed that this dual coordinate descent reaches an -accurate solution in iterations; LIBLINEAR implements it for both the hinge and the squared hinge loss.
Key takeaways
- The soft-margin dual is over , , with ; strong duality holds, and .
- KKT sorts points: outside the margin, exactly on it (these give ), margin violators. Moving a non-support vector outside the margin changes nothing.
- SMO updates the maximal violating pair in closed form, with clipping, and stops when the KKT violation is below a tolerance.
- Vapnik's bound: leave-one-out error , so ; useful for sparse solutions, weak on noisy data.
- Kernel SVM training needs essentially quadratic time to reach high accuracy (conditionally on SETH); linear SVMs train by dual coordinate descent with iterations.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
After solving the dual, how do you compute ?
- From any free support vector (): , usually averaged over all of them
- is always zero for SVMs
- From any point with , which lies on the margin
Show the answer
A. From any free support vector (): , usually averaged over all of them
Free support vectors satisfy exactly; multiplying by (with ) gives , and solving for gives the formula. Averaging over all free support vectors reduces round-off error.
Why does SMO update two multipliers at a time rather than one?
- Two variables are faster to store
- The constraint would be violated by changing any single ; with two, one can compensate the other
- The kernel matrix is only defined for pairs of points
- One-variable updates cannot be clipped to the box
Show the answer
B. The constraint would be violated by changing any single ; with two, one can compensate the other
With the other multipliers fixed, the equality constraint determines from , so a feasible move needs at least two variables, moving along . Two is also small enough to solve analytically.
An SVM trained on examples has 120 support vectors. What does Vapnik's bound give, and what does it bound?
- The training error is at most 6%
- The test error of this classifier is exactly 6%
- The leave-one-out error is at most 6%, an (almost) unbiased estimate of the error of SVMs trained on 1,999 examples
- At most 120 test points will be misclassified
Show the answer
C. The leave-one-out error is at most 6%, an (almost) unbiased estimate of the error of SVMs trained on 1,999 examples
Leave-one-out errors occur only at support vectors, so . In expectation, the leave-one-out error equals the risk of training on examples, so .
Further reading
- C. Cortes & V. Vapnik, "Support-vector networks," Machine Learning 20(3), 273–297 (1995).
- J. C. Platt, "Sequential minimal optimization: a fast algorithm for training support vector machines," Microsoft Research technical report (1998).
- S. S. Keerthi, S. K. Shevade, C. Bhattacharyya & K. R. K. Murthy, "Improvements to Platt's SMO algorithm for SVM classifier design," Neural Computation 13(3), 637–649 (2001).
- R.-E. Fan, P.-H. Chen & C.-J. Lin, "Working set selection using second order information for training support vector machines," Journal of Machine Learning Research 6, 1889–1918 (2005).
- C.-C. Chang & C.-J. Lin, "LIBSVM: a library for support vector machines," ACM Transactions on Intelligent Systems and Technology 2(3), article 27 (2011).