Unit 06.00: Why images need spatial structure
Flatten a 12×12 image into 144 numbers and you have thrown away the single most useful fact about it: which pixels are next to which.
What flattening costs
A dense layer treats every input position as unrelated. A pattern learned in the top-left corner tells it nothing about the same pattern in the bottom-right — it would have to learn that separately, from separate examples.
A convolution applies the same small filter at every position, so a pattern learned anywhere is recognised everywhere:
import torch
from torch import nn
torch.manual_seed(0)
# A 12x12 image with a bright square somewhere in it.
def make_image(row, col):
img = torch.zeros(1, 12, 12)
img[0, row:row + 4, col:col + 4] = 1.0
return img
# Flattening throws away which pixels are neighbours. A dense layer must learn
# the shape separately at every position it can appear.
dense = nn.Sequential(nn.Flatten(), nn.Linear(144, 1))
conv = nn.Sequential(nn.Conv2d(1, 1, kernel_size=4), nn.Flatten(), nn.AdaptiveMaxPool1d(1))
print("dense parameters:", sum(p.numel() for p in dense.parameters())) # 145
print("conv parameters:", sum(p.numel() for p in conv.parameters())) # 17
# The convolution applies the SAME 4x4 filter everywhere, so a pattern learned
# in one corner is recognised in the other. That is translation equivariance.
top_left = conv(make_image(0, 0).unsqueeze(0))
bottom_right = conv(make_image(8, 8).unsqueeze(0))
print("same response wherever the square sits:",
torch.allclose(top_left, bottom_right, atol=1e-6))
The parameter counts tell the first half of the story: 145 for the dense layer against 17 for the convolution.
The second half is the equivalence check. The same square in two different corners produces the same response, because the same filter slid over both. That property is translation equivariance, and it is why convolutions need far less data for image tasks.
The mistake this prevents
Feeding images to a dense network and concluding the problem is hard. It is hard for that architecture, because it has to relearn every pattern at every position.
Takeaway
Convolutions share one filter across all positions. That is fewer parameters and a built-in assumption that images actually satisfy.
