Textbook › Part III Deep Learning: what assumption you add
CHAPTER 1

Write the Rule, or Fit the Rule

FREE15 min

Ask someone to write down, in words, how to tell a cat from a dog. They stall. Pointed ears, whiskers, vertical pupils — every rule has counter-examples. And yet people recognise a cat instantly. The rule cannot be written, but the judgement works. That gap is where this book starts.

There are two ways forward. Write the rule, or fit the rule.

FIG 1Two clusters with real overlap. In "write the rule" you set the line by hand with two knobs — angle and offset. Switch to "fit it from data" and the same line is found without touching them. Point count and spread are adjustable

Writing it: the limit is the number of knobs

The figure opens on a deliberately bad line at 46.7%. Work the sliders patiently and you reach 93.3% — that is the ceiling, measured by exhaustively searching both knobs on this figure's default points. What remains sits where the two clusters overlap; no single straight line can pick it up.

The thing to notice is not that you got there, but why two knobs were enough. The points live on a plane: there are exactly two features, horizontal and vertical.

With dd features the formula contains d+1d+1 numbers: dd weights w1,…,wdw_1,\dots,w_d for the orientation and one offset bb for the position. Three features make the line a plane, so four numbers; with 784 features — a small 28×28 image, pixels laid out flat — 785. At 785 numbers to turn by hand, there is no human strategy left.

Notice that one number is left over. This figure has d=2d=2, so there are three numbers (w1,w2,bw_1, w_2, b) but only two knobs. Scale ww and bb together by two and the set of points satisfying w⊤x+b=0w^{\top}x + b = 0 is unchanged — the line does not move a millimetre. Only two things are worth moving: direction and position. Angle and offset are those two put directly under your fingers.

There are d+1d+1 numbers, but the boundary uses only dd of them. The leftover one — the scale — changes something else instead of the boundary. What it changes is measured later in this chapter.

That is the limit of writing rules. It is set by dimension, not by cleverness.

Fitting it: the procedure does not grow with dimension

Switch the figure to "fit it from data". The knobs lock and the line starts moving on its own. It settles at 91.7% — just under the 93.3% ceiling you can reach by hand, both measured on the same points. It got there without a single knob being touched.

What it does is one line. Judge the points with the current line, then nudge the line in the direction of how it was wrong. Repeat.

w←w−η⋅1m∑i=1m(σ(w⊤xi+b)−yi)xiw \leftarrow w - \eta \cdot \frac{1}{m}\sum_{i=1}^{m} \left(\sigma(w^{\top}x_i + b) - y_i\right) x_i
(1)

Here σ(z)=1/(1+e−z)\sigma(z) = 1/(1+e^{-z}), yiy_i is the label (0 or 1) and η\eta is the step size. The part to watch in equation (1) is (σ(⋅)−yi)(\sigma(\cdot) - y_i): the gap between prediction and truth. Zero gap, no movement; large gap, large movement.

What matters most is that the equation does not mention the dimension of xx. Two features or 784, the formula is identical — only the loop count changes. The "fit" side of the figure is running exactly this, one step per frame.

(The fitted line does not quite reach 93.3%, and that is not sloppiness. Equation (1) lowers log loss, not accuracy; they are different quantities. The numbers are below.)

Writing becomes unmanageable as dimension grows; fitting does not change shape at all. That asymmetry is the reason to use machine learning.

Where equation (1) comes from

"Nudge it in the direction of how it was wrong" was a paraphrase, but nobody picked that direction by hand. Fix one quantity you want to lower and the direction follows uniquely from a derivative. Equation (1) is that derivative. Here is the quantity.

Read pi=σ(w⊤xi+b)p_i = \sigma(w^{\top}x_i + b) as "the probability that this point has yi=1y_i=1". Where the label is 1 you want pip_i large; where it is 0 you want 1−pi1-p_i large. Both at once is pi yi(1−pi)1−yip_i^{\,y_i}(1-p_i)^{1-y_i} — since yiy_i is 0 or 1, one factor always has exponent 0 and disappears. Wanting it large for all mm points at once means a product; a product becomes a sum under a logarithm; and "make it large" becomes "make it small" by flipping the sign.

L(w,b)=−1m∑i=1m[ yilog⁡pi+(1−yi)log⁡(1−pi) ]L(w,b) = -\frac{1}{m}\sum_{i=1}^{m}\Big[\, y_i \log p_i + (1-y_i)\log(1-p_i) \,\Big]
(2)

This is cross-entropy (log loss). At a point with yi=1y_i=1 only −log⁡pi-\log p_i survives: it goes to zero as pip_i approaches 1, and grows without bound as pip_i approaches 0. Right and confident is cheap; wrong and confident is enormous. Accuracy has no such notion of how badly you were wrong — it counts one vote per point.

Now differentiate LL with respect to ww. Three steps.

Step 1. The logistic function's derivative is written with itself: σ′(z)=σ(z)(1−σ(z))=p(1−p)\sigma'(z) = \sigma(z)\big(1-\sigma(z)\big) = p(1-p).

