borch

Foundations · 3

Broadcasting & shapes

Broadcasting lets an operation between different shapes work without copying data — a row is stretched across a matrix, a scalar across everything. Getting the shapes right is most of the battle in real code.

A row stretches across the rows

Adding a shape-[3] vector to a [2,3] matrix adds it to every row. Trailing axes line up from the right.

keepdim: the shape you subtract back

A reduction drops the axis it collapses. keepdim=true keeps it as size 1 — the shape you need to broadcast the result back against the original.

Seeing a broadcast: the sum table

A column [8,1] plus a row [1,8] broadcasts to an [8,8] grid — every cell is row + column. The smooth gradient is the broadcast made visible: neither input was copied, yet every pair was combined.

Your turn: centre the columns

Centring means subtracting each column's mean so every column ends up averaging zero. The block subtracts the wrong mean — fix it to the per-column mean.

What to remember

If a block above errors, that is worth knowing. Every example on this page runs against the same library the tests run against — nothing here is a screenshot. Press Reset to get the original code back.