Md. Asif Uddin

Chapter 2 · I.2

Problems

A chapter that uses mathematics has to teach that mathematics by making the reader compute. A chapter that only displays equations has failed, however correct the equations are.

M3Load-bearing

7/5 problems6/4 variants10/10 exercisesquota met, and enforced

The contract

  • 5 worked problems, minimum.
  • 4 distinct variants, and no variant more than half of them.
  • 10 exercises, every one with a published solution.
  • At least one numeric problem — present.
  • At least one symbolic problem — present.
  • At least one limit or counterexample problem — present.
  • At least one complexity or shape problem — present.
  • At least one ▲▲▲ problem — present.

Numerical instantiationProof or impossibilityConstructed failureLimiting caseCost accountingSymbolic derivation

Problem I.2.B01

One hyperplane, four points, four signed distances

numeric▲△△

All values rounded to 4 d.p.

STATEMENT

A perceptron’s parameters are given. Write the equation of its decision boundary, classify four points, and compute each point’s signed distance from the boundary.

GIVEN

w=[21],b=1\vec{w} = \begin{bmatrix} 2 \\ -1 \end{bmatrix}, \qquad b = -1

and the four points

A=(2,1),B=(0,1),C=(1,1),D=(0,3)A = (2, 1), \quad B = (0, 1), \quad C = (1, -1), \quad D = (0, 3)

FIND

The boundary as an equation in x1,x2x_1, x_2; the predicted label of each point; and each signed distance, a scalar in the same units as the coordinates.

STRATEGY

Compute the raw score s=w,x+bs = \langle \vec{w}, \vec{x}\rangle + b once per point. The sign of ss gives the class and s/ws/\lVert\vec{w}\rVert gives the distance, so one quantity answers both questions.

SOLUTION

Step 1 — the boundary. By Definition 2 the boundary is the set where the score is zero:

2x1x21=0equivalentlyx2=2x112x_1 - x_2 - 1 = 0 \qquad\text{equivalently}\qquad x_2 = 2x_1 - 1

A line of slope 22 through (0.5,0)(0.5, 0). Note that w=(2,1)\vec{w} = (2, -1) is perpendicular to it: the direction along the line is (1,2)(1, 2), and (2,1),(1,2)=22=0\langle (2,-1),(1,2)\rangle = 2 - 2 = 0. The weight vector is always the normal to the boundary. That single fact makes every later step geometric rather than algebraic.

Step 2 — the norm. Needed once, for all four points:

w=22+(1)2=5=2.2361\lVert \vec{w}\rVert = \sqrt{2^2 + (-1)^2} = \sqrt{5} = 2.2361

Step 3 — the scores.

sA=(2)(2)+(1)(1)1=411=+2sB=(2)(0)+(1)(1)1=011=2sC=(2)(1)+(1)(1)1=2+11=+2sD=(2)(0)+(1)(3)1=031=4\begin{aligned} s_A &= (2)(2) + (-1)(1) - 1 = 4 - 1 - 1 = +2 \\ s_B &= (2)(0) + (-1)(1) - 1 = 0 - 1 - 1 = -2 \\ s_C &= (2)(1) + (-1)(-1) - 1 = 2 + 1 - 1 = +2 \\ s_D &= (2)(0) + (-1)(3) - 1 = 0 - 3 - 1 = -4 \end{aligned}

Step 4 — labels and distances. Divide each score by 5\sqrt5 (Projections and orthogonality 0.LA.05):

PointScoreDistanceClass
A=(2,1)A = (2,1)+2+2+0.8944+0.8944+1+1
B=(0,1)B = (0,1)2-20.8944-0.89441-1
C=(1,1)C = (1,-1)+2+2+0.8944+0.8944+1+1
D=(0,3)D = (0,3)4-41.7889-1.78891-1

Answer

Boundary: 2x1x21=02x_1 - x_2 - 1 = 0.

dist(A)=+0.8944,dist(B)=0.8944,dist(C)=+0.8944,dist(D)=1.7889\text{dist}(A) = +0.8944,\quad \text{dist}(B) = -0.8944,\quad \text{dist}(C) = +0.8944,\quad \text{dist}(D) = -1.7889

Classes: A,C+1A, C \to +1 and B,D1B, D \to -1. Distances are scalars in the units of the coordinate axes; the scores are not, which is why the division by w\lVert\vec{w}\rVert is not optional.

Check — numeric · i-2-b01-decision-boundary.py
norm = sqrt(w[0] ** 2 + w[1] ** 2)
score = w[0] * x[0] + w[1] * x[1] + b
dist  = score / norm

Prints ||w|| = 2.2361 and the four distances above.

Executed in CI. The digits above are the digits it printed.

Check — sanity

A point on the boundary has distance zero. Take x=(0.5,0)x = (0.5, 0), which satisfies x2=2x11x_2 = 2x_1 - 1. Its score is (2)(0.5)01=0(2)(0.5) - 0 - 1 = 0, so its distance is 00. The formula agrees with the definition it came from.

Rescaling the parameters changes nothing geometric. Double both: w=(4,2)\vec{w} = (4,-2), b=2b = -2. Then sA=4s_A = 4 and w=25\lVert\vec{w}\rVert = 2\sqrt5, so the distance is 4/(25)=0.89444/(2\sqrt5) = 0.8944 — unchanged. The scores doubled and the distances did not, which is exactly the difference between the functional margin (Definition 4) and the geometric one (Definition 5).

Moving a point along the boundary direction changes nothing. Shift AA by (1,2)(1,2), the direction along the line, to get (3,3)(3,3): its score is 631=26 - 3 - 1 = 2, identical. Moving perpendicular to w\vec{w} cannot change the score, because w\vec{w} is what the score measures against.

Where this breaks

The distance formula requires w0\lVert\vec{w}\rVert \neq 0. At w=0\vec{w} = \vec{0} the score is the constant bb for every input, the “boundary” is empty or all of R2\R^2, and the division is undefined. That is not a pathological corner: it is the state a perceptron is initialised in, which is why the first update of I.2.B02 has margin exactly zero and the rule must treat 0\le 0 rather than <0< 0 as a mistake.

Variation

Keep w\vec{w} and change bb from 1-1 to 5-5. Predict, before computing, which points change class — then verify, and state in one sentence what the bias does geometrically.

Problem I.2.B02

Running the learning rule to convergence

numeric▲▲△

Exact integers throughout; no rounding is needed.

STATEMENT

Run the perceptron learning rule on a separable four-point set until it stops changing. Show every weight update and every margin you tested, including the ones that produced no change.

GIVEN

Four labelled points, presented cyclically in this order:

(1,1) ⁣:+1,(2,0) ⁣:+1,(1,0) ⁣:1,(0,1) ⁣:1(1,1)\!:\,+1, \qquad (2,0)\!:\,+1, \qquad (-1,0)\!:\,-1, \qquad (0,-1)\!:\,-1

Initialise w=(0,0)\vec{w} = (0,0) and b=0b = 0. The rule (Definition 7): if y(w,x+b)0y(\langle\vec{w},\vec{x}\rangle + b) \le 0, set ww+yx\vec{w} \leftarrow \vec{w} + y\vec{x} and bb+yb \leftarrow b + y; otherwise do nothing.

FIND

The sequence of parameter states, the total number of updates, and the final (w,b)(\vec{w}, b).

STRATEGY

Test the margin of every point in order, updating immediately when one is non-positive rather than at the end of a pass. Convergence is declared when a whole pass produces no update — which is the only stopping condition the rule has.

SOLUTION

Epoch 1.

Point (1,1)(1,1), y=+1y=+1. Margin =1(0+0+0)=0= 1\cdot(0 + 0 + 0) = 0. Non-positive, so this counts as a mistake even though nothing is strictly misclassified. Update:

w=(0,0)+(+1)(1,1)=(1,1),b=0+1=1\vec{w} = (0,0) + (+1)(1,1) = (1,1), \qquad b = 0 + 1 = 1

Point (2,0)(2,0), y=+1y=+1. Margin =1((1)(2)+(1)(0)+1)=3>0= 1\cdot\big((1)(2) + (1)(0) + 1\big) = 3 > 0. Correct — no change.

Point (1,0)(-1,0), y=1y=-1. Margin =(1)((1)(1)+(1)(0)+1)=(1)(0)=0= (-1)\cdot\big((1)(-1) + (1)(0) + 1\big) = (-1)(0) = 0. Non-positive again. Update:

w=(1,1)+(1)(1,0)=(1+1,  1+0)=(2,1),b=11=0\vec{w} = (1,1) + (-1)(-1,0) = (1+1,\; 1+0) = (2,1), \qquad b = 1 - 1 = 0

Point (0,1)(0,-1), y=1y=-1. Margin =(1)((2)(0)+(1)(1)+0)=(1)(1)=1>0= (-1)\cdot\big((2)(0) + (1)(-1) + 0\big) = (-1)(-1) = 1 > 0. Correct.

Epoch 2. With w=(2,1)\vec{w} = (2,1), b=0b = 0:

PointyyMarginAction
(1,1)(1,1)+1+1+3+3none
(2,0)(2,0)+1+1+4+4none
(1,0)(-1,0)1-1+2+2none
(0,1)(0,-1)1-1+1+1none

A complete pass with no update. The rule has converged.

Reading the updates. Both updates came from a margin of exactly zero, not from a confidently wrong prediction. The rule is not waiting to be badly wrong; it moves whenever a point is not strictly on the right side. That choice is what makes the zero-initialised first step happen at all.

Answer

w=[21],b=0,2 updates\vec{w} = \begin{bmatrix} 2 \\ 1\end{bmatrix}, \qquad b = 0, \qquad \textbf{2 updates}

The final boundary is 2x1+x2=02x_1 + x_2 = 0, and every point has a functional margin of at least 11.

Check — numeric · i-2-b02-perceptron-updates.py
margin = y * (w[0] * x[0] + w[1] * x[1] + b)
if margin <= 0:
    w = [w[0] + y * x[0], w[1] + y * x[1]]; b += y

Prints both updates, the four clean margins of epoch 2, and converged after 2 updates.

Executed in CI. The digits above are the digits it printed.

Check — sanity

Every point is now correctly classified. The four margins in epoch 2 are +3,+4,+2,+1+3, +4, +2, +1, all strictly positive. That is the definition of having separated the data, checked directly rather than inferred from the algorithm stopping.

The bound is respected. Here R=maxxi=2R = \max\lVert\vec{x}_i\rVert = 2 (from (2,0)(2,0)). The final geometric margin is miniyisi/w=1/5=0.4472\min_i y_i s_i / \lVert\vec{w}\rVert = 1/\sqrt5 = 0.4472. Novikoff’s bound (I.2.4) gives (R/γ)2=(2/0.4472)2=20(R/\gamma)^2 = (2/0.4472)^2 = 20. Two updates is comfortably under twenty, as a valid upper bound requires.

Each update moves toward the point that caused it. After the first update w=(1,1)\vec{w} = (1,1), which points at (1,1)(1,1) — the very example that triggered it. That is what ww+yx\vec{w} \leftarrow \vec{w} + y\vec{x} means geometrically, and it is the whole intuition of the rule.

Where this breaks

Convergence in two updates depended on the presentation order. Present the same four points as (1,0),(0,1),(1,1),(2,0)(-1,0), (0,-1), (1,1), (2,0) and the trace differs, though the theorem still caps the total. What no order changes is the bound: I.2.T1 holds for every order, which is what makes it a theorem about the data rather than about the shuffling.

Variation

Add a fifth point (0.2,0.1)(0.2, 0.1) with label 1-1. The set is still separable but the margin is much smaller. Predict the effect on the mistake bound before running the rule, then run it and count.

Problem I.2.B03

Proving the mistake bound

proof▲▲▲

Symbolic throughout.

STATEMENT

Prove Novikoff’s theorem: on separable data the perceptron makes at most (R/γ)2(R/\gamma)^2 mistakes. State every assumption where it is used, and say at the end which step each assumption was load-bearing for.

GIVEN

A dataset {(xi,yi)}\{(\vec{x}_i, y_i)\} with yi{1,+1}y_i \in \{-1,+1\} and xiR\lVert\vec{x}_i\rVert \le R for all ii. Assume separability: there exists a unit vector u\vec{u}, u=1\lVert\vec{u}\rVert = 1, with

yiu,xiγ>0for every iy_i \langle \vec{u}, \vec{x}_i \rangle \ge \gamma > 0 \quad\text{for every } i

The perceptron starts at w0=0\vec{w}_0 = \vec{0} and updates wk+1=wk+yixi\vec{w}_{k+1} = \vec{w}_k + y_i\vec{x}_i on the kk-th mistake. The bias is absorbed by appending a constant 11 to every xi\vec{x}_i, so only w\vec{w} appears.

FIND

An upper bound on kk, the total number of updates, in terms of RR and γ\gamma only.

STRATEGY

Track one scalar from below and another from above. The inner product u,wk\langle\vec{u},\vec{w}_k\rangle can only grow, and wk2\lVert\vec{w}_k\rVert^2 cannot grow fast. Cauchy–Schwarz then traps kk between them. The whole proof is the observation that a quantity growing linearly cannot stay below one growing as a square root forever.

SOLUTION

Step 1 — the lower bound on the projection. Suppose the kk-th mistake is on example (xi,yi)(\vec{x}_i, y_i). Take the inner product of the update with u\vec{u}, using linearity:

u,wk=u,wk1+yixi=u,wk1+yiu,xi\langle \vec{u}, \vec{w}_{k} \rangle = \langle \vec{u}, \vec{w}_{k-1} + y_i\vec{x}_i \rangle = \langle \vec{u}, \vec{w}_{k-1} \rangle + y_i\langle \vec{u}, \vec{x}_i \rangle

By the separability assumption the last term is at least γ\gamma. So each mistake advances the projection by at least γ\gamma:

u,wk  u,wk1+γ\langle \vec{u}, \vec{w}_{k} \rangle \ \ge\ \langle \vec{u}, \vec{w}_{k-1} \rangle + \gamma

Starting from w0=0\vec{w}_0 = \vec{0}, whose projection is 00, induction over kk mistakes gives

\langle \vec{u}, \vec{w}_{k} \rangle \ \ge\ k\gamma \tag{lower}

Where the assumption entered: separability, and only there.

Step 2 — the upper bound on the norm. Expand the squared norm of the same update:

wk2=wk1+yixi2=wk12+2yiwk1,xi+yi2xi2\lVert \vec{w}_{k}\rVert^2 = \lVert \vec{w}_{k-1} + y_i\vec{x}_i \rVert^2 = \lVert \vec{w}_{k-1}\rVert^2 + 2y_i\langle \vec{w}_{k-1}, \vec{x}_i\rangle + y_i^2\lVert \vec{x}_i\rVert^2

Now use the two facts available. First, the update happened because the example was a mistake, so its margin was non-positive: yiwk1,xi0y_i\langle\vec{w}_{k-1},\vec{x}_i\rangle \le 0, and the cross term can only help. Second, yi2=1y_i^2 = 1 and xi2R2\lVert\vec{x}_i\rVert^2 \le R^2. Therefore

wk2  wk12+R2\lVert \vec{w}_{k}\rVert^2 \ \le\ \lVert \vec{w}_{k-1}\rVert^2 + R^2

and by induction from w0=0\lVert\vec{w}_0\rVert = 0:

wk2  kR2sowk  Rk(upper)\lVert \vec{w}_{k}\rVert^2 \ \le\ kR^2 \qquad\text{so}\qquad \lVert \vec{w}_{k}\rVert \ \le\ R\sqrt{k} \tag{upper}

Where the assumptions entered: the bounded radius, and — crucially — the fact that updates occur only on mistakes. Take that away and the cross term is unsigned, and the bound fails.

Step 3 — squeeze with Cauchy–Schwarz. For any vectors, u,wuw\langle \vec{u}, \vec{w}\rangle \le \lVert\vec{u}\rVert\,\lVert\vec{w}\rVert (Inner products, norms and cosine similarity 0.LA.03), and u=1\lVert\vec{u}\rVert = 1. Chaining the two bounds:

kγ (lower) u,wk  wk (upper) Rkk\gamma \ \overset{\text{(lower)}}{\le}\ \langle \vec{u}, \vec{w}_k\rangle \ \le\ \lVert\vec{w}_k\rVert \ \overset{\text{(upper)}}{\le}\ R\sqrt{k}

Step 4 — solve for kk. From kγRkk\gamma \le R\sqrt{k}, and since k>0k > 0, divide by k\sqrt{k}:

kγRkRγk(Rγ)2\sqrt{k}\,\gamma \le R \quad\Longrightarrow\quad \sqrt{k} \le \frac{R}{\gamma} \quad\Longrightarrow\quad k \le \left(\frac{R}{\gamma}\right)^{2}