Step 2. Differentiate one point's loss ℓ=−[ylog⁡p+(1−y)log⁡(1−p)]\ell = -\big[y\log p + (1-y)\log(1-p)\big] with respect to z=w⊤x+bz = w^{\top}x + b. By the chain rule ∂ℓ/∂z=(∂ℓ/∂p)⋅(∂p/∂z)\partial \ell/\partial z = (\partial \ell/\partial p)\cdot(\partial p/\partial z), where

∂ℓ∂p=−(yp−1−y1−p),∂p∂z=p(1−p)\frac{\partial \ell}{\partial p} = -\left(\frac{y}{p} - \frac{1-y}{1-p}\right), \qquad \frac{\partial p}{\partial z} = p(1-p)

Multiplying clears the denominators:

∂ℓ∂z=−[ y(1−p)−(1−y)p ]=−[ y−yp−p+yp ]=p−y\frac{\partial \ell}{\partial z} = -\big[\, y(1-p) - (1-y)p \,\big] = -\big[\, y - yp - p + yp \,\big] = p - y

That p(1−p)p(1-p) cancels is itself the reason to choose this loss. What is left is p−yp - y — the gap between prediction and truth, the thing inside equation (1)'s brackets.

Step 3. Since z=w⊤x+bz = w^{\top}x + b, we have ∂z/∂w=x\partial z/\partial w = x and ∂z/∂b=1\partial z/\partial b = 1. Averaging over mm points:

∂L∂w=1m∑i=1m(pi−yi) xi,∂L∂b=1m∑i=1m(pi−yi)\frac{\partial L}{\partial w} = \frac{1}{m}\sum_{i=1}^{m}(p_i - y_i)\,x_i, \qquad \frac{\partial L}{\partial b} = \frac{1}{m}\sum_{i=1}^{m}(p_i - y_i)
(3)

