One missing dimension can wreck your model, and NumPy will never warn you.
Hook
A Silent Shape Bug
One missing dimension can wreck your model, and NumPy will never warn you.
- No error raised
- Wrong answer anyway
Intro
Broadcasting
Meet broadcasting, NumPy's quiet superpower.
- NumPy's quiet superpower
Demo
Column Meets Row
Take a column of three numbers, and a row of four. Add them. NumPy stretches both into a three by four grid. No loops.
- (3,1) plus (1,4)
- Result is (3,4)
- No loops, no copies
Rule
The One Rule
Here is the one rule. Compare shapes from the right. Each pair must be equal, or one of them must be one.
- Compare from the right
- Equal, or one
Check Each Pair
Three by one against one by four? Four versus one works. Three versus one works. Done.
- 1 versus 4: works
- 3 versus 1: works
Bug
The Silent Bug
Now the silent bug. Predictions shaped n, labels shaped n by one. Subtract them, and you get no error. You get an n by n grid, and a wrong loss.
- (n,) minus (n,1)
- Gives (n,n), not (n,)
- Loss is quietly wrong
Fix
Fix: Match Shapes
The fix is simple. Reshape so both are n by one, and always print your shapes.
- Reshape to (n,1)
- Always print shapes
Outro
Next: Lesson 31
Lesson thirty-one builds NumPy arrays from the ground up. Follow along.
- NumPy arrays from scratch