\blacksquare

What made it work. The projection grows linearly in the number of mistakes, while the norm grows only as k\sqrt{k}. A vector cannot have its shadow on a unit direction outrun its own length, so the two rates must collide, and the collision point is the bound.

Answer

k  (Rγ)2k \ \le\ \left(\frac{R}{\gamma}\right)^{2}

A dimensionless count. Note what it does not contain: the number of examples nn, the dimension dd, and the presentation order. The bound holds for a million points in a million dimensions arriving adversarially.

Check — sanity

Dimensional consistency. RR and γ\gamma are both lengths — RR a norm, γ\gamma a projection onto a unit vector — so R/γR/\gamma is dimensionless and so is its square. A bound on a count must be dimensionless, and this one is.

It reproduces the run of I.2.B02. There R=2R = 2 and γ=0.4472\gamma = 0.4472, giving k20k \le 20. The observed count was 22. A valid upper bound must be at least the truth, and 2202 \le 20.

Both monotonicities are right. Larger RR (data further from the origin) raises the bound; larger γ\gamma (an easier separation) lowers it. If the inequality had come out the other way round in either variable, a sign was lost.

The degenerate case behaves. If the data is already separated at the start, no mistake occurs, k=0k = 0, and 0(R/γ)20 \le (R/\gamma)^2 holds for any positive right-hand side.

Where this breaks

Every step of Step 2 used updates occur only on mistakes. Change the rule to update on every example regardless of margin and the cross term 2yiwk1,xi2y_i\langle\vec{w}_{k-1},\vec{x}_i\rangle is no longer 0\le 0; the norm can grow faster than RkR\sqrt k, and the squeeze collapses. This is why the perceptron is not gradient descent on a smooth loss: it is a rule that acts only on its own errors, and the proof depends on exactly that.

Separability is load-bearing in Step 1 alone, but its failure is total: without some u\vec{u} achieving margin γ>0\gamma > 0, there is no lower bound at all, kk is unbounded, and the algorithm cycles forever (I.2.X07).

Variation

Suppose w00\vec{w}_0 \neq \vec{0}. Redo Steps 1 and 2 carrying the initial terms, and show the bound becomes k(R+w0)2/γ2k \le \big(R + \lVert\vec{w}_0\rVert\big)^2/\gamma^2 up to constants. Then say why the theorem is usually stated from zero.

Problem I.2.B04

Four inequalities that cannot all hold

counterexample▲▲△

Exact; the argument is algebraic.

STATEMENT

Prove that no perceptron computes XOR on {0,1}2\{0,1\}^2, by writing the four requirements as inequalities in (w1,w2,b)(w_1, w_2, b) and showing they are jointly inconsistent.

GIVEN

The XOR function:

x1x_1x2x_2target
000
011
101
110

A perceptron predicts 11 when w1x1+w2x2+b>0w_1x_1 + w_2x_2 + b > 0 and 00 otherwise.

FIND

Either parameters that work, or a proof that none exist.

STRATEGY

Substitute each of the four inputs to turn the truth table into four linear inequalities. Then add pairs of them to derive a contradiction — the point being that this is ordinary algebra, not an appeal to geometry or intuition.

SOLUTION

Step 1 — write the four requirements. Substituting each row:

(0,0)0:b0(1)(0,1)1:w2+b>0(2)(1,0)1:w1+b>0(3)(1,1)0:w1+w2+b0(4)\begin{aligned} (0,0) \to 0: &\qquad b \le 0 &&(1)\\ (0,1) \to 1: &\qquad w_2 + b > 0 &&(2)\\ (1,0) \to 1: &\qquad w_1 + b > 0 &&(3)\\ (1,1) \to 0: &\qquad w_1 + w_2 + b \le 0 &&(4) \end{aligned}

Step 2 — add (2) and (3). Adding two strict inequalities of the same direction is legal and preserves strictness:

w1+w2+2b>0(5)w_1 + w_2 + 2b > 0 \qquad (5)

Step 3 — combine with (4). From (4), w1+w2bw_1 + w_2 \le -b. Substituting that upper bound into (5):

0<w1+w2+2b(b)+2b=b0 < w_1 + w_2 + 2b \le (-b) + 2b = b

so b>0b > 0.

Step 4 — the contradiction. Step 3 concludes b>0b > 0. Requirement (1) says b0b \le 0. Both cannot hold. Therefore no (w1,w2,b)(w_1, w_2, b) satisfies all four, and no perceptron computes XOR. \blacksquare

Why this is stronger than “training fails”. Nothing here mentions an algorithm, an initialisation, a learning rate, or a quantity of data. The inequalities describe every perceptron that could ever exist. The failure is a property of the hypothesis class F\mathcal{F} of Definition 3 in Chapter I.1 — XOR is simply not in it.

The geometric reading. The two positive points (0,1)(0,1) and (1,0)(1,0) lie on one diagonal of the unit square; the two negatives (0,0)(0,0) and (1,1)(1,1) lie on the other. The diagonals cross. Any straight line separating one diagonal’s endpoints must pass between them, and therefore also separates the other diagonal’s endpoints — putting one negative on each side. The algebra above is that picture with the geometry removed.

Answer

No such perceptron exists. Requirements (2) and (3) force b>0b > 0 while requirement (1) forces b0b \le 0.

The obstruction is exactly one dimension of freedom short: adding a third feature x1x2x_1x_2 makes the four points linearly separable in R3\R^3, with (w1,w2,w3,b)=(1,1,2,0.5)(w_1, w_2, w_3, b) = (1, 1, -2, -0.5) solving it.

Check — sanity

The proposed three-dimensional solution works. With the extra feature x3=x1x2x_3 = x_1x_2 and (1,1,2,0.5)(1, 1, -2, -0.5):

InputScorePredictedTarget
(0,0,0)(0,0,0)0.5-0.500
(0,1,0)(0,1,0)+0.5+0.511
(1,0,0)(1,0,0)+0.5+0.511
(1,1,1)(1,1,1)1+120.5=0.51 + 1 - 2 - 0.5 = -0.500

All four correct. So the impossibility really is about the representation and not about XOR being hard.

Three of the four are satisfiable. Drop requirement (4) and (w1,w2,b)=(1,1,0.5)(w_1,w_2,b) = (1,1,-0.5) satisfies (1), (2), (3). This confirms the contradiction genuinely needs all four rows, rather than arising from a slip in transcribing one of them.

AND and OR are fine. AND: (1,1,1.5)(1,1,-1.5). OR: (1,1,0.5)(1,1,-0.5). So the perceptron is not simply weak — it computes fourteen of the sixteen Boolean functions of two variables. XOR and its negation are the exceptions.

Where this breaks

The proof needs the exact convention that a score of 00 predicts class 00. If ties were resolved the other way, requirement (1) becomes b<0b < 0 and requirement (4) becomes w1+w2+b<0w_1 + w_2 + b < 0 — both strict — and the same addition still yields b>0b > 0 against b<0b < 0. The contradiction survives the convention change, which is worth checking rather than assuming: a proof that depended on the tie-breaking rule would be a proof about the rule, not about XOR.

Variation

Show that parity on three bits — output 11 when an odd number of inputs are 11 — is also not computable by a perceptron, by exhibiting a contradiction among its eight inequalities. Then state the general pattern.

Problem I.2.B05

What happens to the bound as the margin closes

limit▲▲△

Counts are exact; ratios to 4 d.p.

STATEMENT

Analyse the mistake bound (R/γ)2(R/\gamma)^2 as γ0+\gamma \to 0^{+} at fixed RR. Tabulate it, state the limit, and say precisely what the theorem still guarantees for near-separable data — and what it stops guaranteeing.

GIVEN

Novikoff’s bound k(R/γ)2k \le (R/\gamma)^2 from I.2.B03, with the radius fixed at R=1R = 1 and the geometric margin γ\gamma shrinking.

FIND

The bound at γ{0.5, 0.1, 0.01, 103, 106}\gamma \in \{0.5,\ 0.1,\ 0.01,\ 10^{-3},\ 10^{-6}\}; the limit as γ0+\gamma \to 0^{+}; and the status of the guarantee at γ=0\gamma = 0 exactly.

STRATEGY

Evaluate, then take the limit, then separate two cases that look continuous but are not: γ\gamma small and γ\gamma zero.

SOLUTION

Step 1 — tabulate. With R=1R = 1 the bound is simply γ2\gamma^{-2}.

