Tensor Shapes and Broadcasting
Interactive lab
Try it: Tensor Shapes and Broadcasting
How NumPy decides result shapes for element-wise addition with broadcasting, np.dot, reshape and transpose on small tensors of rank 0 to 3, including the exact errors it raises for incompatible shapes.
How it works
- Broadcasting: prepend size-1 broadcast axes to the lower-rank shape so the trailing dimensions line up.
- Compare dimensions right to left: equal ones stay, a 1 is stretched, anything else raises ValueError.
- np.dot sums over A's last axis and B's only (1-D) or second-to-last axis; those sizes must match.
- reshape regroups the same values in row-major order (one -1 may be inferred); transpose reverses the axes.
Default run (5 steps): A has shape (2, 3) (rank 2), B has shape (3,) (rank 1). Operation: A + B. … Step 2: the stretched tensors are added element by element, giving shape (2, 3). For example result[1, 2] = 36.
Simplified: Tensors are filled with 1, 2, 3, ... (A) and 10, 20, 30, ... (B); ranks 0 to 3 with dimensions 1 to 4. Only these four operations are modelled.
Educational simulation
Loading the simulation…