Simple Algorithm
Machine Learning · Convergence · 30 min

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 w\mathbf w and bb from α\boldsymbol\alpha.
  • State the KKT conditions and classify each training point by whether αi=0\alpha_i = 0, 0<αi<C0 < \alpha_i < C or αi=C\alpha_i = C.
  • Derive SMO's analytic two-variable update with clipping, and its maximal-violating-pair selection and stopping rule.
  • Prove the leave-one-out bound ER(fn−1)≤E[#SVn]/n\E R(f_{n-1}) \le \E[\#\mathrm{SV}_n]/n.
  • Explain the computational limits of kernel SVMs and how linear SVMs avoid them.

From primal to dual

Labels are yi∈{−1,+1}y_i \in \{-1, +1\}. Start from the soft-margin primal with a feature map φ\varphi (the identity for a linear SVM), and attach multipliers αi≥0\alpha_i \ge 0 to the margin constraints and μi≥0\mu_i \ge 0 to ξi≥0\xi_i \ge 0:

L(w,b,ξ,α,μ)=12∥w∥2+C∑iξi−∑iαi[yi(w⊤φ(xi)+b)−1+ξi]−∑iμiξi\mathcal L(\mathbf w, b, \boldsymbol\xi, \boldsymbol\alpha, \boldsymbol\mu) = \tfrac12\norm{\mathbf w}^2 + C\sum_i\xi_i - \sum_i\alpha_i\big[y_i(\mathbf w^\top\varphi(\mathbf x_i) + b) - 1 + \xi_i\big] - \sum_i\mu_i\xi_i
The Lagrangian. The dual function is its infimum over the primal variables (w,b,ξ)(\mathbf w, b, \boldsymbol\xi).
DerivationDeriving the dual
  1. ∇wL=0⇒w=∑iαiyiφ(xi),∂bL=0⇒∑iαiyi=0,∂ξiL=0⇒μi=C−αi\nabla_{\mathbf w}\mathcal L = 0 \Rightarrow \mathbf w = \sum_i\alpha_iy_i\varphi(\mathbf x_i),\qquad \partial_b\mathcal L = 0 \Rightarrow \sum_i\alpha_iy_i = 0,\qquad \partial_{\xi_i}\mathcal L = 0 \Rightarrow \mu_i = C - \alpha_i

    Set the derivatives with respect to the primal variables to zero:

  2. max⁡α ∑iαi−12∑i,jαiαjyiyj k(xi,xj)s.t.0≤αi≤C,  ∑iαiyi=0\max_{\boldsymbol\alpha}\ \sum_i\alpha_i - \frac12\sum_{i,j}\alpha_i\alpha_jy_iy_j\,k(\mathbf x_i, \mathbf x_j)\quad\text{s.t.}\quad 0 \le \alpha_i \le C,\ \ \sum_i\alpha_iy_i = 0

    μi≥0\mu_i \ge 0 becomes αi≤C\alpha_i \le C. Substituting, the ξ\xi terms cancel, the bb term vanishes, and with k(xi,xj)=φ(xi)⊤φ(xj)k(\mathbf x_i, \mathbf x_j) = \varphi(\mathbf x_i)^\top\varphi(\mathbf x_j) what remains is a function of α\boldsymbol\alpha alone:

  3. max⁡α 1⊤α−12α⊤Qαs.t.0≤α≤C1,  y⊤α=0\max_{\boldsymbol\alpha}\ \mathbf 1^\top\boldsymbol\alpha - \tfrac12\boldsymbol\alpha^\top Q\boldsymbol\alpha\quad\text{s.t.}\quad \mathbf 0 \le \boldsymbol\alpha \le C\mathbf 1,\ \ \mathbf y^\top\boldsymbol\alpha = 0

    In matrix form, with Qij=yiyjKijQ_{ij} = y_iy_jK_{ij}, that is Q=DyKDyQ = D_yKD_y for Dy=diag⁡(y)D_y = \diag(\mathbf y), which is positive semidefinite whenever the Gram matrix KK is: a concave maximization over a polytope.

  4. Strong duality. The primal is convex with affine constraints and is always feasible (w=0\mathbf w = 0, b=0b = 0, ξi=1\xi_i = 1), 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 w⋆=∑iαi⋆yiφ(xi)\mathbf w^\star = \sum_i\alpha_i^\star y_i\varphi(\mathbf x_i), unique because the objective is strictly convex in w\mathbf w; the optimal α⋆\boldsymbol\alpha^\star need not be unique.

The decision function never needs w\mathbf w explicitly:

f(x)=∑i: αi>0αiyi k(xi,x)+b.f(\mathbf x) = \sum_{i:\ \alpha_i > 0}\alpha_iy_i\,k(\mathbf x_i, \mathbf x) + b.

Training needs the n×nn \times n matrix QQ (or its entries on demand), prediction needs one kernel evaluation per support vector, and φ\varphi 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 αi[yif(xi)−1+ξi]=0\alpha_i[y_if(\mathbf x_i) - 1 + \xi_i] = 0 and μiξi=(C−αi)ξi=0\mu_i\xi_i = (C - \alpha_i)\xi_i = 0. Combining them with feasibility sorts every training point:

MultiplierSlackMargin yif(xi)y_if(\mathbf x_i)Where the point is
αi=0\alpha_i = 0ξi=0\xi_i = 0≥1\ge 1on or outside its margin hyperplane: not a support vector
0<αi<C0 < \alpha_i < Cξi=0\xi_i = 0=1= 1exactly on the margin: a free (unbounded) support vector
αi=C\alpha_i = Cξi=1−yif(xi)≥0\xi_i = 1 - y_if(\mathbf x_i) \ge 0≤1\le 1on or inside the margin, or misclassified: a bounded support vector
Why ξi=0\xi_i = 0 when αi<C\alpha_i < C: then μi=C−αi>0\mu_i = C - \alpha_i > 0, so μiξi=0\mu_i\xi_i = 0 forces ξi=0\xi_i = 0. Why yif=1y_if = 1 when αi>0\alpha_i > 0 and ξi=0\xi_i = 0: the first condition forces the margin constraint to be tight.

Recovering bb. Every free support vector satisfies yif(xi)=1y_if(\mathbf x_i) = 1, so b=yi−∑jαjyjk(xj,xi)b = y_i - \sum_j\alpha_jy_jk(\mathbf x_j, \mathbf x_i). Solvers average this over all free support vectors for numerical stability. If there are none, the KKT conditions only confine bb to an interval, and LIBSVM takes its midpoint.

Sparsity, and stability of the solution. A non-support vector can be moved anywhere that keeps yif(xi)>1y_if(\mathbf x_i) > 1: the old α\boldsymbol\alpha still satisfies every KKT condition, so it stays optimal and ff does not change at all. A quirk follows for linear kernels in the plane: when three free support vectors pin down (w1,w2,b)(w_1, w_2, b) through their three equations yif(xi)=1y_if(\mathbf x_i) = 1, moving a bounded support vector inside the margin is often absorbed by the free α\alpha's, leaving the boundary in place until some multiplier reaches 00 or CC.

Check your understanding

At the optimum, a training point has αi=C\alpha_i = C. What can you conclude?

  1. It is misclassified
  2. It lies exactly on the margin
  3. yif(xi)≤1y_if(\mathbf x_i) \le 1: it is on or inside the margin, possibly misclassified, with slack ξi=1−yif(xi)\xi_i = 1 - y_if(\mathbf x_i)
  4. It has no influence on the solution
Show the answer

C. yif(xi)≤1y_if(\mathbf x_i) \le 1: it is on or inside the margin, possibly misclassified, with slack ξi=1−yif(xi)\xi_i = 1 - y_if(\mathbf x_i)

With αi>0\alpha_i > 0 the constraint is tight, yif(xi)=1−ξiy_if(\mathbf x_i) = 1 - \xi_i, and ξi≥0\xi_i \ge 0. Whether it is misclassified depends on whether ξi>1\xi_i > 1; a bounded support vector can also sit inside the street on its correct side, or (degenerately) exactly on the margin with ξi=0\xi_i = 0.

SMO: two variables at a time

General-purpose quadratic programming solvers need the dense n×nn \times n matrix QQ 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 αi\alpha_i would violate ∑iαiyi=0\sum_i\alpha_iy_i = 0.

Write the dual as the minimization of D(α)=12α⊤Qα−1⊤αD(\boldsymbol\alpha) = \tfrac12\boldsymbol\alpha^\top Q\boldsymbol\alpha - \mathbf 1^\top\boldsymbol\alpha, and let Et=f(xt)−ytE_t = f(\mathbf x_t) - y_t be the current prediction error on point tt.

DerivationThe analytic two-variable step
  1. D(t)=D(0)+t (Ej−Ei)+12 η t2,η=Kii+Kjj−2Kij=∥φ(xi)−φ(xj)∥2≥0D(t) = D(0) + t\,(E_j - E_i) + \tfrac12\,\eta\,t^2,\qquad \eta = K_{ii} + K_{jj} - 2K_{ij} = \norm{\varphi(\mathbf x_i) - \varphi(\mathbf x_j)}^2 \ge 0

    Move along the direction that keeps yiαi+yjαjy_i\alpha_i + y_j\alpha_j fixed: αj→αj+yjt\alpha_j \to \alpha_j + y_jt and αi→αi−yit\alpha_i \to \alpha_i - y_it. Using ∇D=Qα−1\nabla D = Q\boldsymbol\alpha - \mathbf 1 and yt ∂tD=Et−by_t\,\partial_tD = E_t - b, the objective along this line is a one-dimensional quadratic:

  2. αjnew=αj+yj (Ei−Ej)η\alpha_j^{\text{new}} = \alpha_j + \frac{y_j\,(E_i - E_j)}{\eta}

    For η>0\eta > 0 its minimizer gives Platt's update (the bias bb cancels in Ei−EjE_i - E_j):

  3. yi≠yj:L=max⁡(0,αj−αi),H=min⁡(C,C+αj−αi)yi=yj:L=max⁡(0,αi+αj−C),H=min⁡(C,αi+αj)αinew=αi+yiyj(αj−αjclipped)\begin{aligned} y_i \ne y_j&:\quad L = \max(0, \alpha_j - \alpha_i),\quad H = \min(C, C + \alpha_j - \alpha_i)\\ y_i = y_j&:\quad L = \max(0, \alpha_i + \alpha_j - C),\quad H = \min(C, \alpha_i + \alpha_j)\\ \alpha_i^{\text{new}} &= \alpha_i + y_iy_j\big(\alpha_j - \alpha_j^{\text{clipped}}\big) \end{aligned}

    Clip to the segment of the line inside the box [0,C]2[0, C]^2, then restore the equality constraint:

  4. Each step lowers DD (or leaves it unchanged), costs two kernel columns to update every EtE_t, and never needs QQ in full. When η=0\eta = 0, two identical feature vectors, the objective is linear along the line and the minimum sits at an endpoint, LL or HH.

text
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 vectors

The pair (i,j)(i, j) is the maximal violating pair: the KKT conditions hold exactly when max⁡Iup(−ytGt)≤min⁡Ilow(−ytGt)\max_{I_{\text{up}}}(-y_tG_t) \le \min_{I_{\text{low}}}(-y_tG_t). Keerthi, Shevade, Bhattacharyya and Murthy (2001) introduced this two-threshold view; Fan, Chen and Lin (2005) choose jj by second-order information (the largest guaranteed decrease of DD) and proved linear convergence of the resulting method. LIBSVM's default tolerance is 10−310^{-3}.

Predict before you look

An SMO step on a pair with yi=yj=+1y_i = y_j = +1, Kii=Kjj=1K_{ii} = K_{jj} = 1, Kij=0.2K_{ij} = 0.2, current αi=0.5\alpha_i = 0.5, αj=0.1\alpha_j = 0.1, C=1C = 1, and errors Ei=0.1E_i = 0.1, Ej=−0.3E_j = -0.3. What is the new αj\alpha_j?

Show the answer

0.35

η=1+1−0.4=1.6\eta = 1 + 1 - 0.4 = 1.6 and αj+yj(Ei−Ej)/η=0.1+0.4/1.6=0.35\alpha_j + y_j(E_i - E_j)/\eta = 0.1 + 0.4/1.6 = 0.35. With yi=yjy_i = y_j the feasible segment is [L,H]=[max⁡(0,0.6−1),min⁡(1,0.6)]=[0,0.6][L, H] = [\max(0, 0.6 - 1), \min(1, 0.6)] = [0, 0.6], so no clipping is needed. Then αinew=0.5+(0.1−0.35)=0.25\alpha_i^{\text{new}} = 0.5 + (0.1 - 0.35) = 0.25, and αi+αj=0.6\alpha_i + \alpha_j = 0.6 is preserved.

The leave-one-out bound

DerivationProof
  1. Take ii with αi=0\alpha_i = 0. Drop it: the remaining multipliers still satisfy the box and ∑l≠iαlyl=0\sum_{l \ne i}\alpha_ly_l = 0, since αiyi=0\alpha_iy_i = 0. The decision function is unchanged, because xix_i contributed αiyik(xi,⋅)=0\alpha_iy_ik(\mathbf x_i, \cdot) = 0.

  2. Every KKT condition for the smaller problem involves only the multipliers and ff, which are unchanged, so it still holds with the same bb. For a convex problem, KKT points are optimal: fS∖i=fSf_{S\setminus i} = f_S.

  3. ∑i1{yifS∖i(xi)≤0}  ≤  #{i:αi>0}\sum_i \mathbf 1\{y_if_{S\setminus i}(\mathbf x_i) \le 0\} \;\le\; \#\{i : \alpha_i > 0\}

    αi=0\alpha_i = 0 implies yifS(xi)≥1>0y_if_S(\mathbf x_i) \ge 1 > 0, so fS∖if_{S\setminus i} classifies xi\mathbf x_i correctly. Leave-one-out errors can therefore occur only at support vectors:

  4. E LOO(S)=E R(fn−1)  ≤  E[#SVn]n\E\,\mathrm{LOO}(S) = \E\,R(f_{n-1}) \;\le\; \frac{\E[\#\mathrm{SV}_n]}{n}

    For the expectation: the ii-th leave-one-out term is the error of a rule trained on n−1n - 1 i.i.d. examples, tested on an independent one, so each term has mean ER(fn−1)\E R(f_{n-1}).

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 nn 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 n2n^2 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 φ\varphi 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 w=∑iαiyixi\mathbf w = \sum_i\alpha_iy_i\mathbf x_i, so the partial derivative yiw⊤xi−1y_i\mathbf w^\top\mathbf x_i - 1 costs one sparse dot product.

python
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, alpha

Each update minimizes the dual exactly in one coordinate and clips to [0,C][0, C]. Hsieh, Chang, Lin, Keerthi and Sundararajan (2008) showed that this dual coordinate descent reaches an ε\varepsilon-accurate solution in O(log⁡(1/ε))\order{\log(1/\varepsilon)} iterations; LIBLINEAR implements it for both the hinge and the squared hinge loss.

Key takeaways

  • The soft-margin dual is max⁡ 1⊤α−12α⊤Qα\max\ \mathbf 1^\top\boldsymbol\alpha - \tfrac12\boldsymbol\alpha^\top Q\boldsymbol\alpha over 0≤αi≤C0 \le \alpha_i \le C, y⊤α=0\mathbf y^\top\boldsymbol\alpha = 0, with Qij=yiyjk(xi,xj)Q_{ij} = y_iy_jk(\mathbf x_i, \mathbf x_j); strong duality holds, and w=∑iαiyiφ(xi)\mathbf w = \sum_i\alpha_iy_i\varphi(\mathbf x_i).
  • KKT sorts points: αi=0\alpha_i = 0 outside the margin, 0<αi<C0 < \alpha_i < C exactly on it (these give bb), αi=C\alpha_i = C margin violators. Moving a non-support vector outside the margin changes nothing.
  • SMO updates the maximal violating pair in closed form, αj←αj+yj(Ei−Ej)/η\alpha_j \leftarrow \alpha_j + y_j(E_i - E_j)/\eta with clipping, and stops when the KKT violation is below a tolerance.
  • Vapnik's bound: leave-one-out error ≤#SV/n\le \#\mathrm{SV}/n, so ER(fn−1)≤E[#SVn]/n\E R(f_{n-1}) \le \E[\#\mathrm{SV}_n]/n; 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 O(log⁡(1/ε))\order{\log(1/\varepsilon)} iterations.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

After solving the dual, how do you compute bb?

  1. From any free support vector (0<αi<C0 < \alpha_i < C): b=yi−∑jαjyjk(xj,xi)b = y_i - \sum_j\alpha_jy_jk(\mathbf x_j, \mathbf x_i), usually averaged over all of them
  2. b=∑iαiyib = \sum_i\alpha_iy_i
  3. bb is always zero for SVMs
  4. From any point with αi=0\alpha_i = 0, which lies on the margin
Show the answer

A. From any free support vector (0<αi<C0 < \alpha_i < C): b=yi−∑jαjyjk(xj,xi)b = y_i - \sum_j\alpha_jy_jk(\mathbf x_j, \mathbf x_i), usually averaged over all of them

Free support vectors satisfy yif(xi)=1y_if(\mathbf x_i) = 1 exactly; multiplying by yiy_i (with yi2=1y_i^2 = 1) gives f(xi)=yif(\mathbf x_i) = y_i, and solving for bb gives the formula. Averaging over all free support vectors reduces round-off error.

Question 2

Why does SMO update two multipliers at a time rather than one?

  1. Two variables are faster to store
  2. The constraint ∑iαiyi=0\sum_i\alpha_iy_i = 0 would be violated by changing any single αi\alpha_i; with two, one can compensate the other
  3. The kernel matrix is only defined for pairs of points
  4. One-variable updates cannot be clipped to the box
Show the answer

B. The constraint ∑iαiyi=0\sum_i\alpha_iy_i = 0 would be violated by changing any single αi\alpha_i; with two, one can compensate the other

With the other multipliers fixed, the equality constraint determines αi\alpha_i from αj\alpha_j, so a feasible move needs at least two variables, moving along yiαi+yjαj=consty_i\alpha_i + y_j\alpha_j = \text{const}. Two is also small enough to solve analytically.

Question 3

An SVM trained on n=2,000n = 2{,}000 examples has 120 support vectors. What does Vapnik's bound give, and what does it bound?

  1. The training error is at most 6%
  2. The test error of this classifier is exactly 6%
  3. The leave-one-out error is at most 6%, an (almost) unbiased estimate of the error of SVMs trained on 1,999 examples
  4. 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 LOO≤120/2000=6%\mathrm{LOO} \le 120/2000 = 6\%. In expectation, the leave-one-out error equals the risk of training on n−1n - 1 examples, so ER(f1999)≤E[#SV2000]/2000\E R(f_{1999}) \le \E[\#\mathrm{SV}_{2000}]/2000.

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).