Exercise: linear classifiers
Before this unit: Day 33: Random forests.
See also: Day 36: Neural networks; Day 35: Gaussian mixtures.
Goal
By the end of this exercise you should be able to:
- describe pixels, objects and windows as points with labels in a feature space,
- read a linear classifier f(\mathbf x) = \mathbf w\cdot\mathbf x + b geometrically: normal vector, offset, signed distance,
- compute the nearest-class-mean line by hand,
- recognise problems that no line can solve.
Do the pen-and-paper part first, then the demo.
Background
| Idea | What it means |
|---|---|
| Linear classifier | decide by the sign of f(\mathbf x) = \mathbf w\cdot\mathbf x + b: class +1 if f > 0, class -1 if f < 0 |
| Labels | y = +1 or y = -1; a point is on its correct side when y\,f(\mathbf x) > 0 |
| \mathbf w | the normal vector of the line (hyperplane), pointing to the +1 side |
| b | the offset: it shifts the line along \mathbf w |
| Signed distance | f(\mathbf x)/\|\mathbf w\|; positive on the +1 side |
| Scaling | (c\mathbf w, cb) with c > 0 gives the same line and the same distances; f is multiplied by c |
| Nearest class mean | \mathbf w = \boldsymbol\mu_+ - \boldsymbol\mu_-, line through the midpoint (\boldsymbol\mu_+ + \boldsymbol\mu_-)/2 |
Train/test splits, overfitting and cross-validation are recalled from the random forests day and not repeated here.
By hand
B1. The classifier f(\mathbf x) = x_1 + 2x_2 - 4 and the points A = (1, 1), B = (3, 2), C = (0, 3), D = (2, 0.5). (a) Compute f for each point and classify it. (b) Compute the signed distance of each point to the line. Which points are closest? (c) Sketch the line (where does it cross the axes?) and the vector \mathbf w.
B2. Multiply \mathbf w and b of B1 by 3. What happens to the line, to the four values of f and to the four distances? And if you multiply by -1?
B3. Class +1: (3, 3), (4, 2), (5, 4). Class -1: (1, 1), (2, 0), (0, 2). (a) Compute the two class means and the nearest-class-mean classifier \mathbf w, b. (b) Check that it classifies all six training points correctly. (c) Classify the new points (2, 3) and (3, 1).
B4. A thresholding rule says “object if the intensity x > 100”. (a) Write it as a linear classifier (w, b) in one dimension. Is the answer unique? (b) The objects are now dark on a bright background: “object if x < 100”. Which (w, b)?
B5. XOR. Class -1: (0, 0) and (1, 1). Class +1: (1, 0) and (0, 1). Suppose a line f = w_1x_1 + w_2x_2 + b classified all four correctly. Add up f(1, 0) + f(0, 1) and f(0, 0) + f(1, 1). What do you conclude? Which extra feature would make the problem linear?
B6. Objects are described by their area in pixels (typically 200 to 2000) and their mean intensity on a scale 0 to 1. A colleague proposes \mathbf w = (1, 1), b = -1000. (a) Which feature decides the class, in practice? Why? (b) Would nearest class mean or a distance-based method (1-NN) have the same problem? (This comes back in unit 4.)
Math questions
B7 (math). Show that the distance of \mathbf x to the hyperplane \mathbf w\cdot\mathbf x + b = 0 is |\mathbf w\cdot\mathbf x + b|/\|\mathbf w\|. (Take a point \mathbf x_0 on the hyperplane and project \mathbf x - \mathbf x_0 on the unit normal.)
B8 (math). Show that the nearest-class-mean rule “\|\mathbf x - \boldsymbol\mu_+\| < \|\mathbf x - \boldsymbol\mu_-\|” is a linear classifier, and find \mathbf w and b.
B9 (math). The perceptron update for a misclassified point (\mathbf x, y) is \mathbf w \leftarrow \mathbf w + y\mathbf x, b \leftarrow b + y. Show that the new value of y\,f(\mathbf x) is larger than the old one, by exactly \|\mathbf x\|^2 + 1. Does that guarantee that the point is now correct?
B1. (a) f(\text{A}) = 1 + 2 - 4 = -1 (class -1), f(\text{B}) = 3 + 4 - 4 = 3 (+1), f(\text{C}) = 0 + 6 - 4 = 2 (+1), f(\text{D}) = 2 + 1 - 4 = -1 (-1). (b) \|\mathbf w\| = \sqrt5 = 2.236. Distances: A -0.447, B +1.342, C +0.894, D -0.447. A and D are closest, on the -1 side, at the same distance. (c) The line crosses the x_1-axis at (4, 0) and the x_2-axis at (0, 2). \mathbf w = (1, 2) points up and to the right, perpendicular to the line, towards B and C.
B2. 3\mathbf w = (3, 6), 3b = -12: the same line. The values of f triple: -3, 9, 6, -3. The distances do not change: f and \|\mathbf w\| both triple. Multiplying by -1 keeps the line but swaps the sides: every point changes class, every distance changes sign.
B3. (a) \boldsymbol\mu_+ = (4, 3), \boldsymbol\mu_- = (1, 1). \mathbf w = (3, 2), midpoint (2.5, 2), b = -(3 \cdot 2.5 + 2 \cdot 2) = -11.5. (b) f = 3.5, 4.5, 11.5 for the +1 points and -6.5, -5.5, -7.5 for the -1 points: all correct. (c) f(2, 3) = 6 + 6 - 11.5 = 0.5: class +1, just. f(3, 1) = 9 + 2 - 11.5 = -0.5: class -1, just.
B4. (a) f(x) = x - 100: w = 1, b = -100. Not unique: any (c, -100c) with c > 0 is the same rule. (b) f(x) = 100 - x: w = -1, b = 100 (or any positive multiple).
B5. For a correct line: f(1, 0) > 0 and f(0, 1) > 0, so f(1, 0) + f(0, 1) = w_1 + w_2 + 2b > 0. Also f(0, 0) < 0 and f(1, 1) < 0, so f(0, 0) + f(1, 1) = w_1 + w_2 + 2b < 0. The same number cannot be both: no line classifies all four. With the product x_3 = x_1x_2 as a third feature, f = x_1 + x_2 - 2x_1x_2 - 0.5 gives -0.5, -0.5 for the class -1 points and +0.5, +0.5 for the class +1 points: linear in (x_1, x_2, x_3).
B6. (a) The area. f = area + intensity - 1000: the intensity changes f by at most 1, the area by hundreds or thousands. In practice the rule is “area > about 1000 px”, whatever the intensity. The numbers in \mathbf w must make up for the different units, and (1, 1) does not. (b) Yes: any method that measures distances between feature vectors (nearest mean, 1-NN, and the RBF kernel function of unit 3) is dominated by the feature with the largest range. A threshold on one feature, or a tree, is not. The cure (unit 4): standardise each feature.
B7. Take \mathbf x_0 with \mathbf w\cdot\mathbf x_0 = -b. The distance of \mathbf x to the hyperplane is the length of the component of \mathbf x - \mathbf x_0 along the unit normal \mathbf n = \mathbf w/\|\mathbf w\| (the component along the hyperplane does not change the distance): \mathbf n\cdot(\mathbf x - \mathbf x_0) = (\mathbf w\cdot\mathbf x - \mathbf w\cdot\mathbf x_0)/\|\mathbf w\| = (\mathbf w\cdot\mathbf x + b)/\|\mathbf w\|. Its absolute value is the distance; the sign tells the side.
B8. \|\mathbf x - \boldsymbol\mu_-\|^2 - \|\mathbf x - \boldsymbol\mu_+\|^2 = -2\boldsymbol\mu_-\cdot\mathbf x + \|\boldsymbol\mu_-\|^2 + 2\boldsymbol\mu_+\cdot\mathbf x - \|\boldsymbol\mu_+\|^2 = 2(\boldsymbol\mu_+ - \boldsymbol\mu_-)\cdot\mathbf x - (\|\boldsymbol\mu_+\|^2 - \|\boldsymbol\mu_-\|^2). It is positive exactly when \boldsymbol\mu_+ is closer. Divided by 2: \mathbf w = \boldsymbol\mu_+ - \boldsymbol\mu_-, b = -\tfrac12(\|\boldsymbol\mu_+\|^2 - \|\boldsymbol\mu_-\|^2) = -\mathbf w\cdot(\boldsymbol\mu_+ + \boldsymbol\mu_-)/2 (since \|\boldsymbol\mu_+\|^2 - \|\boldsymbol\mu_-\|^2 = (\boldsymbol\mu_+ - \boldsymbol\mu_-)\cdot(\boldsymbol\mu_+ + \boldsymbol\mu_-)). With the means of B3: b = -\tfrac12(25 - 2) = -11.5, as before.
B9. New value: y\left((\mathbf w + y\mathbf x)\cdot\mathbf x + b + y\right) = y(\mathbf w\cdot\mathbf x + b) + y^2(\|\mathbf x\|^2 + 1) = y\,f(\mathbf x) + \|\mathbf x\|^2 + 1, since y^2 = 1. It increases, but not necessarily above 0: a badly wrong point may need several updates. And the update can make other points wrong.
Explore
The demo shows two classes of points (blue = -1, red = +1) and a line with its normal vector \mathbf w (the arrow). Drag the arrow’s tip to turn \mathbf w, drag its foot to shift the line. The background is tinted by the sign of f; points on the wrong side are ringed (dashed). (w, b) × 2 and ÷ 2 scale \mathbf w and b together. Nearest class mean puts the line through the midpoint of the two means (the crosses); perceptron step makes one perceptron update with the next misclassified point (circled in yellow). Tick show signed distances to label each point with f/\|\mathbf w\|, and shade the 1-NN regions to see the nearest-neighbour classifier instead. Click to add points, drag them to move them.
L1. Preset by hand. Check your answers to B1 in the readout. Press (w, b) × 2 twice: what happens to the line, to f and to the distances? Compare with B2.
L2. Preset many lines are perfect. Turn and shift the line: find two quite different lines with 0 errors. Compare their smallest y \cdot f/\|\mathbf w\|. Then press nearest class mean: errors and smallest distance? Which of the lines would you trust most on new points, and why?
L3. Preset nearest mean is not the best line. Press nearest class mean: how many errors? Now drag the arrow until the line runs along the two clouds: can you reach 0 errors? Why does the nearest-mean line fail here?
L4. On the same preset, start again (reset line) and press perceptron step a few times. Describe how the line moves. (The perceptron needs 646 updates here before it stops; what is the smallest distance of the line it stops at? See the solution.)
L5. Preset one outlier. What is the smallest number of errors you can reach with a line? Tick shade the 1-NN regions: what does 1-NN do with the outlier?
L6. Presets XOR and ring around a blob. Try to find the best line. What is the fewest errors you can get? Which extra feature (a third axis) would help in each case?
L1. f = -1, 3, 2, -1 and d = -0.45, +1.34, +0.89, -0.45 (A, B, C, D in the order of the readout). After two doublings (\mathbf w, b) = (4, 8, -16): the line does not move, f is 4 times larger (-4, 12, 8, -4), the distances are unchanged.
L2. Every line through the empty gap between the blobs has 0 errors, but the smallest distance varies: the starting line has 0 errors with a smallest distance of 0.58; the nearest-class-mean line has 0 errors with 0.99. A line that passes close to some training point (small smallest distance) is the one most likely to misclassify a new point that falls a little beyond it. This is the idea of the next unit: choose the line with the largest smallest distance.
L3. The nearest-mean line makes 9 errors of 60. A line along the clouds (roughly parallel to their long axes, in the narrow gap between them) has 0 errors. The two clouds are shifted along their length, so the line through the midpoint perpendicular to \boldsymbol\mu_+ - \boldsymbol\mu_- runs nearly across the clouds and cuts both. Nearest mean uses only the two means, not the shape of the clouds.
L4. Each step picks the next point on the wrong side and pulls the line towards classifying it correctly; the line often jumps far and many points flip side (after the first step 30 errors, then 6, then 30 again). It does converge here because the data are separable, but only after 646 updates, and it stops at the first line with 0 errors, whose smallest distance is only 0.015: it grazes a point. The perceptron finds a separating line, not a good one.
L5. At least 1 error: no line puts the outlier on the red side without misclassifying many blue points (the nearest-mean line also has 1 error). 1-NN classifies every training point correctly by construction: it draws a red island around the outlier, which will misclassify new blue points near it.
L6. XOR: the best line still has 14 errors of 60 (a line can at most cut off one corner cluster cleanly). The product x_1x_2 (with the origin in the middle of the square) separates the diagonals. Ring: no line beats calling everything blue, which gets the 17 inner (red) points wrong. The squared distance from the centre, (x_1 - 5)^2 + (x_2 - 5)^2, separates inside from outside with a single threshold. Both are the subject of unit 3.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in svm/src/classify/exercise.rs. A point is [x1, x2]; the labels are +1 and -1.
C1. decision(w, b, x) and signed_distance(w, b, x): the value f(\mathbf x) = \mathbf w\cdot\mathbf x + b and the signed distance f(\mathbf x)/\|\mathbf w\|. The checks are your answers to B1 and B2.
C2. smallest_margin(points, labels, w, b): the smallest y\,f(\mathbf x)/\|\mathbf w\| over all training points, the readout “smallest y \cdot f/\|\mathbf w\|” of the demo:
smallest = +infinity
for every training point (x, y):
smallest = min(smallest, y · f(x) / ‖w‖)
C3. nearest_mean(points, labels): the nearest-class-mean line. The check is B3.
μ+ = mean of the points with y = +1; μ− = mean of the points with y = −1
w = μ+ − μ−
b = −w · (μ+ + μ−) / 2
C4. perceptron(points, labels, max_passes): the perceptron rule of the lecture, going through the points in order until a whole pass makes no update. The checks: it separates the points of B3, one update raises y\,f by \|\mathbf x\|^2 + 1 (B9), and on XOR (B5) it never stops.
w = (0, 0); b = 0
repeat (at most max_passes times):
for every training point (x, y):
if y · f(x) ≤ 0: # wrong side, or on the line
w = w + y · x; b = b + y
if no point was updated in this pass: stop
Then cargo run -p svm -- classify and open the pictures in out/classify/. It prints, for each example, the number of training errors and the smallest y\,f/\|\mathbf w\|.
C5. Two long parallel clouds, shifted along their length (long-nearest-mean.png and long-perceptron.png). How many errors does the nearest-mean line make, and why? The perceptron reaches 0 errors: how many updates does it need, and how far is its line from the closest point? Which of the two lines would you trust on new points?
C6. The last example treats every pixel of a synthetic fluorescence image as a point (grey value, mean of its 5 \times 5 neighbourhood), trained on 200 random pixels. Look at nuclei-feature-space.png and nuclei-nearest-mean.png. Why is a line enough here, unlike for the XOR example?
The model solution is svm/src/classify/solution.rs in the download.
C5. The nearest-mean line makes 7 errors of 60: it is perpendicular to \boldsymbol\mu_+ - \boldsymbol\mu_-, which points partly along the clouds, so the line runs across their ends. The perceptron needs 7125 updates and stops at the first line with 0 errors, whose smallest distance is only 0.011: it grazes a point (in the picture it touches the red cloud). Neither is trustworthy: one is wrong on the training data, the other lies so close to a training point that a new point a little beyond it lands on the wrong side. This is the question of the next unit. (On the six points of B3 both lines have 0 errors, with smallest distances 0.971 for nearest mean and 0.236 for the perceptron.)
C6. The two classes form two separate groups in the feature space: background pixels are dark with a dark neighbourhood, nucleus pixels bright with a bright neighbourhood, and the nuclei lie along the diagonal. A line between the groups separates them; on all 16 384 pixels 99.3 % are classified correctly. In XOR the classes sit in opposite corners, and every line keeps about half of the points wrong (nearest mean: 28 of 60; the perceptron gives up with 30 of 60 wrong).
Where this leads
Among all lines with zero training errors, which is best? The next unit chooses the one with the widest margin: the support vector machine. Unit 3 then replaces the line by curves without giving up the linear algebra: kernel functions.