09 / 27 · Linear Classification
Linear classifiers and surrogate losses
Separating two classes with a hyperplane: the geometry of a linear classifier and the distance to its boundary, the margin, why the number of errors cannot be minimized directly, the convex surrogate losses that bound it (logistic, hinge, exponential, squared hinge) and what each of them estimates, the perceptron as stochastic gradient descent, linear separability, and what a regularizer adds.
Least squares · Stochastic and adaptive optimizers · Regularization
On this page 14
The regularization note finished the linear regression section with a model that predicts a number and does not chase the noise. Many targets are not numbers but classes: spam or not, default or repayment, malignant or benign. The tempting shortcut is to code the two classes as and , fit least squares with a ridge penalty, and predict the sign of the fitted value. This note shows why least squares with a linear model does that job badly, what a linear classifier should minimize instead, and how the oldest learning algorithm, the perceptron, fits into the picture. The losses it introduces return in every remaining note of the section: the logistic loss in logistic regression, the hinge in support vector machines.
Notation. The training set is with and labels . A linear classifier has weights and a bias ; its score is and its prediction . The margin of example is . is 1 when the condition inside holds and 0 otherwise, and is the Euclidean norm. is the probability of the positive class at .
Why not least squares on the labels?#
Take six points on a line: negatives at and positives at . Least squares on the labels fits , and the boundary sits at , exactly between the classes. Now add three more positives at , so clearly positive that no sensible rule should move. The least-squares fit becomes , the boundary moves to , and the positive at is now misclassified. Logistic regression, the subject of the next note, keeps the boundary at with no errors on the same nine points.
The reason is visible once the loss is written in terms of the margin. Since ,
Squared error is a function of the margin that is smallest at and grows on both sides. The three far points have scores around to under the new fit, margins above 1, and squared error charges them for being too right: lowering their scores towards 1 means flattening the line, which drags the boundary to the right. For a linear classifier, a loss that punishes large positive margins lets easy points move the boundary; the losses below never punish them.
A hyperplane splits the space#
The set is a hyperplane: a line in the plane, a plane in three dimensions. The classifier sends one side of it to and the other to . For two points on the hyperplane, , so is perpendicular to every direction within it: is the normal vector, and it points towards the positive side.
Distance to the boundary. Write any point as , where is its projection on the hyperplane and the signed distance along the unit normal. Since ,
The score is the signed distance to the boundary, measured in units of . Multiplying and by the same positive constant changes the scores but neither the boundary nor the predictions, so a linear classifier has one redundant degree of freedom; regularization will remove it. The bias can be absorbed by appending a constant feature to every and to , which is how the code below handles it.
Worked example. With and , the boundary is the line and . The point has score , so it is on the positive side at distance . Its projection on the line is , and indeed .
A linear classifier: boundary, normal and margin
The margin#
The margin combines the label and the score into one number. Its sign says whether the example is classified correctly; its size, divided by , is the distance to the boundary, so it measures how confidently. In the figure the point closest to the boundary among the correct ones has , while a point of class deep on the positive side has . The training error rate is the share of non-positive margins,
where , a point exactly on the boundary, is counted as an error. The function is the 0–1 loss written as a function of the margin.
Why the error count cannot be minimized directly#
The error rate is the natural objective, and it is unusable for two reasons.
- No gradient. As a function of it is piecewise constant: a small change of the weights either changes nothing or flips one point across the boundary. Its gradient is zero almost everywhere and undefined at the jumps, so gradient descent has nothing to follow.
- Computational hardness. Finding the hyperplane with the fewest training errors is NP-hard when the classes overlap (Höffgen, Simon and Van Horn, 1995), and even finding one whose error count is within a constant factor of the best is hard (Ben-David, Eiron and Long, 2003). Only the separable case is easy: a hyperplane with for all is a feasible point of a linear program, found in polynomial time.
What the error rate estimates is still the right target. Its population version, the expected 0–1 loss, is minimized by the Bayes classifier, which predicts the more probable class, , as the table of other losses, other targets records. The question is how to reach it by a computation that works.
Surrogate losses: upper bounds that can be minimized#
If for every , then
The common choices are
The logistic loss is usually written with the natural logarithm, . That version equals at and is not an upper bound near zero; dividing by , as above, makes it one and changes nothing about the minimizer.
The bound caps the error rate; it does not track it. With this loss, the margins have average surrogate loss and one error in two, while the margins have and no error.
The 0–1 loss and its convex upper bounds
At , one unit on the wrong side, the four losses are , , and ; at , comfortably correct, they are , , and . The squared error of the opening example is also an upper bound on the step, but it is the only curve in the figure that rises again on the right. The other differences matter in practice:
| Loss | Convex | Smooth | Zero for confident correct answers | Growth for | Used by |
|---|---|---|---|---|---|
| logistic | yes | yes | no, only tends to 0 | linear | logistic regression |
| hinge | yes | no, kink at | yes, for | linear | support vector machines |
| exponential | yes | yes | no | exponential | AdaBoost |
| squared hinge | yes | once differentiable | yes, for | quadratic | scikit-learn’s LinearSVC (its default loss) |
| squared error | yes | yes | no, rises again for | quadratic | least squares on labels |
Convexity makes them minimizable. Each margin is linear in , and a convex function of a linear function is convex. The average of convex functions is convex, so every local minimum of the empirical surrogate risk is global and gradient methods converge to it. Smoothness decides which methods apply: gradient descent and Newton’s method for the logistic loss, subgradients or a reformulation for the hinge.
Growth on the left decides robustness. A mislabelled point sits at a large negative margin. The linear losses charge it in proportion to how wrong it is, the exponential loss exponentially, so one bad label can dominate the sum; this is the known weakness of AdaBoost on noisy labels (Hastie, Tibshirani and Friedman, 2009, Section 10.6). Exponential loss returns in the gradient boosting note.
What each surrogate estimates#
At a single point with , a score incurs the expected surrogate loss , because the label is with probability . Minimizing it over gives the score that a perfectly flexible model trained on unlimited data would output. For the logistic loss with the natural logarithm, with , so the derivative of the expected loss is
The other rows follow the same way (Hastie, Tibshirani and Friedman, 2009, Table 12.1); the support vector machines note derives the hinge row.
| Loss | Minimizer | At | Recovers ? |
|---|---|---|---|
| logistic | , the log-odds | yes | |
| exponential | yes | ||
| hinge | no | ||
| squared hinge, squared error | yes, on |
Every row has the sign of , so each surrogate, minimized over all functions, gives the Bayes classifier. This property is called classification calibration, and for a convex surrogate it holds exactly when the loss is differentiable at with a negative derivative there (Bartlett, Jordan and McAuliffe, 2006; Zhang, 2004). The rows differ in what else they keep. The hinge minimizer is whatever is, so a hinge-trained score says on which side a point falls and nothing about how likely the side is; the logistic minimizer is the log-odds itself, from which . The exponential and squared rows invert too, through other links: and , both at the scores of the table.
The squared-error row also explains the opening example. Over all functions, squared error on labels is classification-calibrated and even recovers . What failed there was the linear fit: a line cannot follow everywhere, and squared error spends its slope on far points that are already correct. The logistic loss is the starting point of the next note.
The perceptron: stochastic gradient descent at zero margin#
The perceptron of Rosenblatt (1958) is the oldest training rule for a linear classifier. It visits the examples one by one and corrects the weights only after a mistake:
It is stochastic gradient descent on the perceptron loss with step size 1. The gradient of with respect to is for a misclassified example and for a correct one; at the loss has a kink, and the rule takes the subgradient . The step size does not matter when the weights start at zero: every iterate is then a sum of , and a different step only rescales it, which does not change any prediction. The perceptron loss is convex but not an upper bound on the 0–1 loss (it is at ). The hinge differs from it by the shift to , which also charges correct points with margins below 1 and, as the SVM note shows, pushes the boundary away from the data.
Worked example. Four points, with the bias as a third coordinate: and positive, and negative. Start from , and visit the points in order:
| Step | Point | Margin before | Update | after | after |
|---|---|---|---|---|---|
| 1 | , | add | |||
| 2 | , | none | |||
| 3 | , | subtract | |||
| 4 | , | subtract |
On the second pass the margins under are , all positive, and the algorithm stops after three updates.
Why it stops on separable data. Suppose some unit vector separates the data with margin , that is for all , and (the bias is absorbed into , so both hold in the augmented space). Start from and let be the weights after updates. Each update is triggered by a point with , so
After updates and , and by the Cauchy–Schwarz inequality . Hence
the bound of Novikoff (1962). It depends on neither the number of points nor the dimension, only on how wide the gap between the classes is relative to the spread of the data. It is a worst case: on 200 random points in the plane with a gap of at least on each side of a line through the origin, the bound is updates and the perceptron needs 4 (Code section).
When the classes overlap, some point is always misclassified, the updates never stop, and the final weights depend on where the run is cut off. Averaging the weight vectors over all steps, or letting them vote, gives a stable classifier with similar guarantees (Freund and Schapire, 1999), but the standard answer is to minimize a convex surrogate with a regularizer.
Linear separability#
A training set is linearly separable if some hyperplane classifies all of it correctly. The XOR pattern, positives at and and negatives at and , is not: any line that puts both positives on one side puts a negative there too. Minsky and Papert (1969) made this limitation of the single perceptron famous. A new feature fixes it: with added, the four points become separable in three dimensions. Adding features by hand, implicitly through kernels, or by learning them in a neural network are the three ways the course goes beyond lines.
In high dimensions the opposite problem appears: almost everything is separable. Cover (1965) counted the labellings of points in general position in that an affine hyperplane can realize. If the labels are random coin flips, the probability that they are linearly separable is
For it is 1, every labelling; for it is exactly ; beyond that it falls quickly. In dimensions the formula gives , and for 6, 12 and 18 points, and 400 random labellings per size give , and (Code section). Since decreases as grows, a classifier with at least half as many parameters as examples ( with the bias, so ) separates random labels with probability at least , and with it always does. Zero training error therefore proves nothing, since pure-noise labels reach it too; a bag-of-words model with tens of thousands of features is in exactly this regime. It is the classification face of the overfitting of the validation note.
What a regularizer adds#
On separable data the surrogate losses misbehave in two different ways. The logistic and exponential losses keep decreasing as is scaled up along any separating direction, so they have no minimizer and the weights run away; the hinge and squared hinge reach zero as soon as every margin exceeds 1, and then every larger multiple of is an equally good solution. Adding the ridge penalty of the regularization note,
fixes both. The objective is now strictly convex in and grows without bound as , so it has exactly one minimizer in , and it is finite. The penalty also has a geometric meaning: with the margin condition , the distance from the boundary to the nearest point is at least , so making small while keeping margins at least 1 maximizes the gap between the classes. That trade is the support vector machine. For the logistic loss the penalty has a third reading, a Gaussian prior on the weights, which the next note develops; the lasso penalty works as well and, as in regression, sets weights to zero.
What changed#
| Least squares on labels | Linear classifier with a surrogate loss | |
|---|---|---|
| Loss of one example | a convex upper bound on : logistic, hinge, exponential | |
| Confident correct answers | punished for | free or nearly free |
| What the score estimates | , through a loss that also chases far points | log-odds (logistic), only the side (hinge) |
| Separable data | a unique fit that may misclassify | zero loss in the limit (logistic, exponential) or exactly (hinge); a regularizer picks one solution |
| Training | normal equations | gradient methods; the perceptron as SGD at zero margin |
Limitations#
- The boundary is a hyperplane. Interactions such as XOR are invisible without new features.
- The surrogate is not the metric. Minimizing the logistic or hinge loss bounds the error rate, but the quantity that matters may be recall at a fixed false-alarm rate or the cost of errors; the classification metrics note separates the two.
- Scores are not probabilities. A score optimum turns into a probability only through a known link: for the logistic loss, for the exponential loss, for the squared losses; the hinge has none. Even then the probabilities are approximate with a finite sample and a linear model, which may not contain .
- Outliers still pull. Linear growth is gentler than exponential, but a mislabelled point far on the wrong side still contributes the largest term of the sum.
- The guarantees are for the training set. Zero training error and a small surrogate loss say little when the number of features approaches the number of examples, as Cover’s count shows.
What you should remember#
- A linear classifier predicts ; is normal to the boundary, and is the signed distance to it.
- The margin is positive for a correct answer and measures confidence. Squared error on labels is and punishes large margins.
- The 0–1 loss has no useful gradient and minimizing it is NP-hard; convex upper bounds (logistic, hinge, exponential, squared hinge) bound the error rate and can be minimized.
- All of these surrogates lead to the Bayes classifier. The logistic loss also recovers the log-odds, the exponential and squared losses recover through other links, the hinge only the side.
- The perceptron is SGD on ; on separable data it makes at most mistakes, and on overlapping data it never settles.
- Separability is common in high dimensions ( at random labels); a regularizer makes the solution unique and finite and, with the hinge, maximizes the margin.
Code#
Least squares against a classification loss, the surrogate losses and their minimizers, the perceptron and its bound, and Cover’s count:
import numpy as np
from math import comb
from scipy.optimize import linprog, minimize_scalar
from sklearn.linear_model import LogisticRegression
rng = np.random.default_rng(0)
# 1. least squares on ±1 labels: three far, clearly positive points move the boundary
x = np.array([-3, -2, -1, 1, 2, 3, 20, 21, 22.])
y = np.r_[-np.ones(3), np.ones(6)]
b, w = np.linalg.lstsq(np.c_[np.ones_like(x), x], y, rcond=None)[0]
print(round(-b / w, 4), int((np.sign(b + w * x) != y).sum())) # 1.3704 1: the positive at x = 1 is lost
lr = LogisticRegression().fit(x[:, None], y)
print(round(-lr.intercept_[0] / lr.coef_[0, 0], 4), int((lr.predict(x[:, None]) != y).sum())) # -0.0002 0
# 2. the surrogate losses at margins -1, 0 and 2
M = np.array([-1, 0, 2.])
losses = {'logistic': np.log2(1 + np.exp(-M)), 'hinge': np.maximum(0, 1 - M), 'exponential': np.exp(-M),
'squared hinge': np.maximum(0, 1 - M) ** 2, 'squared error': (1 - M) ** 2}
for name, v in losses.items():
print(name, v.round(3))
# logistic [1.895 1. 0.183] | hinge [2. 1. 0.] | exponential [2.718 1. 0.135]
# squared hinge [4. 1. 0.] | squared error [4. 1. 1.]
# a smaller surrogate need not mean fewer errors: mean logistic loss and error rate of two pairs of margins
for pair in ([-0.1, 3], [0.1, 0.5]):
print(np.log2(1 + np.exp(-np.array(pair))).mean().round(2), np.mean(np.array(pair) <= 0)) # 0.57 0.5 | 0.81 0.0
# 3. what each surrogate estimates: the minimizer of eta L(f) + (1 - eta) L(-f) at eta = 0.8
eta = 0.8
for name, L in [('logistic', lambda m: np.log2(1 + np.exp(-m))), ('hinge', lambda m: max(0, 1 - m)),
('exponential', lambda m: np.exp(-m)), ('squared hinge', lambda m: max(0, 1 - m) ** 2)]:
f = minimize_scalar(lambda f: eta * L(f) + (1 - eta) * L(-f), bounds=(-5, 5), method='bounded', options={'xatol': 1e-9}).x
print(name, round(f, 3))
# logistic 1.386 (log 4) | hinge 1.0 | exponential 0.693 (log 2) | squared hinge 0.6
# 4. the perceptron on the four points of the worked example, then on a separable cloud against Novikoff's bound
def perceptron(X, y, max_epochs=1000):
Xb = np.c_[X, np.ones(len(X))]
w, updates = np.zeros(Xb.shape[1]), 0
for _ in range(max_epochs):
mistakes = 0
for xi, yi in zip(Xb, y):
if yi * (w @ xi) <= 0:
w, updates, mistakes = w + yi * xi, updates + 1, mistakes + 1
if mistakes == 0:
return w, updates
return w, updates
print(perceptron(np.array([[2., 1], [1, 3], [-1, 1], [0, -2]]), np.array([1, 1, -1, -1]))) # (array([ 3., 2., -1.]), 3)
u = np.array([0.6, 0.8]) # the separating direction, through the origin
X = rng.uniform(-1, 1, (2000, 2))
X = X[np.abs(X @ u) >= 0.1][:200] # keep a gap of at least 0.1 on each side of the line
y = np.sign(X @ u)
Xb = np.c_[X, np.ones(len(X))] # the perceptron learns a bias too: work in the augmented space
R, gamma = np.linalg.norm(Xb, axis=1).max(), (y * (Xb @ np.r_[u, 0])).min()
print(perceptron(X, y)[1], round((R / gamma) ** 2, 1)) # 4 253.4: four updates, bound 253
# 5. Cover: the chance that random labels on n points in general position in R^5 are linearly separable
def separable(X, y):
A = -y[:, None] * np.c_[X, np.ones(len(X))] # y_i (w·x_i + b) >= 1 as A [w, b] <= -1
return linprog(np.zeros(X.shape[1] + 1), A_ub=A, b_ub=-np.ones(len(X)), bounds=(None, None)).status == 0
d = 5
for n in (6, 12, 18):
share = np.mean([separable(rng.normal(size=(n, d)), rng.choice([-1., 1.], n)) for _ in range(400)])
exact = sum(comb(n - 1, k) for k in range(d + 1)) / 2 ** (n - 1)
print(n, share, round(exact, 4))
# 6 1.0 1.0 | 12 0.48 0.5 | 18 0.065 0.0717
Next#
The surrogate losses turn classification into an optimization problem, and among them the logistic loss stands out: its minimizer is the log-odds, so its scores can be read as probabilities. A probability is often what the application needs, to compare a click’s chance with its price or a patient’s risk with the cost of a test. The next note derives logistic regression from a probability model of the label, shows that the logistic loss is its negative log-likelihood, and asks when its probabilities can be trusted.
References#
- F. Rosenblatt. The perceptron: a probabilistic model for information storage and organization in the brain. Psychological Review, 65(6), 386–408, 1958.
- A. B. J. Novikoff. On convergence proofs on perceptrons. Proceedings of the Symposium on the Mathematical Theory of Automata, 12, 615–622. Polytechnic Institute of Brooklyn, 1962.
- M. Minsky, S. Papert. Perceptrons: An Introduction to Computational Geometry. MIT Press, 1969.
- T. M. Cover. Geometrical and statistical properties of systems of linear inequalities with applications in pattern recognition. IEEE Transactions on Electronic Computers, EC-14(3), 326–334, 1965.
- K.-U. Höffgen, H. U. Simon, K. S. Van Horn. Robust trainability of single neurons. Journal of Computer and System Sciences, 50(1), 114–125, 1995.
- S. Ben-David, N. Eiron, P. M. Long. On the difficulty of approximately maximizing agreements. Journal of Computer and System Sciences, 66(3), 496–514, 2003.
- 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. The Annals of Statistics, 32(1), 56–85, 2004.
- Y. Freund, R. E. Schapire. Large margin classification using the perceptron algorithm. Machine Learning, 37(3), 277–296, 1999.
- C. Cortes, V. Vapnik. Support-vector networks. Machine Learning, 20(3), 273–297, 1995.
- J. Friedman, T. Hastie, R. Tibshirani. Additive logistic regression: a statistical view of boosting. The Annals of Statistics, 28(2), 337–407, 2000. (AdaBoost as minimization of the exponential loss.)
- T. Hastie, R. Tibshirani, J. Friedman. The Elements of Statistical Learning, 2nd ed. Springer, 2009. Section 4.5 (separating hyperplanes and the perceptron), Section 10.6 (loss functions and robustness), Section 12.3.2 and Table 12.1 (losses and their population minimizers).
- S. Shalev-Shwartz, S. Ben-David. Understanding Machine Learning: From Theory to Algorithms. Cambridge University Press, 2014. Chapter 9 (halfspaces and the perceptron), Section 12.3 (surrogate loss functions).
- E. Sokolov et al. Машинное обучение (HSE), lecture notes: lecture 4: linear classification (margins and upper bounds on the error rate), lecture 5 (the logistic and hinge losses).