AI Grounds

Open AI Grounds on a desktop

These interactive lessons need a larger screen. Please continue on a desktop or laptop computer.

Guided discovery

Tensor Shape & Broadcasting Lab

Align the axes. Trace one output value.

Shape lists the sizes of an array’s axes; rank is the number of axes. This lab keeps rank 3. Axis −1 is last, −2 is middle and −3 is first. Each size can be an integer from 1 through 5.

A shape [2, 3, 1]
B shape [3, 3, 4]

Aligned axes, checked from the right

Equal sizes OR one size is 1. Every axis must pass. Each compatible pair below gives a candidate axis size; the final output exists only when no pair fails.

A [2, 3, 1], B [3, 3, 4]. Check order: −1, −2, −3.
AxisA sizeB sizeCandidate sizeRule
-1144A reuses index 0 (size 1 → 4)
-2333Same size
-323UnavailableIncompatible: unequal and neither is 1

Failed axes: -3. No output shape or output value exists.

Output shape

One output axis for each aligned pair, if all pairs pass.

Unavailable

Unequal non-singleton sizes block the operation

Repair each failed axis before tracing a value.

Output elements

Number of elements in the resulting array.

Unavailable

No resulting array

Elementwise C = A + B, not matrix multiplication.

Trace one output value

No output value exists until all aligned axes are compatible.

Axis −3 comparisons

All five possible B sizes for the current A axis −3 = 2. Either side may supply size 1.

B axis −3Candidate sizeRuleCurrent
12B reuses index 0 (size 1 → 2)
22Same size
3UnavailableIncompatible: unequal and neither is 1Selected
4UnavailableIncompatible: unequal and neither is 1
5UnavailableIncompatible: unequal and neither is 1
Missing leading axes and data reuse

In general, ranks can differ. Align from the right and treat missing leading axes as size 1. For A [3,1] and B [2,3,4], align A as [1,3,1]: all pairs pass, giving output [2,3,4]. This lab’s editable arrays retain three axes.

Broadcasting logically reuses source values without requiring tiled input copies. An addition result can allocate output memory; broadcasting does not make the result cost-free. It does not change stored input shapes. Size-0 axes are outside these controls.

A [2, 3, 1], B [3, 3, 4]. Incompatible shapes; no output value.