# Silent broadcasting: why a falling loss can break your model
*Data & AI Institute Academy — Full Written Guide*

## Core Question
Why can a PyTorch training script print a smooth falling loss curve while quietly destroying a model that started at 100% accuracy?

## 1. The Silent Trap in Five Lines of Code
```python
import torch
x = torch.tensor([1.0, 2.0, 3.0, 4.0])
targets = torch.tensor([[1.0], [2.0], [3.0], [4.0]])
preds = w * x + b
loss = (preds - targets).pow(2).mean()
```

## 2. Live Verified PyTorch Capture
Executed live with `.venv/bin/python -B pytorch_ref_silent_traps.py  # PyTorch 2.15.0+cpu` (exit `0`):
```text
matched MSE=0.0000 | silent broadcast MSE=2.5000
step=  0  reported=2.5000  true_mse=0.0000  w=+1.0000  b=+0.0000
step= 50  reported=1.4895  true_mse=0.4713  w=+0.4073  b=+1.3025
step=100  reported=1.3030  true_mse=0.8240  w=+0.1916  b=+1.9367
step=200  reported=1.2526  true_mse=1.1466  w=+0.0424  b=+2.3753
fixed squeeze(-1) MSE=0.0000  shape=(4,)
```

## 3. Why Broadcasting Backfires Here
PyTorch broadcasting is helpful when adding a short bias row across a batch. When two tensors accidentally differ by one extra bracket—`shape (4,)` vs `shape (4, 1)`—PyTorch stretches both into a `4x4` grid of 16 pairs. Twelve of those pairs compare the wrong prediction against the wrong target.

## 4. The Production Fix
```python
assert preds.shape == targets.shape, (preds.shape, targets.shape)
loss = (preds - targets.squeeze(-1)).pow(2).mean()
```

## 5. Full Transcript (Mira & Kai)
- **MIRA** (00:00): A PyTorch training script can print a smooth falling error score while quietly destroying your model.
- **KAI** (00:07): Wait, isn't a falling error score the exact sign that a model is learning?
- **MIRA** (00:12): Usually, yes. First, picture how PyTorch holds data. Numbers sit in grids called tensors, and each grid has a shape counting its rows and columns.
- **KAI** (00:22): So how does one extra bracket turn four exact matches into an error score of two point five?
- **MIRA** (00:29): PyTorch broadcasts that tall column into a four by four grid, mixing twelve wrong pairs with four right ones. Over two hundred steps, reported loss falls while true error climbs.
- **MIRA** (00:42): Always assert matching shapes and squeeze extra axes before every loss call. We verified this live in PyTorch and saved the script for you.
