Soft Margins & Surrogate Losses
Slack variables, the hinge loss and classification calibration.
An SVM is judged by how many points it misclassifies, but it is trained to minimize the hinge loss. When does driving down a convex surrogate also drive down the classification error? Classification calibration answers this exactly, turns it into quantitative inequalities, and explains a practical fact: SVM scores are not probabilities.
What you’ll learn
- Write soft-margin SVMs as regularized hinge-loss minimization and compare the hinge with the squared hinge, logistic and exponential surrogates.
- Define classification calibration and the -transform, compute for four losses, and state the Bartlett–Jordan–McAuliffe theorem.
- Show that the hinge risk is minimized by , so SVM scores do not estimate probabilities, and recalibrate them with Platt scaling.
- Analyze Pegasos, stochastic subgradient descent on the primal with step .
- Formulate the -SVM and interpret as a bound on margin errors and support vectors.
Surrogates for the 0–1 loss
Labels are , a score function predicts , and the margin is . The classification risk is , the risk of the 0–1 loss , and is the Bayes risk. Minimizing the empirical 0–1 loss is discontinuous, non-convex and, over linear classifiers, computationally hard in the worst case. So we minimize a convex surrogate instead, with -risk and minimum over all measurable .
As the Descent lesson showed, the soft-margin SVM is exactly regularized empirical -risk minimization with the hinge loss:
with the norm of , or the kernel's function norm.
| Loss | Used by | Differentiable at 0, | |
|---|---|---|---|
| hinge | SVM | yes, (the kink is at ) | |
| squared hinge | L2-loss SVM | yes, | |
| logistic | logistic regression | yes, | |
| exponential | AdaBoost | yes, | |
| perceptron | the perceptron | no: kink at |
Classification calibration
Because with , everything reduces to one point at a time. Define the conditional -risk of predicting the score when , its minimum, and its minimum over scores with the wrong sign:
On both hinges are active, so the conditional risk is linear in ; outside that interval it only grows:
For the slope is negative, so the minimum is at ; for , at :
On the wrong side, for , the function is smallest at , so . With :
| Loss | Minimizer | ||
|---|---|---|---|
| hinge | |||
| squared hinge | |||
| logistic (natural log) | |||
| exponential | |||
| perceptron | for every | : not calibrated |
A classifier trained with the squared hinge loss has excess -risk . What upper bound on its excess classification risk does the comparison inequality give?
Show the answer
0.1
gives . The same excess hinge risk would have given , since the hinge's is . Under a low-noise (margin) condition, the bounds for curved can be sharpened.
SVM scores are not probabilities
The table's second column holds the key practical fact. The logistic and exponential minimizers are invertible functions of : from one recovers . The hinge minimizer keeps only the side of on which falls. Lin (2002) made this point for SVMs: their population target is the Bayes decision itself, not the conditional probability. With lots of data and a flexible kernel, an SVM's scores therefore tend to pile up near and say little about whether is 0.6 or 0.99.
This is not a defect so much as a trade. The hinge loss is exactly flat for margins above 1, which is what makes the SVM sparse. Bartlett and Tewari (2007) proved that the two goals conflict: losses that allow the conditional probability to be estimated over its whole range lead, asymptotically, to solutions that are not sparse.
Platt scaling. When probabilities are needed, fit a two-parameter sigmoid to the scores,
by maximum likelihood on scores from data the SVM was not trained on: a held-out split or cross-validation. Training-set scores are biased, because the points that shaped the boundary sit at margin exactly 1 by construction. Platt (2000) also smoothed the 0/1 targets slightly to avoid overfitting with few examples; Lin, Lin and Weng (2007) gave a numerically more robust fitting algorithm. The general theory of recalibration is in Calibration & Proper Scoring Rules.
Pegasos: solving the primal stochastically
For linear SVMs with many examples, the primal (bias folded into a constant feature) is -strongly convex, and stochastic subgradient descent exploits that. Pegasos (Shalev-Shwartz, Singer, Srebro & Cotter, 2011) samples one example per step, takes a subgradient step of size , and optionally projects onto the ball , which contains the optimum. (A cruder argument already gives : .)
import numpy as np
def pegasos(X, y, lam=1e-4, T=100_000, seed=0):
"""Stochastic subgradient descent on lam/2 ||w||^2 + mean hinge loss."""
n, d = X.shape
w = np.zeros(d)
rng = np.random.default_rng(seed)
for t in range(1, T + 1):
i = rng.integers(n)
eta = 1.0 / (lam * t)
active = y[i] * (w @ X[i]) < 1 # is the hinge active?
w *= 1.0 - eta * lam # gradient of the regularizer
if active:
w += eta * y[i] * X[i] # subgradient of the hinge
norm = np.linalg.norm(w)
if norm > 1.0 / np.sqrt(lam): # optional projection
w /= norm * np.sqrt(lam)
return wThe step is the classical choice for -strongly convex objectives. With subgradient norms bounded by , the averaged iterate's suboptimality is of order in expectation, so iterations reach accuracy , and for linear kernels the total run time does not grow with the training-set size .
Think first Pegasos's run time does not depend on . Does that mean a bigger training set is never more expensive?
For a fixed accuracy on the training objective, yes: each step touches one example, and the number of steps depends on and , not . But with more data you would choose a smaller (less regularization is needed), and the cost grows like . Shalev-Shwartz and Srebro's view is that with more data, reaching a given test error can even get cheaper, because the optimization need not be as precise.
The ν-SVM, and beyond two classes
is hard to interpret: its useful range depends on and on the scale of the kernel. Schölkopf, Smola, Williamson and Bartlett (2000) replaced it by a parameter with a meaning. The -SVM also optimizes the margin level :
A margin error is a point with , on the wrong side of its margin hyperplane.
Stationarity of the Lagrangian in and (with multipliers , and for the three constraint families) gives
If , complementary slackness gives , so . A margin error has , hence and . Every support vector has :
So is an upper bound on the fraction of margin errors and a lower bound on the fraction of support vectors. Schölkopf et al. also showed that, under mild conditions on the distribution and the kernel, both fractions converge to with probability 1 as .
Consistency. Calibration of the hinge, plus the ability of a universal kernel's function class to approximate any decision function, plus a regularization at a suitable rate, makes kernel SVMs universally consistent (Steinwart, 2005).
More than two classes. Reductions such as one-vs-rest inherit binary calibration. Single-machine multiclass SVMs are more delicate: Tewari and Bartlett (2005) showed that the popular Crammer–Singer and Weston–Watkins hinge losses are not calibrated in general, so they can fail to be consistent when no class has conditional probability above , while the loss of Lee, Lin and Wahba is calibrated.
Key takeaways
- A surrogate is classification-calibrated when choosing the wrong sign always costs extra conditional -risk; then , and -risk consistency implies classification consistency.
- A convex is calibrated iff exists and is negative. The hinge has , the squared hinge , the exponential , and the logistic about .
- The hinge risk is minimized by , so SVM scores are not probabilities; Platt scaling fits on held-out scores. Sparsity and probability estimation are in genuine tension.
- Pegasos solves the primal by stochastic subgradient steps of size , needing iterations independent of for linear kernels.
- The -SVM's bounds the fraction of margin errors from above and of support vectors from below; single-machine multiclass hinge losses are not always calibrated.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which convex loss of the margin is not classification-calibrated?
Show the answer
C.
A convex is calibrated if and only if it is differentiable at with . The perceptron loss has a kink at , and indeed its conditional risk is minimized at for every , an uninformative score.
With unlimited data and a rich enough function class, an SVM's score function approaches the minimizer of the hinge risk. What can you read off it?
- The conditional probability , through the sigmoid
- Only the Bayes decision,
- The log-odds
- Nothing: the hinge minimizer does not exist
Show the answer
B. Only the Bayes decision,
The conditional hinge risk is minimized at , which discards . Probabilities need an extra step such as Platt scaling on held-out scores, or a loss with an invertible minimizer, such as the logistic.
A -SVM is trained with on 4,000 examples and finds . What do you know?
- Exactly 200 examples are support vectors
- The test error is at most 5%
- At most 200 examples are margin errors, and at least 200 are support vectors
- At least 200 margin errors and at most 200 support vectors
Show the answer
C. At most 200 examples are margin errors, and at least 200 are support vectors
With , and , with equality for margin errors. Hence (margin errors) (support vectors): at most margin errors and at least 200 support vectors.
Why should Platt scaling be fitted on held-out scores rather than on the SVM's training-set scores?
- Training scores are biased: support vectors sit at margin exactly 1 or inside it by construction, so the sigmoid would be fitted to a distorted picture
- Because the sigmoid has too many parameters
- Because SVM scores on the training set are always negative
- It makes no difference which scores are used
Show the answer
A. Training scores are biased: support vectors sit at margin exactly 1 or inside it by construction, so the sigmoid would be fitted to a distorted picture
The fit shaped the training scores: many training points are pushed exactly to , and the rest beyond it. Scores on fresh data are what the calibrated probabilities will be applied to, so the sigmoid must be fitted on them (a held-out split or cross-validation).
Further reading
- P. L. Bartlett, M. I. Jordan & J. D. McAuliffe, "Convexity, classification, and risk bounds," Journal of the American Statistical Association 101(473), 138–156 (2006).
- T. Zhang, "Statistical behavior and consistency of classification methods based on convex risk minimization," Annals of Statistics 32(1), 56–85 (2004).
- Y. Lin, "Support vector machines and the Bayes rule in classification," Data Mining and Knowledge Discovery 6(3), 259–275 (2002).
- J. C. Platt, "Probabilistic outputs for support vector machines and comparisons to regularized likelihood methods," in Advances in Large Margin Classifiers (MIT Press, 2000).
- S. Shalev-Shwartz, Y. Singer, N. Srebro & A. Cotter, "Pegasos: primal estimated sub-gradient solver for SVM," Mathematical Programming 127(1), 3–30 (2011).