Shape Reasoning
Rules for predicting the shape of every operation before you run it.
The four rules
Predict every output shape with four checks: (1) matmul — inner dims must match and cancel, (a, k) @ (k, b) → (a, b); (2) broadcasting — align shapes from the right, each pair of axes must be equal or one of them 1; (3) reduction — a reduce over axis=i removes that axis (or keeps it as 1 with keepdims=True); (4) rank — (n,) is a 1-D vector, not the same as a row (1, n) or column (n, 1).
Matrix multiply & transpose
| Operation | Input shapes | Output shape |
|---|---|---|
|
|
|
|
| Error — inner dims must match |
|
|
|
|
|
|
Batched matmul |
|
|
Broadcasting & reductions
| Operation | Input shapes | Output shape |
|---|---|---|
Add row vector |
|
|
Add column vector |
|
|
Outer via broadcast |
|
|
Incompatible |
| Error — right-aligned axes clash |
|
|
|
|
|
|
|
|
|