γ\gamma(R/γ)2(R/\gamma)^2
0.50.544
0.10.1100100
0.010.0110,00010{,}000
10310^{-3}10610^{6}
10610^{-6}101210^{12}

Step 2 — the rate. The bound is quadratic in 1/γ1/\gamma, so halving the margin quadruples the work:

(R/(γ/2))2(R/γ)2=4\frac{(R/(\gamma/2))^2}{(R/\gamma)^2} = 4

This is worth stating as a rule of thumb, because it is the reason near-separable problems are qualitatively different from comfortably separable ones rather than merely slower.

Step 3 — the limit.

limγ0+(Rγ)2=+\lim_{\gamma \to 0^{+}} \left(\frac{R}{\gamma}\right)^{2} = +\infty

Step 4 — the two cases, which are not the same.

Case γ>0\gamma > 0, however small. The data is separable. The theorem applies and the algorithm does terminate, in finitely many updates. The guarantee is intact — it has merely become useless in practice, because a finite number can exceed any budget anyone has.

Case γ=0\gamma = 0 exactly. There is no separating u\vec{u} with positive margin. Step 1 of the proof in I.2.B03 has no lower bound to offer, the squeeze never closes, and the conclusion is not weakened but withdrawn. The algorithm does not terminate at all.

The distinction matters because the two cases are indistinguishable from outside. A run that has made 10610^6 updates might be at γ=103\gamma = 10^{-3} and one update from finishing, or at γ=0\gamma = 0 and never finishing. The perceptron has no test that separates them, and no amount of patience resolves it.

Answer

limγ0+(Rγ)2=+,quadratically: halving γ quadruples the bound.\lim_{\gamma \to 0^{+}} \left(\frac{R}{\gamma}\right)^{2} = +\infty, \qquad \text{quadratically: halving } \gamma \text{ quadruples the bound.}

For every γ>0\gamma > 0 the guarantee holds and termination is certain. At γ=0\gamma = 0 the guarantee does not degrade — it disappears, and so does termination.

Check — sanity

The tabulated values satisfy the quadratic rule. From γ=0.1\gamma = 0.1 to γ=0.01\gamma = 0.01 the margin fell by 10×10\times and the bound rose by 100×100\times, as γ2\gamma^{-2} requires.

The bound is scale-invariant in the right way. Doubling every coordinate doubles both RR and γ\gamma, leaving R/γR/\gamma — and therefore the bound — unchanged. A mistake count must not depend on the units the data is measured in, and it does not.

It matches the worked run. I.2.B02 had R=2R = 2, γ=0.4472\gamma = 0.4472, bound 2020, actual 22. Consistent.

Where this breaks

The whole analysis holds RR fixed while γ\gamma moves. In real data they move together: adding a distant outlier raises RR and usually lowers γ\gamma, so the bound degrades on both factors at once and the quadratic rate above is an underestimate of the damage. The single-variable limit is a clean statement about the formula, not a forecast about a dataset.

Variation

Fix γ=0.1\gamma = 0.1 and let RR \to \infty instead. Compare the rate of blow-up with the γ0\gamma \to 0 case, and say which of the two a practitioner can actually control.

Problem I.2.B06

What one perceptron costs, and what a bank of them costs

complexity▲△△

Exact counts.

STATEMENT

Count the parameters and the forward FLOPs of a single perceptron over dd inputs, then extend to a one-vs-rest bank of KK perceptrons. Evaluate at two realistic sizes.

GIVEN

A perceptron y^=sign(w,x+b)\hat{y} = \mathrm{sign}(\langle\vec{w},\vec{x}\rangle + b) with wRd\vec{w} \in \R^{d}. Count a multiply and an add as one FLOP each; ignore the sign, which is one comparison.

FIND

Parameters and FLOPs as functions of dd; then of dd and KK; then numbers at (d,K)=(784,10)(d, K) = (784, 10) and (4096,1000)(4096, 1000).

STRATEGY

Count the arithmetic in the definition literally rather than recalling a rule. The inner product is the only expensive part, and everything else is O(1)O(1).

SOLUTION

Step 1 — one perceptron, parameters. The weight vector holds dd numbers and the bias holds one:

Nparams=d+1N_{\text{params}} = d + 1

Step 2 — one perceptron, FLOPs. The inner product j=1dwjxj\sum_{j=1}^{d} w_j x_j needs dd multiplications and d1d-1 additions. Adding the bias is one more addition, bringing the additions to dd. Total:

NFLOPs=d+d=2dN_{\text{FLOPs}} = d + d = 2d

The convenient way to remember this: one multiply–add per parameter, and there are dd weights doing real work. That rule of thumb survives all the way to Chapter II.8, where the 2N2N per token in C6NDC \approx 6ND is the same count.

Step 3 — a bank of KK. One-vs-rest trains KK independent perceptrons, one per class, each over the same dd inputs. Nothing is shared:

Nparams=K(d+1),NFLOPs=2dKN_{\text{params}} = K(d+1), \qquad N_{\text{FLOPs}} = 2dK

Equivalently the bank is one matrix WRd×K\mat{W} \in \R^{d\times K} and one bias row, so the forward pass is a single (1×d)(d×K)(1\times d)(d\times K) product — the same object as a linear layer in Chapter I.5, arrived at from the other direction.

Step 4 — evaluate.

ddParamsFLOPs
223344
7847847857851,5681{,}568
409640964,0974{,}0978,1928{,}192
ddKKParamsFLOPs
78478410107,8507{,}85015,68015{,}680
40964096100010004,097,0004{,}097{,}0008,192,0008{,}192{,}000

Answer

one:N=d+1,F=2dbank of K:N=K(d+1),F=2dK\text{one:}\quad N = d + 1,\quad F = 2d \qquad\qquad \text{bank of }K:\quad N = K(d+1),\quad F = 2dK

At d=784d = 784, K=10K = 10: 7,8507{,}850 parameters and 15,68015{,}680 FLOPs per example. At d=4096d = 4096, K=1000K = 1000: 4,097,0004{,}097{,}000 parameters and 8,192,0008{,}192{,}000 FLOPs.

Check — numeric · i-2-b06-perceptron-cost.py
params = d + 1
flops  = 2 * d
print(f"d={d} K={K} params {K * (d + 1)} flops {2 * d * K}")

Prints all six rows above.

Executed in CI. The digits above are the digits it printed.

Check — sanity

FLOPs are about twice the parameters. 8,192,000/4,097,000=1.99958{,}192{,}000 / 4{,}097{,}000 = 1.9995, approaching 22 as dd grows because the bias becomes negligible. If your ratio were near 11 or near 44, a multiply or an add went missing.

Both scale linearly in each variable separately. Ten times the classes gives ten times the cost; ten times the input width gives ten times the cost. There is no interaction term, because a one-vs-rest bank shares nothing between classes.

The d=784d = 784, K=10K = 10 figure is checkable against a known object. That is MNIST with a linear classifier, and 7,8507{,}850 is the familiar parameter count for exactly that model. Landing on a number that appears in the literature is weak evidence, but it is evidence.

Where this breaks

The count assumes the input is dense. For sparse x\vec{x} with only sds \ll d non-zeros, the inner product costs 2s2s rather than 2d2d, and a bag-of-words perceptron over a 10610^6-word vocabulary with 2020 words per document costs 4040 FLOPs and not two million. The parameter count is unchanged — the memory is still d+1d+1 — which is why sparsity helps compute far more than it helps storage.

Variation

Add the backward pass. The perceptron rule updates d+1d+1 numbers on a mistake and none otherwise. Compute the expected cost per example when a fraction pp of examples cause updates, and compare with a gradient method that updates on every example.

Problem I.2.B07

The learning rule is a subgradient step

symbolic▲▲△

Symbolic throughout.

STATEMENT

The perceptron rule looks like a hand-made heuristic. Show that it is exactly one step of subgradient descent, with learning rate 11, on a specific loss — and identify that loss.

GIVEN

The perceptron loss for one example, sometimes called the hinge-at-zero loss:

(w,b)=max(0,  y(w,x+b))\ell(\vec{w}, b) = \max\big(0,\; -y(\langle\vec{w},\vec{x}\rangle + b)\big)

and the update rule of Definition 7: on a mistake, ww+yx\vec{w} \leftarrow \vec{w} + y\vec{x} and bb+yb \leftarrow b + y.

FIND

The subgradient of \ell with respect to w\vec{w} and bb in both regimes, and the resulting descent step at η=1\eta = 1.

STRATEGY