So equation (1) is w←w−η ∂L/∂ww \leftarrow w - \eta\,\partial L/\partial w: one step in the direction that lowers the loss, and nothing more. (Equation (3) was checked against numerical differentiation on this chapter's own 60 points; the largest discrepancy is 1.6×10−101.6\times10^{-10}.)

Between "nudge it in the direction of how it was wrong" and equation (1), a human decided exactly one thing: how to write the loss. The derivative decides the direction. Stacking layers does not add a second decision — that is what chapter 3 is about.

The reason to go deep has not appeared yet

So far there is one straight line. For arrangements a line cannot split — one class enclosing the other, say — no angle wins. Push the figure's "spread" up and the clusters merge and accuracy falls. That is not a badly drawn line; the shape "straight line" is insufficient.

Two options follow.

  1. Build features by hand. Add a column such as "distance from the centre" and a straight line can separate it again. This was the mainstream approach for a long time, and designing those columns was the craft.
  2. Fit the features too. Learn the stage that builds features from the input together with the stage that classifies, under the same equation.

The second is what "going deep" means. Layers are stacked not so that a human can assign meaning to each one, but so that the work of building features is also handed to equation (1). The remaining eleven chapters are, almost entirely, the consequences of that one move.

  • Chapter 2: making the fit stronger makes it fit only the data you hold (overfitting).
  • Chapter 3: with layers stacked, how the "gap" in equation (1) reaches the lower layers. The chain rule is all it takes.
  • Chapter 4: choosing η\eta and treating overfitting turn out to be two sides of the same question.
  • Chapter 5 onward: what assumption to build into the structure, according to the shape of the data.

A common misconception: "accuracy up means loss down"

They move together often enough to look like one quantity. They are two, and this chapter's figure pulls them apart.

The source of the gap is the number we left over. Accuracy only reads the sign of w⊤x+bw^{\top}x + b, so scaling ww and bb by the same positive factor changes nothing at all. Loss reads the value of p=σ(w⊤x+b)p = \sigma(w^{\top}x + b), so scaling pushes pp towards 0 or 1 — cheaper at the points you get right, far more expensive at the points you get wrong. Scale changes the loss without changing the boundary. Measured on the figure's default 60 points:

Accuracy Log loss
Best hand-searched line (angle −30°-30°, offset 0.080.08) 93.3% (56/60) 0.4838
The same line, scaled by 5.55 93.3% (56/60) 0.2895
Fitted side, 240 steps (where the figure stops) 91.7% (55/60) 0.3420

Rows 1 and 2 are the same boundary. Not one point is classified differently, yet the loss is 0.4838 against 0.2895 — nearly double. Accuracy looks only at the boundary; loss looks at the boundary and at the confidence. That is what "different quantities" means.

It breaks the other way too. Across those 240 steps there were three occasions where the loss went down and the accuracy went down with it: step 1→2, accuracy 88.3%→81.7% with loss 0.6848→0.6775; step 25→26, 88.3%→86.7% with 0.5547→0.5509; step 35→36, 86.7%→85.0% with 0.5205→0.5175. Equation (1) lowered the loss every single time, and accuracy still fell.

So the fitted side stopping at 91.7%, short of the hand-made 93.3%, is not a question of skill. Equation (1) is doing its job — lowering loss — and that job is not "maximise accuracy". By loss, row 3 (0.3420) beats row 1 (0.4838); but row 2 shows the hand-made boundary reaches 0.2895 if you pick its scale. In other words the fitted side has not reached the bottom of the loss either at 240 steps. Where to stop is chapter 4.

This is not "ignore accuracy". Watch both. Looking only at loss hides the state where the loss improved while the rate you can actually use in production fell — exactly what step 1→2 above did.

Exercises

The answers are folded away. Work them out first.

Exercise 1. For one point's loss ℓ=−[ylog⁡σ(z)+(1−y)log⁡(1−σ(z))]\ell = -\big[y\log\sigma(z) + (1-y)\log(1-\sigma(z))\big], show that ∂ℓ/∂z=σ(z)−1\partial\ell/\partial z = \sigma(z) - 1 in the case y=1y=1. You may use σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1-\sigma(z)).

Show the answer

With y=1y=1 the coefficient 1−y1-y of the second term is zero, leaving ℓ=−log⁡σ(z)\ell = -\log\sigma(z). By the derivative of log⁡\log and the chain rule,

∂ℓ∂z=−1σ(z)⋅σ′(z)=−σ(z)(1−σ(z))σ(z)=−(1−σ(z))=σ(z)−1\frac{\partial \ell}{\partial z} = -\frac{1}{\sigma(z)}\cdot\sigma'(z) = -\frac{\sigma(z)\big(1-\sigma(z)\big)}{\sigma(z)} = -\big(1-\sigma(z)\big) = \sigma(z) - 1

which is p−yp - y at y=1y=1, as in the text. The y=0y=0 case is three lines as well: from ℓ=−log⁡(1−σ(z))\ell = -\log\big(1-\sigma(z)\big) you get ∂ℓ/∂z=σ(z)\partial\ell/\partial z = \sigma(z). The point is that whether yy is 0 or 1, the derivative of σ\sigma clears the denominator and only the gap survives.

Exercise 2. Suppose you feed 28×28 pixels in directly as features. (a) How many numbers does equation (1) move? (b) How many of them determine the shape of the boundary? (c) What does the remaining one change?

Show the answer

(a) 785. 784784 weights in ww plus one offset bb, i.e. d+1d+1.

(b) 784. Scaling ww and bb by the same positive cc leaves the solutions of c (w⊤x+b)=0c\,(w^{\top}x + b) = 0 unchanged, so the boundary uses one fewer than d+1d+1 numbers. Its degrees of freedom are d=784d = 784.

(c) The confidence of the prediction. The larger the scale, the closer σ\sigma's output sits to 0 or 1. The boundary is identical, so accuracy does not move and only the loss does — rows 1 and 2 of the table above.

Exercise 3. Three points, all with label y=1y=1. Model A predicts p=(0.55, 0.55, 0.05)p=(0.55,\,0.55,\,0.05); model B predicts p=(0.45, 0.45, 0.45)p=(0.45,\,0.45,\,0.45). Call it 1 when p>0.5p>0.5. (a) The accuracy of each. (b) The mean log loss of each. (c) Does "higher accuracy means lower loss" hold?

Show the answer

(a) A gets two points right (0.55>0.50.55>0.5): 2/3=66.7%2/3 = 66.7\%. B gets all three wrong (0.45<0.50.45<0.5): 0/3=0%0/3 = 0\%.

(b) With y=1y=1, each point costs −log⁡p-\log p. −log⁡0.55=0.5978-\log 0.55 = 0.5978, −log⁡0.05=2.9957-\log 0.05 = 2.9957, −log⁡0.45=0.7985-\log 0.45 = 0.7985.

LA=0.5978+0.5978+2.99573=1.3971,LB=0.7985×33=0.7985L_A = \frac{0.5978 + 0.5978 + 2.9957}{3} = 1.3971, \qquad L_B = \frac{0.7985 \times 3}{3} = 0.7985

(c) No. A has the higher accuracy and about 1.75× the loss. The difference is A's third point, which was wrong and confident (p=0.05p=0.05 where y=1y=1), costing 2.99572.9957 on its own. B is wrong everywhere, but every prediction sits at 0.450.45 — close to "don't know" — for 0.79850.7985 apiece. Loss counts how badly you were wrong; accuracy does not. A model that is confidently wrong is punished by the loss however good its accuracy looks.

What this chapter does not claim

  • It does not say deeper is always better. Adding layers destabilises training and needs the treatments in chapter 4. Where data is scarce, hand-built features still win.
  • Equation (1) is the simplest classifier (logistic regression). Cross-entropy is derived here, but why it is the right loss to pick — its relation to maximum likelihood, and how it compares with other choices — is chapter 2.
  • The numbers in "a common misconception" are measurements on this chapter's default points (60 synthetic points). Change the count or the spread and the values change. What does not change is that accuracy is blind to scale and loss is sensitive to it.
  • The figure's points are synthetic. Properties of real data — missingness, bias, drift — are not covered.

Going deeper

This chapter is the map. The detail is in the articles.

Comments

Sign in to comment