Md. Asif Uddin
Problem I.1.B02

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 B=32B = 32 examples, each a flattened 28×2828\times 28 image:

StageOperationDeclared shape
0input X\mat{X}32×78432 \times 784
1XW(1)\mat{X}\mat{W}^{(1)}W(1):784×256\mat{W}^{(1)}: 784 \times 256
2  W(2)\cdot\;\mat{W}^{(2)}W(2):256×128\mat{W}^{(2)}: 256 \times 128
3  W(3)\cdot\;\mat{W}^{(3)}W(3):256×64\mat{W}^{(3)}: 256 \times 64
4  W(4)\cdot\;\mat{W}^{(4)}W(4):64×10\mat{W}^{(4)}: 64 \times 10

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 (a×b)(b×c)(a×c)(a \times b)(b \times c) \to (a \times c): the inner pair must match and then vanishes, while the outer pair survives.

after 0:32×784after 1:(32×784)(784×256)32×256after 2:(32×256)(256×128)32×128after 3:(32×128)(256×64)undefined\begin{aligned} \text{after 0:}\quad & 32 \times 784 \\ \text{after 1:}\quad & (32\times784)(784\times256) \to 32 \times 256 \\ \text{after 2:}\quad & (32\times256)(256\times128) \to 32 \times 128 \\ \text{after 3:}\quad & (32\times \mathbf{128})(\mathbf{256}\times64) \to \text{undefined} \end{aligned}

Step 2 — name the failure. At stage 3 the running width is 128128, because stage 2 projected down to 128128. But W(3)\mat{W}^{(3)} expects an input of width 256256. The inner dimensions are 128128 and 256256; 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 128128; the width produced was intended to be 6464 and nothing contradicts that. So

W(3):128×64\mat{W}^{(3)} : 128 \times 64

and the pipeline then runs 32×784256128641032\times784 \to 256 \to 128 \to 64 \to 10.

Step 4 — count the parameters. Weights only, so the sum of the products:

784×256=200,704256×128=32,768128×64=8,19264×10=640total=242,304\begin{aligned} 784 \times 256 &= 200{,}704 \\ 256 \times 128 &= 32{,}768 \\ 128 \times 64 &= 8{,}192 \\ 64 \times 10 &= 640 \\ \hline \text{total} &= 242{,}304 \end{aligned}

Answer

Stage 3 fails: the running width is 128128 but W(3)\mat{W}^{(3)} declares an input width of 256256. The corrected shape is W(3):128×64\mat{W}^{(3)} : 128 \times 64, and the pipeline then holds 242,304\mathbf{242{,}304} weights.

Adding biases of widths 256,128,64,10256, 128, 64, 10 would contribute a further 458458, for 242,762242{,}762 parameters in total.

Check — sanity

The first layer dominates, as it must. 200,704200{,}704 of 242,304242{,}304 weights — 82.8%82.8\% — sit in the first matrix, because it is the only one touching the 784784-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 7842561286410784 \to 256 \to 128 \to 64 \to 10, 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 256256-wide trunk throughout, the error is in W(2)\mat{W}^{(2)}, which should have been 256×256256\times256, 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.

Draws on