Split on the sign of the margin, differentiate each branch, and compare the resulting step with the rule. The function is not differentiable at the join, which is why “subgradient” rather than “gradient” — and that is a feature worth naming rather than glossing.

SOLUTION

Step 1 — name the inner quantity. Let m=y(w,x+b)m = y(\langle\vec{w},\vec{x}\rangle + b) be the functional margin, so =max(0,m)\ell = \max(0, -m).

Step 2 — the case m>0m > 0 (correct). Then m<0-m < 0, the maximum is achieved by the constant 00, and \ell is identically zero in a neighbourhood. A locally constant function has zero derivative:

w=0,/b=0\nabla_{\vec{w}}\,\ell = \vec{0}, \qquad \partial \ell/\partial b = 0

The descent step changes nothing. This matches the rule’s “otherwise do nothing” exactly.

Step 3 — the case m<0m < 0 (wrong). Then =m=y(w,x+b)\ell = -m = -y(\langle\vec{w},\vec{x}\rangle + b), which is affine in the parameters and so differentiable. Using ww,x=x\nabla_{\vec{w}}\langle\vec{w},\vec{x}\rangle = \vec{x} (The derivative of a linear map 0.MC.04):

w=yx,b=y\nabla_{\vec{w}}\,\ell = -y\vec{x}, \qquad \frac{\partial \ell}{\partial b} = -y

Step 4 — take a descent step. Gradient descent moves against the gradient (Gradient descent 0.OP.02), with η=1\eta = 1:

wwηw=w(1)(yx)=w+yx\vec{w} \leftarrow \vec{w} - \eta\,\nabla_{\vec{w}}\ell = \vec{w} - (1)(-y\vec{x}) = \vec{w} + y\vec{x}bbηb=b(1)(y)=b+yb \leftarrow b - \eta\,\frac{\partial\ell}{\partial b} = b - (1)(-y) = b + y

These are the update rule, term for term.

Step 5 — the join at m=0m = 0. At exactly zero the function has a kink and no derivative exists. The subdifferential is the whole interval between the two one-sided slopes, {αyx:α[0,1]}\{\,-\alpha\, y\vec{x} : \alpha \in [0,1]\,\}. Any element is a legal subgradient step; the perceptron picks α=1\alpha = 1, which is why it updates on m0m \le 0 rather than m<0m < 0. That convention, which looked arbitrary in I.2.B02, is a choice of subgradient.

Answer

The perceptron rule is subgradient descent on =max(0,y(w,x+b))\ell = \max(0, -y(\langle\vec{w},\vec{x}\rangle + b)) with η=1\eta = 1, taking the subgradient α=1\alpha = 1 at the kink:

