Width costs a square, depth costs a line
complexity▲▲△Exact integer counts; derived quantities to 3 d.p.
STATEMENT
Put an image into an MLP and read the bill. Count the parameters and the forward FLOPs of a wide shallow network and a narrower deep one on the same input, say where the cost sits in each, and compare both against the parameter count of a convolution doing a comparable job.
GIVEN
The input is a image flattened to numbers, and the output is classes.
Wide: one hidden layer of units, widths .
Deep: six hidden layers of units, widths .
Count a multiply–add as two FLOPs, and one fp32 parameter as four bytes.
FIND
For each network: total parameters, fp32 size, forward FLOPs per example, and the share of the parameters held by the input-facing matrix. Then the parameter count of a convolution taking 3 channels to 64, against the dense layer that maps inputs to outputs.
STRATEGY
Use (I.5.2) and keep the layers separate rather than summing early. The whole lesson lives in the ratio between layers, and a total hides it.
SOLUTION
Step 1 — the wide network.
FLOPs: , or GFLOP for a single image.
The first matrix is of the model.
Step 2 — the deep network. The first layer is . The five inner layers are . The head is .
and the first matrix is of it.
Step 3 — read the two numbers against each other. The deep network has six times the hidden layers and a quarter of the parameters. Nothing subtle happened: the wide network’s hidden width is four times larger, and it multiplies against , so one matrix grew fourfold. Adding five entire layers of width 1024 cost parameters — of that model, and less than one percent of the wide one.
This is (I.5.2) with the letters read carefully. Width enters as a product of two adjacent widths, so doubling one width doubles one matrix and doubling both quadruples it. Depth enters as a sum over , so adding a layer adds one term.
Step 4 — what the density is buying. A convolution from 3 channels to 64 holds parameters and applies the same filter at every position. A dense layer taking a image ( numbers) to 64 outputs holds — times more, on a smaller image, to produce one vector rather than a feature map at every location.
The MLP is not paying for capacity. It is paying for the assumption that every input coordinate might interact with every unit, which for an image is false and known to be false. Chapter I.11 is the repair.
Answer
| parameters | fp32 | FLOP/example | first matrix | |
|---|---|---|---|---|
| wide, | GB | G | ||
| deep, | GB | G |
A convolution to 64 channels: parameters, against for the dense layer of the same output width — a factor of .
In both networks the input-facing matrix is essentially the whole model.
Check — numeric · i-5-b04-dense-cost.py
def mlp(widths):
return sum(widths[i] * widths[i + 1] + widths[i + 1]
for i in range(len(widths) - 1))Prints both totals, both GB figures, both first-matrix shares, the convolution comparison, and the square law for widths , , .
Executed in CI. The digits above are the digits it printed.
Check — sanity
The square law is visible in one column. A hidden layer of width holds parameters in its own matrix; holds ; holds . Each doubling of the width very nearly quadruples the count, and the small excess is the bias.
The FLOP count is twice the parameter count, minus the biases. For the wide network, . That is not a coincidence: a dense layer performs one multiply–add per weight per example, so forward FLOPs and parameters are locked together for MLPs. They come apart for convolutions, where one weight is used at every position.
The deep network is cheaper in FLOPs by the same ratio as in parameters. against . It must be, by the previous check.
Where this breaks
FLOPs are not time. The wide network’s single matrix is one large, efficient multiply; the deep one’s seven matrices are seven smaller launches with a dependency between each, so the deep network can be slower despite doing a quarter of the arithmetic. Which one wins depends on the batch size, on memory bandwidth, and on whether the layers are large enough to saturate the hardware — which is I.5.X08. Ranking two architectures by FLOPs alone is a well-behaved mistake: it is wrong in a direction you can predict, but it is still wrong.
Variation
Keep the total parameter count of the wide network fixed at roughly but spend it on a deep network of constant width. How wide can the layers be at depth 10, and what fraction of the budget does the first layer still take? The answer says something about why the first layer of an image model is never dense in practice.