Log inStart free
LESSON 6.02 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Broadcasting

Watch two tensors of different shapes line up, stretch and combine.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 6.02 · BROADCAST BAYA (3, 1) · B (4,)
01
Stage 1 of 6 · Two tensors
A has shape (3, 1) and B has shape (4,). Broadcasting lets one operation join them.
Explain it simply

You see two small grids of numbers. A, the first grid, holds tens, and B, the second, holds ones. Their shapes are different, so the cells do not pair up yet. Broadcasting is the rule PyTorch uses to fit different shapes together.

Grid A has shape (3, 1). Grid B has shape (4,). The shape table above lists each size.

stored valuesstretched view, not copiedstridesresultshape error
dim 0dim 11020301234A(3, 1)B(4,)314two tensorsA (3, 1)B (4,)+=A: (3, 1) · B: (4,)dim 0dim 11020301234A(3, 1)B(4,)314two tensorsA (3, 1)B (4,)+=A: (3, 1) · B: (4,)
Toy: A of tens, (3, 1) or (3, 4); B of ones
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
A: (a0, a1)B: (b0, b1), (b1) or ()\sy{a}{data}{A}:\ (a_0,\ a_1) \qquad \sy{b}{data}{B}:\ (b_0,\ b_1),\ (b_1)\ \text{or}\ ()
A: (3,1)B: (4,)A:\ (3, 1) \qquad B:\ (4,)

Each tensor has a shape: its size along each dim.

Common mix-ups

MYTHA vector of length 3 lines up with the 3 rows.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01If A has shape (3, 4) and B has shape (3, 1), which tensor stretches?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.