Shape Debugging
Shape debugging is the practice of finding where the expected tensor shape stops matching the actual tensor shape.
It is one of the most useful habits in deep learning.
Write the expected shape first
Before writing or reading a layer, write the shape you expect.
X: (batch, input_features)
W: (input_features, output_features)
b: (output_features)
output: (batch, output_features)
Then compare each real tensor to that expectation.
Check the first wrong place
When a later operation fails, the cause may be earlier. Do not only inspect the line that produced the error. Trace the shapes from the input forward.
input -> reshape -> layer -> activation -> loss
At each step, ask:
- What shape did I expect?
- What shape did I get?
- Which axis changed?
- Was that change intended?
Common shape mistakes
The most common mistakes are simple:
- using feature count where batch size belongs;
- flattening too early;
- forgetting that a bias follows output features;
- using a weight matrix with the wrong input feature size;
- mixing two axis conventions without noticing.
Shape debugging is not separate from understanding. It is understanding made testable.
X has shape (10, 5). A layer uses W with shape (6, 3). Which number causes the mismatch?
Compute it first, then check your number.
HintCompare inner dimensions
For XW, compare the second dimension of X with the first dimension of W.
SolutionWork it out
The product tries to multiply (10, 5) x (6, 3). The inner dimensions are
5 and 6. The 6 in W says the layer expects 6 input features, but
X has only 5.
X has shape (10, 5). The layer should produce 3 output features. What shape should W have?
Compute it first, then check your number.
HintInput features first
Use (input_features, output_features).
SolutionWork it out
X has 5 input features per example. The layer should produce 3 output
features, so W should have shape (5, 3).