w={0m>0yxm0ww+yx  on a mistake.\nabla_{\vec{w}}\,\ell = \begin{cases} \vec{0} & m > 0\\ -y\vec{x} & m \le 0 \end{cases} \qquad\Longrightarrow\qquad \vec{w} \leftarrow \vec{w} + y\vec{x} \ \text{ on a mistake.}

Check — sanity

The loss is zero exactly when the rule is idle. =0    m0    \ell = 0 \iff m \ge 0 \iff no update. The two descriptions of “nothing to do here” agree, which they must if one is to be a restatement of the other.

The loss is convex. It is the maximum of two affine functions of the parameters, and a pointwise maximum of convex functions is convex (Convexity 0.OP.01). So the perceptron is minimising a convex objective — and yet it still fails to terminate on XOR, because the minimum of that objective is not zero there. Convexity buys a well-posed problem, not a useful answer.

The learning rate is genuinely irrelevant here. Any η>0\eta > 0 gives ww+ηyx\vec{w} \leftarrow \vec{w} + \eta y\vec{x}, which rescales w\vec{w} but not the sign of any score when starting from 0\vec{0} — so the sequence of predictions is identical. This is unique to the perceptron and stops being true the moment Chapter I.4 introduces a loss with curvature.

Where this breaks

The identification needs η=1\eta = 1 and w0=0\vec{w}_0 = \vec{0} for the sequence to match; only the form of the step matches for general η\eta. It also needs the loss to be this one. The very similar hinge loss max(0,1m)\max(0, 1 - m) — which demands a margin of at least 11 rather than merely a correct sign — gives the same-shaped update but a different idle region, and converges to a maximum-margin solution the perceptron never finds. One symbol’s difference in the loss changes what the algorithm is for.

Variation

Derive the update for the hinge loss max(0,1m)\max(0, 1 - m) and state, in one sentence, what the extra 11 changes about which hyperplanes are stationary points.

Exercises

Every one has a published solution. A hidden solution is a solution; a missing one is an abandonment.

I.2.X01A second hyperplanenumeric▲△△

For w=(1,3)\vec{w} = (1, 3) and b=6b = -6, write the boundary equation and give the class and signed distance of P=(3,2)P=(3,2), Q=(0,0)Q=(0,0), R=(6,1)R=(6,1), S=(1,0)S=(1,0). Then say which point the boundary would reach first if bb were increased.

Hint

Increasing bb raises every score by the same amount, so it slides the boundary without rotating it. The first point to flip is the one with the smallest positive score.

Solution

Boundary. x1+3x26=0x_1 + 3x_2 - 6 = 0, i.e. x2=2x1/3x_2 = 2 - x_1/3.

Norm. w=1+9=10=3.1623\lVert\vec{w}\rVert = \sqrt{1 + 9} = \sqrt{10} = 3.1623.

PointScoreDistanceClass
P=(3,2)P=(3,2)3+66=+33 + 6 - 6 = +3+0.9487+0.9487+1+1
Q=(0,0)Q=(0,0)0+06=60 + 0 - 6 = -61.8974-1.89741-1
R=(6,1)R=(6,1)6+36=+36 + 3 - 6 = +3+0.9487+0.9487+1+1
S=(1,0)S=(1,0)1+06=51 + 0 - 6 = -51.5811-1.58111-1

Which flips first. Decreasing bb lowers every score equally. PP and RR tie at +3+3, so both flip together at b=9b = -9. Going the other way, increasing bb raises scores: SS at 5-5 flips before QQ at 6-6, at b=1b = -1.

What that shows. The bias is a translation, not a rotation. It moves the boundary along its own normal w\vec{w} and cannot change which side of the plane through the origin a point sits on. Any question that requires re-orienting the boundary needs w\vec{w} to change, and the bias cannot help.

The tie between PP and RR is worth noting: two points at different locations can be equidistant from a boundary, because distance to a hyperplane collapses dd coordinates into one number.

I.2.X03A bank of perceptrons is one matrixshape▲△△

Write a one-vs-rest bank of KK perceptrons over dd inputs as a single matrix operation on a batch of BB examples. Give every shape, and state what the bank can and cannot express that KK separately-stored perceptrons can.

Hint

Stack the KK weight vectors as columns, not rows. Then check the shapes meet under the row-major convention.

Solution

The stacking. Place the kk-th perceptron’s weight vector in the kk-th column:

W=[w1w2wK]Rd×K,bR1×K\mat{W} = \begin{bmatrix} \vec{w}_1 & \vec{w}_2 & \cdots & \vec{w}_K \end{bmatrix} \in \R^{d\times K}, \qquad \vec{b} \in \R^{1\times K}

The forward pass. With a batch XRB×d\mat{X} \in \R^{B\times d}, one row per example:

S=XW+bRB×K\mat{S} = \mat{X}\mat{W} + \vec{b} \in \R^{B\times K}

Shapes: (B×d)(d×K)(B×K)(B\times d)(d\times K) \to (B\times K), then the bias broadcasts along the batch axis. Entry SikS_{ik} is the score of example ii under perceptron kk, so one product computes BKBK scores.

Every shape.

ObjectShape
X\mat{X}B×dB \times d
W\mat{W}d×Kd \times K
b\vec{b}1×K1 \times K (broadcast to B×KB \times K)
S\mat{S}B×KB \times K
predictionB×1B \times 1, by argmax\arg\max along the KK axis

What is identical. The arithmetic. KK separate perceptrons compute exactly these numbers; the matrix form only arranges them so one call does the work of KK. The parameter count K(d+1)K(d+1) from I.2.B06 is unchanged.

What the matrix form adds. Nothing expressive — and that is the point worth taking. It is the same hypothesis class. What it adds is:

A single decision rule. Separate perceptrons each answer yes or no, and can answer yes twice or never. Taking argmax\arg\max over the score row forces exactly one answer, which the KK independent models do not.

The object of Chapter I.5. XW+b\mat{X}\mat{W} + \vec{b} is precisely a linear layer. A bank of perceptrons and the first layer of an MLP are the same computation; only the training rule and what sits after it differ. Arriving at the linear layer from the perceptron rather than from the definition is worth doing once, because it makes clear that depth — not the layer — is the new idea.

What neither can express. Any function requiring a non-linear boundary, XOR included (I.2.B04). Stacking KK hyperplanes side by side gives KK hyperplanes, not a curve.

I.2.X02Twenty-four orders, six answersnumeric▲▲△

Take the separable set (1,1) ⁣: ⁣+1(1,1)\!:\!+1, (2,0) ⁣: ⁣+1(-2,0)\!:\!+1, (2,1) ⁣: ⁣1(2,1)\!:\!-1, (1,0) ⁣: ⁣1(1,0)\!:\!-1. Run the perceptron on every one of the 4!=244! = 24 presentation orders. Report the range of update counts and the number of distinct final hyperplanes, then reconcile that variability with I.2.T1.

Hint

The theorem bounds the mistakes for every order. It does not claim they are equal.

Solution

The outcomes.

UpdatesFinal (w,b)(\vec{w}, b)Orders
5(2,+2), +1(-2, +2),\ +16
8(3,+2), +2(-3, +2),\ +25
9(2,+2), +1(-2, +2),\ +12
10(2,+3), 0(-2, +3),\ 04
12(3,+2), +2(-3, +2),\ +25
14(3,+4), 0(-3, +4),\ 02

The range: 5 to 14 updates, a factor of 2.82.8, on byte-identical data. And four distinct hyperplanes, so the algorithm’s output — not merely its runtime — depends on the order the examples happened to arrive in.

Reconciling with the theorem. Here

R=maxixi=(2,1)=5=2.2361,γ=0.3536R = \max_i \lVert\vec{x}_i\rVert = \lVert(2,1)\rVert = \sqrt5 = 2.2361, \qquad \gamma = 0.3536

so the bound is

k(2.23610.3536)2=40k \le \left(\frac{2.2361}{0.3536}\right)^2 = 40

Every one of the 24 orders lands under 40, and the worst — 14 — uses about a third of the allowance. There is no contradiction: I.2.T1 is an upper bound holding uniformly over orders, and a uniform bound must accommodate the worst one. It never claimed the orders agree.

What this costs in practice. Three things follow, and none of them is obvious from the theorem alone.

The solution is not a function of the data. Two researchers running the same algorithm on the same file get different classifiers. Any claim about “the” perceptron solution for a dataset is under-specified until the order is fixed.

The margin achieved is arbitrary. The order giving 5 updates lands on (2,2,1)(-2,2,1), with geometric margin miniyisi/w=1/(22)=0.3536\min_i y_i s_i/\lVert\vec{w}\rVert = 1/(2\sqrt2) = 0.3536 — which happens to be optimal here. Other orders land on hyperplanes with worse margins. Nothing in the rule prefers the good one.

Shuffling is not a nuisance to be eliminated. It is the reason the algorithm terminates quickly on average, and the reason a single run’s update count is not a measurement of anything about the data. Chapter VIII.4 makes that argument quantitative for training runs generally.

Compare with the set of I.2.B02, where all 24 orders give 2 updates and the same weights. Order-independence happens; it is not guaranteed.

I.2.X04What rescaling the parameters does and does not changesymbolic▲▲△

Let c>0c > 0 and replace (w,b)(\vec{w}, b) by (cw,cb)(c\vec{w}, cb). Determine what happens to the decision boundary, the predicted labels, the functional margin, and the geometric margin. Then say which of the two margins can appear in a theorem and why.

Hint

Write each quantity out with the cc in place and see whether it cancels.

Solution

The boundary. The set {x:cw,x+cb=0}\{\vec{x} : c\langle\vec{w},\vec{x}\rangle + cb = 0\} equals {x:c(w,x+b)=0}\{\vec{x} : c(\langle\vec{w},\vec{x}\rangle + b) = 0\}, and since c0c \neq 0 this is the same set as before. Unchanged.

The labels. sign(cs)=sign(s)\mathrm{sign}(c\,s) = \mathrm{sign}(s) for c>0c > 0. Unchanged.

The functional margin. m=y(w,x+b)m = y(\langle\vec{w},\vec{x}\rangle + b) becomes

y(cw,x+cb)=cmy\big(c\langle\vec{w},\vec{x}\rangle + cb\big) = c\,m

Scaled by cc. It can be made as large as one likes by taking cc large, without moving the boundary an inch.

The geometric margin. Both numerator and denominator scale:

y(cw,x+cb)cw=cmcw=mw\frac{y(c\langle\vec{w},\vec{x}\rangle + cb)}{\lVert c\vec{w}\rVert} = \frac{c\,m}{c\lVert\vec{w}\rVert} = \frac{m}{\lVert\vec{w}\rVert}

Unchanged. The cc cancels exactly.

Which one can appear in a theorem. Only the geometric margin. A theorem whose hypothesis was “the functional margin is at least γ\gamma” would be vacuous: given any separating hyperplane, multiply its parameters by c=γ/mminc = \gamma / m_{\min} and the hypothesis is satisfied, with no change to the classifier at all. The statement would constrain nothing.

This is why I.2.T1 is stated with a unit vector u\vec{u}, and why the proof in I.2.B03 uses u=1\lVert\vec{u}\rVert = 1 in its Cauchy–Schwarz step. Normalising is not tidiness; it is what makes the quantity a property of the data rather than of an arbitrary scaling.

A consequence worth carrying forward. Any quantity you plan to threshold, compare across models, or put in a bound must be checked for this kind of spurious freedom first. The same question recurs for attention scores in II.3 — where the answer is that dk\sqrt{d_k} removes a scale that would otherwise grow with width — and for logits in IV.8, where a temperature does exactly what cc does here.

One asymmetry. For c<0c < 0 the boundary is still unchanged but every label flips, since sign(cs)=sign(s)\mathrm{sign}(cs) = -\mathrm{sign}(s). So the invariance is to positive rescaling only, and the sign of w\vec{w} carries the orientation — which side is which.

I.2.X05Absorbing the bias, and what it costsproof▲▲△

The proof in I.2.B03 assumed the bias had been absorbed by appending a constant 11 to every input. Prove the two formulations are equivalent, then show that absorption changes RR — and quantify the effect on the mistake bound.

Hint

Appending a coordinate changes the norm of every input. Compute the new RR.

Solution

Step 1 — the construction. Define augmented vectors

x~=[x1]Rd+1,w~=[wb]Rd+1\tilde{\vec{x}} = \begin{bmatrix}\vec{x} \\ 1\end{bmatrix} \in \R^{d+1}, \qquad \tilde{\vec{w}} = \begin{bmatrix}\vec{w} \\ b\end{bmatrix} \in \R^{d+1}

Step 2 — the scores agree. By the definition of the inner product,

w~,x~=j=1dwjxj+b1=w,x+b\langle \tilde{\vec{w}}, \tilde{\vec{x}}\rangle = \sum_{j=1}^{d} w_j x_j + b \cdot 1 = \langle \vec{w},\vec{x}\rangle + b

Identical for every x\vec{x}, so the two models classify identically and have the same decision boundary in Rd\R^d. \blacksquare

Step 3 — the updates agree. The augmented update is w~w~+yx~\tilde{\vec{w}} \leftarrow \tilde{\vec{w}} + y\tilde{\vec{x}}. Reading off its components: the first dd give ww+yx\vec{w} \leftarrow \vec{w} + y\vec{x}, and the last gives bb+y1=b+yb \leftarrow b + y\cdot 1 = b + y. Exactly Definition 7. So the two runs are the same run, step for step.

Step 4 — what it costs. The norm grows:

x~=x2+1\lVert\tilde{\vec{x}}\rVert = \sqrt{\lVert\vec{x}\rVert^2 + 1}

so the radius becomes R~=R2+1\tilde{R} = \sqrt{R^2 + 1}, and the bound becomes

kR2+1γ~2k \le \frac{R^2 + 1}{\tilde\gamma^{\,2}}

Step 5 — quantify. For the data of I.2.B02, R=2R = 2 so R~=5=2.2361\tilde R = \sqrt5 = 2.2361, and R2+1=5R^2 + 1 = 5 against R2=4R^2 = 4: the numerator rises by 25%25\%. For data scaled so that R=10R = 10, the rise is 101/100101/100, or 1%1\%. The penalty is severe only when the data lies close to the origin, because then the appended 11 is comparable to the data’s own scale.

The practical reading. This is the same phenomenon as feature scaling. The constant feature has magnitude 11 regardless of the units the other features are measured in, so if the inputs are in millimetres the bias feature is negligible and if they are normalised to unit variance it is not. A bound that changes when you change units is telling you the units matter — and here they do.

Why the theorem is stated in the absorbed form anyway. With bb separate, Step 2 of I.2.B03 acquires a bb-update term that must be bounded separately, and the proof becomes two interleaved inductions instead of one. Absorption buys a clean proof at the price of a slightly looser constant, which is the usual trade and worth naming as such rather than hiding.

I.2.X06Fourteen of sixteencounterexample▲▲△

There are 1616 Boolean functions of two variables. Determine how many a perceptron computes, exhibit weights for AND, OR and NAND, and prove that XNOR joins XOR among the failures. Then state the general characterisation.

Hint

XNOR is the negation of XOR. What does negating a perceptron’s output do to its parameters?

Solution

Three that work.

Function(w1,w2,b)(w_1, w_2, b)Check
AND(1,1,1.5)(1, 1, -1.5)(1,1) ⁣: ⁣0.5>0(1,1)\!:\!0.5>0; all others 0.5\le -0.5
OR(1,1,0.5)(1, 1, -0.5)(0,0) ⁣: ⁣0.5(0,0)\!:\!-0.5; all others 0.5\ge 0.5
NAND(1,1,1.5)(-1, -1, 1.5)the negation of AND, so negate every parameter

Verify NAND fully: (0,0)1.5>01(0,0) \to 1.5 > 0 \to 1; (0,1)0.5>01(0,1) \to 0.5 > 0 \to 1; (1,0)0.5>01(1,0) \to 0.5 > 0 \to 1; (1,1)0.500(1,1) \to -0.5 \le 0 \to 0. Correct.

XNOR fails. Negating a perceptron’s output is achieved by negating all its parameters, since sign(s)=sign(s)\mathrm{sign}(-s) = -\mathrm{sign}(s) away from zero. So if some (w,b)(\vec{w}, b) computed XNOR, then (w,b)(-\vec{w}, -b) would compute XOR — which I.2.B04 proved impossible. Hence no perceptron computes XNOR either. \blacksquare

Directly, its four inequalities are the mirror image:

b>0,w2+b0,w1+b0,w1+w2+b>0b > 0, \quad w_2 + b \le 0, \quad w_1 + b \le 0, \quad w_1 + w_2 + b > 0

Adding the middle two gives w1+w2+2b0w_1 + w_2 + 2b \le 0; the fourth gives w1+w2>bw_1 + w_2 > -b; together b+2b<0-b + 2b < 0, so b<0b < 0, contradicting the first.

The count. Sixteen functions, two failures, so a perceptron computes 14\mathbf{14} of them.

The general characterisation. A Boolean function is computable by a perceptron exactly when its true points and false points are linearly separable as subsets of the hypercube {0,1}n\{0,1\}^n — such functions are called threshold functions. XOR and XNOR are the only two of the sixteen whose true and false sets interleave along both diagonals of the square.

The scale of the failure grows. For nn inputs there are 22n2^{2^n} Boolean functions but only about 2n22^{n^2} threshold functions. At n=2n = 2 the ratio is 14/16=87.5%14/16 = 87.5\%; at n=4n = 4 it is roughly 2162^{16} against 65,53665{,}536 total — already under 2%2\%; and it collapses to zero as nn grows. The perceptron looks adequate on two variables and is arbitrarily inadequate in general, which is the honest version of the story usually told with XOR alone.

The escape, once more. Every one of these functions is computable by a network with one hidden layer, because AND, OR and NOT are, and those three compose to give all of them. That is the constructive half of the universal approximation story, and it is Chapter I.5’s business.

I.2.X07The run that never endscounterexample▲▲△

The first assumption of this chapter is separability. Remove it: run the perceptron on the four XOR points and describe the resulting behaviour precisely. Show the state is periodic, state the period, and explain why no stopping rule based on watching the run can distinguish this from slow convergence.

Hint

Track (w,b)(\vec{w}, b) after each full epoch rather than after each update, and look for a repeat.

Solution

The data. XOR with labels in {1,+1}\{-1, +1\}:

(0,0) ⁣: ⁣1,(0,1) ⁣: ⁣+1,(1,0) ⁣: ⁣+1,(1,1) ⁣: ⁣1(0,0)\!:\!-1, \quad (0,1)\!:\!+1, \quad (1,0)\!:\!+1, \quad (1,1)\!:\!-1

The trace. Starting from w=(0,0)\vec{w} = (0,0), b=0b = 0 and cycling in that order:

StepPointyyMarginNew (w,b)(\vec{w}, b)
1(0,0)(0,0)1-100(0,0), 1(0,0),\ -1
2(0,1)(0,1)+1+11-1(0,1), 0(0,1),\ 0
3(1,0)(1,0)+1+100(1,1), +1(1,1),\ +1
4(1,1)(1,1)1-13-3(0,0), 0(0,0),\ 0

After one complete epoch the state is (w,b)=((0,0),0)(\vec{w}, b) = ((0,0), 0)exactly the initial state. The next epoch reproduces the same four updates, and so on forever.

The period is 4 updates, or one epoch. The state space visited is the four-element cycle above, and no other state is ever reached.

Training accuracy along the way. At the best point in the cycle — state ((1,1),+1)((1,1), +1) — the predictions are 20 20 12 61 79 80 81 98 33 100 204 250 395 398 399 400 7010,0)\to+1, 20 20 12 61 79 80 81 98 33 100 204 250 395 398 399 400 7010,1)\to+1, 20 20 12 61 79 80 81 98 33 100 204 250 395 398 399 400 7011,0)\to+1, 20 20 12 61 79 80 81 98 33 100 204 250 395 398 399 400 7011,1)\to+1: two of four, 50%50\%. At the worst it is also two of four. Accuracy never exceeds chance and never improves, which is consistent with I.2.B04: no hyperplane does better than three of four here, and this cycle does not even reach that.

