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 inputs, then extend to a one-vs-rest bank of perceptrons. Evaluate at two realistic sizes.
GIVEN
A perceptron with . Count a multiply and an add as one FLOP each; ignore the sign, which is one comparison.
FIND
Parameters and FLOPs as functions of ; then of and ; then numbers at and .
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 .
SOLUTION
Step 1 — one perceptron, parameters. The weight vector holds numbers and the bias holds one:
Step 2 — one perceptron, FLOPs. The inner product needs multiplications and additions. Adding the bias is one more addition, bringing the additions to . Total:
The convenient way to remember this: one multiply–add per parameter, and there are weights doing real work. That rule of thumb survives all the way to Chapter II.8, where the per token in is the same count.
Step 3 — a bank of . One-vs-rest trains independent perceptrons, one per class, each over the same inputs. Nothing is shared:
Equivalently the bank is one matrix and one bias row, so the forward pass is a single product — the same object as a linear layer in Chapter I.5, arrived at from the other direction.
Step 4 — evaluate.
| Params | FLOPs | |
|---|---|---|
| Params | FLOPs | ||
|---|---|---|---|
Answer
At , : parameters and FLOPs per example. At , : parameters and 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. , approaching as grows because the bias becomes negligible. If your ratio were near or near , 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 , figure is checkable against a known object. That is MNIST with a linear classifier, and 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 with only non-zeros, the inner product costs rather than , and a bag-of-words perceptron over a -word vocabulary with words per document costs FLOPs and not two million. The parameter count is unchanged — the memory is still — which is why sparsity helps compute far more than it helps storage.
Variation
Add the backward pass. The perceptron rule updates numbers on a mistake and none otherwise. Compute the expected cost per example when a fraction of examples cause updates, and compare with a gradient method that updates on every example.