A shape and parameter audit of a four-layer MLP
shape▲△△Exact integer counts; memory in decimal megabytes.
STATEMENT
Audit a classifier layer by layer: the shape of every matrix, the shape of every intermediate, the parameter count of each layer and of the whole model, and the memory that model occupies in fp32. Then answer the question the audit exists to answer — where is the money.
GIVEN
An MLP with widths
Four affine layers, ReLU after the first three, no activation on the head. One example at a time, so the input is a row .
FIND
A table of and shapes, the shape of each , per-layer and total parameter counts, the fp32 size, and the share of the total held by the first layer.
STRATEGY
Count with equation (I.5.2), one layer at a time. The two habits worth building are writing the bias into the count rather than waving it away, and checking that each layer’s output shape is the next layer’s input shape before adding anything up.
SOLUTION
Step 1 — shapes. A layer holds and , and the row passing through is .
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
Every inner dimension meets. The batch dimension is untouched throughout, which is what makes the same weights work for one example or ten thousand.
Step 2 — parameters, layer by layer. :
Step 3 — the total.
Step 4 — memory. At 4 bytes per fp32 parameter, bytes: MB, or MiB. Training with Adam multiplies that by roughly four, since the optimiser carries two moment estimates alongside the gradient — a point Chapter I.7 makes properly.
Step 5 — where the money is.
The first layer holds seven parameters in ten. It is not the widest layer and it is not doing the classification; it is simply the one facing the input, and the input is 784 numbers wide. This is the general shape of the bill: an MLP pays for its interface with the data.
Answer
| shape | parameters | |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| total |
bytes in fp32, which is MB. The first layer holds of the model.
Check — sanity
The chain of shapes closes. , with each layer’s output width equal to the next layer’s input width, and the final width equal to the number of classes. A single mismatch anywhere would make the model unrunnable, so this check is free and catches most typing errors.
The biases are a rounding error, and are still counted. of , or . They are cheap, which is a reason to keep them rather than a reason to omit them from the count — and I.5.X03 shows what dropping them costs.
The layer sizes fall as the widths multiply out. is close to , matching the products , , , . Parameters track the product of adjacent widths, exactly as (I.5.2) says.
Where this breaks
The count is a count of parameters, and parameters are only one of three things that occupy memory. Activations scale with the batch: at batch 1024 the intermediates alone are bytes, more than the model. Optimiser state scales with the parameters. Gradients match the parameters again. Quoting a parameter count as though it were a memory requirement is the most common way to underestimate what a model needs by an order of magnitude — and the ratio is worst exactly where it matters, which is during training rather than inference.
Variation
Re-audit with widths — a wider model with one hidden layer instead of three. Compare the totals before computing them, then check the prediction. Which is larger, and by how much?