One conformability error in five stages
shape▲△△Shapes only; no rounding applies.
STATEMENT
A five-stage pipeline is specified below. Exactly one stage cannot be computed. Find it, say why, and state the shape the offending tensor must have for the pipeline to run.
GIVEN
A batch of examples, each a flattened image:
| Stage | Operation | Declared shape |
|---|---|---|
| 0 | input | |
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
Elementwise nonlinearities sit between the stages. They do not change shape, so they can be ignored for this question — which is itself worth noticing.
FIND
The failing stage, the reason, and the corrected shape of the offending weight matrix. Then the total parameter count once it is corrected, counting weights only.
STRATEGY
Propagate the shape forward one stage at a time. A matrix product needs the inner dimensions to agree, so at each stage compare the running width with the first dimension of the next weight matrix; the first disagreement is the error.
SOLUTION
Step 1 — propagate. Write the running shape after each stage. The rule is : the inner pair must match and then vanishes, while the outer pair survives.
Step 2 — name the failure. At stage 3 the running width is , because stage 2 projected down to . But expects an input of width . The inner dimensions are and ; they disagree, so the product is undefined.
Note the batch axis played no part. It is carried along untouched by every stage, which is why a shape error in a stack of dense layers is always a statement about widths and never about how many examples you fed in.
Step 3 — correct it. A weight matrix’s first dimension is the width it consumes and its second is the width it produces. The width consumed must be ; the width produced was intended to be and nothing contradicts that. So
and the pipeline then runs .
Step 4 — count the parameters. Weights only, so the sum of the products:
Answer
Stage 3 fails: the running width is but declares an input width of . The corrected shape is , and the pipeline then holds weights.
Adding biases of widths would contribute a further , for parameters in total.
Check — sanity
The first layer dominates, as it must. of weights — — sit in the first matrix, because it is the only one touching the -dimensional input. Any parameter count for a network with a wide input and a narrowing stack should be dominated by its first layer; if yours is not, recheck.
The corrected chain telescopes. Reading the widths in order gives , where each weight matrix’s second dimension is the next one’s first. That chain property is the shape rule restated, and it is the fastest way to eyeball a whole architecture.
Where this breaks
The diagnosis assumes the declared stage-2 output is correct and stage 3 is at fault. Nothing in the specification proves that. If the author intended a -wide trunk throughout, the error is in , which should have been , and the corrected parameter count is different. A shape error localises a contradiction; it does not tell you which side to change. That judgement needs the architecture’s intent, which lives outside the shapes.
Variation
Insert a skip connection adding the stage-1 output to the stage-3 output. State the new constraint this imposes on the widths, and say which of the two repairs above it rules out.