1. The Silent Trap in Five Lines of Code
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()
Wrapping each target value in its own bracket adds a second axis of size 1. PyTorch treats subtracting (4,) and (4, 1) as valid broadcasting instead of raising an error.
2. Real Captured Output (PYTORCH 2.15.0+CPU)
$ .venv/bin/python -B pytorch_ref_silent_traps.py # PyTorch 2.15.0+cpu
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. The Production Fix
assert preds.shape == targets.shape, (preds.shape, targets.shape)
loss = (preds - targets.squeeze(-1)).pow(2).mean()
4. Dialogue 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.