Why no stopping rule helps. Consider what an observer can measure:

Update count. Rises without bound here. But it also rises without bound — up to (R/γ)2(R/\gamma)^2 — for a separable set with a tiny margin. At any finite moment the two look identical.

Accuracy. Flat at 50%50\% here. But a separable set with a small margin can also sit at a plateau for a long time before the final updates resolve it.

State repetition. This is detectable — the state returns to ((0,0),0)((0,0),0) — and in principle one could hash the visited states. But the state space is unbounded in general, the cycle length can be exponential, and for non-separable data with real-valued inputs exact repetition may never occur even though the run never terminates.

The conclusion the chapter needs. Separability is not a technical convenience in I.2.T1; it is the whole hypothesis. Without it there is no theorem, no bound, and no way to tell from inside the algorithm that anything is wrong. The algorithm’s silence is the failure mode.

What was done about it historically. Two answers, both later. The pocket algorithm keeps the best-so-far weights and returns those, converting non-termination into a heuristic. More importantly, replacing the perceptron loss with one that has a finite minimum on non-separable data — logistic loss, Chapter I.4 — makes the question disappear: gradient descent on a bounded-below convex objective always has somewhere to go.

I.2.X08A guarantee that stops being worth anythinglimit▲▲△

The second assumption is that γ\gamma is bounded away from zero. Construct a family of two-point datasets indexed by ε\varepsilon, separable for every ε>0\varepsilon > 0, whose mistake bound diverges as ε0\varepsilon \to 0. Give the bound at three values, and state the exact sense in which the theorem is still true when it has become useless.

Hint

Two points, one of each class, moved toward each other.

Solution

The family. In one dimension, for ε>0\varepsilon > 0:

x+=+ε  with y=+1,x=ε  with y=1x_+ = +\varepsilon \ \text{ with } y = +1, \qquad x_- = -\varepsilon \ \text{ with } y = -1

with the bias absorbed, so the augmented points are (ε,1)(\varepsilon, 1) and (ε,1)(-\varepsilon, 1).

Separable for every ε>0\varepsilon > 0. The unit vector u=(1,0)\vec{u} = (1, 0) gives margins yiu,x~i=εy_i\langle\vec{u},\tilde{\vec{x}}_i\rangle = \varepsilon for both points. Positive, so separable.

The radius. x~=ε2+1\lVert\tilde{\vec{x}}\rVert = \sqrt{\varepsilon^2 + 1}, so R=ε2+11R = \sqrt{\varepsilon^2 + 1} \to 1 as ε0\varepsilon \to 0.

The margin. γ=ε\gamma = \varepsilon (the direction above is optimal here by symmetry).

The bound.

k(ε2+1ε)2=ε2+1ε2=1+1ε2k \le \left(\frac{\sqrt{\varepsilon^2+1}}{\varepsilon}\right)^{2} = \frac{\varepsilon^2 + 1}{\varepsilon^2} = 1 + \frac{1}{\varepsilon^2}

ε\varepsilonγ\gammaBound
0.10.10.10.1101101
0.010.010.010.0110,00110{,}001
0.0010.0010.0010.0011,000,0011{,}000{,}001

limε0+kmax=+\lim_{\varepsilon \to 0^{+}} k_{\max} = +\infty

In what sense the theorem is still true. Precisely this: for every fixed ε>0\varepsilon > 0 the number of mistakes is finite, the algorithm does terminate, and the bound is correct. Nothing about the theorem weakens as ε\varepsilon shrinks. What changes is only its usefulness — a true bound of 10610^{6} tells a practitioner nothing they can act on.

The distinction that matters. Compare two failures:

Here. The guarantee holds and is uninformative. More patience genuinely suffices.

In I.2.X07. The guarantee does not hold at all. No amount of patience suffices.

These are different situations with the same observable signature — a run that has not finished. That is the honest content of “the theorem assumes γ>0\gamma > 0”: not that small margins are difficult, but that the boundary case γ=0\gamma = 0 is a different mathematical object and the algorithm cannot see which side of it the data lies on.

Where this recurs. The same shape appears throughout Elementa: a bound that is true, tight, and vacuous. Positivity in causal inference (VI.7) fails the same way — the estimator remains unbiased as the propensity approaches zero while its variance diverges. Recognising “true but vacuous” as a category, distinct from “false”, is one of the habits this book is trying to build.

I.2.X09The subgradient at the joingradient▲▲△

Using the perceptron loss of I.2.B07, compute the loss and a subgradient at three states: one with positive margin, one with negative margin, and one exactly at the kink. Then explain why the third case has a set of valid answers and which element the perceptron rule chooses.

Hint

At the kink, the two branches disagree about the slope. Every value between them is legitimate.

Solution

The three states. All with w\vec{w}, bb and (x,y)(\vec{x}, y) as given:

Casem=y(w,x+b)m = y(\langle\vec{w},\vec{x}\rangle+b)\ellw\nabla_{\vec{w}}/b\partial/\partial b
w=(2,1),b=0,x=(1,1),y=+1\vec{w}=(2,1), b=0, \vec{x}=(1,1), y=+1+3+300(0,0)(0, 0)00
w=(2,1),b=0,x=(1,1),y=+1\vec{w}=(2,1), b=0, \vec{x}=(-1,-1), y=+13-333(+1,+1)(+1, +1)1-1
w=(1,1),b=0,x=(1,1),y=+1\vec{w}=(1,-1), b=0, \vec{x}=(1,1), y=+10000(1,1)(-1, -1)1-1

Case 1, m>0m > 0. =max(0,3)=0\ell = \max(0, -3) = 0, and \ell is identically zero nearby, so every derivative is zero. The rule is idle, and gradient descent agrees.

Case 2, m<0m < 0. =max(0,3)=3\ell = \max(0, 3) = 3. On this branch =y(w,x+b)\ell = -y(\langle\vec{w},\vec{x}\rangle+b), so w=yx=(+1)(1,1)=(+1,+1)\nabla_{\vec{w}}\ell = -y\vec{x} = -(+1)(-1,-1) = (+1,+1) and /b=y=1\partial\ell/\partial b = -y = -1. The descent step is ww(+1,+1)=(1,0)\vec{w} \leftarrow \vec{w} - (+1,+1) = (1, 0), which equals w+yx=(2,1)+(1,1)\vec{w} + y\vec{x} = (2,1) + (-1,-1). The rule and the gradient step coincide.

Case 3, m=0m = 0. =max(0,0)=0\ell = \max(0, 0) = 0, but the function is not differentiable here. Approaching from m>0m > 0 the slope is 0\vec{0}; approaching from m<0m < 0 it is yx=(1,1)-y\vec{x} = (-1,-1). The subdifferential is the whole segment joining them:

w={αyx:α[0,1]}={(α,α):α[0,1]}\partial_{\vec{w}}\,\ell = \{\,-\alpha\, y\vec{x} : \alpha \in [0, 1]\,\} = \{\,(-\alpha, -\alpha) : \alpha \in [0,1]\,\}

Every element is a valid subgradient, and each gives a different — equally legitimate — algorithm.

Which one the perceptron picks. α=1\alpha = 1, the most aggressive element. That is exactly what the condition “update when m0m \le 0” encodes: at the kink it behaves as though the example were wrong. Choosing α=0\alpha = 0 instead would give “update only when m<0m < 0”, and starting from w=0\vec{w} = \vec{0} that algorithm never takes a first step at all, because the initial margin of every example is exactly zero.

Why this matters beyond the perceptron. ReLU has precisely this structure at the origin (Chapter I.3), and every framework silently chooses a subgradient there — usually 00, occasionally 12\tfrac12 or 11. The choice is invisible in the loss curve and can change which units are dead. Knowing that a kink means a set rather than a value, and that someone has chosen for you, is worth carrying into every chapter that uses a piecewise-linear activation.

I.2.X10A mistake bound is not a generalisation boundproof▲▲▲

The chapter’s open exercise. I.2.T1 bounds mistakes on the training sequence. Chapter I.1 was about the gap between training and expected error. Determine precisely what — if anything — I.2.T1 says about performance on unseen data. Argue both directions, and state the strongest true claim you can.

Hint

An online mistake bound counts errors on examples the algorithm has not yet seen when it makes them. Ask whether that is the same as a test set.

Solution

The naive reading, and why it is wrong. “The perceptron makes at most (R/γ)2(R/\gamma)^2 mistakes, so it will make at most that many on new data.” False. The bound is over the whole sequence presented, and once the algorithm has converged it makes no further updates — but it can still be arbitrarily wrong on a point drawn from a region the training sequence never visited. The theorem is silent about points outside the sequence.

The counterexample. Take a separable set concentrated in a small region and a test point far away. The perceptron returns some separating hyperplane — whichever the order happened to produce (I.2.X02). Four different hyperplanes were possible there, all with zero training error, and they disagree on distant points. So training error zero, and test error entirely undetermined by the theorem.

The direction that does work. There is a real result, and it is subtler than the naive reading. In the online setting, each mistake is made on an example before the algorithm has seen it. So the bound really does count errors on unseen data — it just counts them cumulatively over one pass rather than in expectation over a distribution.

This gives the strongest true claim:

On a single pass through nn separable examples, the perceptron’s error rate is at most (R/γ)2/n(R/\gamma)^2 / n, and every one of those errors was on an example not previously seen.

At n=106n = 10^6 with R/γ=10R/\gamma = 10, that is at most 100100 mistakes, an online error rate below 0.01%0.01\% — a genuine statement about unseen data, obtained without any distributional assumption at all.

Converting online to distributional. An online-to-batch conversion does exist: if the examples are i.i.d., then averaging the hypotheses visited during the run — or picking one uniformly at random — yields expected risk at most M/nM/n, where MM is the mistake bound. Note what this costs:

It needs the i.i.d. assumption, which the mistake bound itself never used. The online bound holds against an adversarial order; the batch guarantee does not.

It is a guarantee about the averaged hypothesis, not about the final one. The last hyperplane, which is what anyone would actually deploy, is not covered.

The honest summary in three lines.

  1. As stated, I.2.T1 says nothing about a held-out test set.
  2. Read as an online bound, it says something genuinely strong and assumption-free about errors on unseen examples during the pass.
  3. Turning that into a statement about a deployed classifier requires i.i.d. sampling and a change to which hypothesis is returned — two additions, both easy to forget.

Why this belongs in Book I. It is the first instance of a pattern the rest of Elementa repeats: a theorem about the algorithm is quietly read as a theorem about the model. The distinction between what an optimisation guarantee covers and what a generalisation guarantee covers is the same one that separates I.1.T1’s “unbiased for fixed θ\theta” from “unbiased for the θ\theta you chose”. Get it wrong here, with three lines of algebra in view, and it will be got wrong later where nothing is in view.