From Fully Connected Layers to Convolutions
Dive into Deep Learning · §6.1
From Fully Connected Layers to Convolutions
translation equivariance · locality · weight sharing · cross-correlation · channels
Why Dense Layers Fail on High-Resolution Images
The Scaling Bottleneck
- All-to-all quadratic explosion: Mapping a 1000 \times 1000 image to an equal-sized hidden grid needs 10^{12} weights (4 TB grayscale, 36 TB RGB).
- Even narrow projections fail: Projecting a 1MP RGB frame to just 300 hidden units demands 9 \times 10^8 weights (3.6 GB).
- Root cause: Dense layers assign separate weights to every input–output coordinate pair and ignore 2D spatial proximity.
Visual Perception Relies on Two Spatial Symmetries
The “Where’s Waldo” Intuition
Finding a target pattern in a cluttered visual scene illustrates the two geometric principles that govern early vision layers:
- Position-independent appearance: What Waldo looks like does not depend on where he stands, so a single local template can sweep across every patch of the scene.
- Translation equivariance: Shifting the input image shifts the detector’s response map by the exact same offset.
- Locality: Early feature detectors need only inspect a small neighborhood around each pixel, leaving deeper layers to aggregate global context.
Translation Equivariance vs. Translation Invariance
Formalizing Spatial Symmetries
- Shift operator T_v: Translates an image by v = (v_1, v_2) so [T_v \mathbf{X}]_{i,j} = [\mathbf{X}]_{i-v_1, j-v_2}.
- Equivariance (f(T_v \mathbf{X}) = T_v f(\mathbf{X})): Feature maps shift with the input, preserving where patterns occur for localization.
- Invariance (f(T_v \mathbf{X}) = f(\mathbf{X})): Whole-image classifiers pool equivariant maps across space so the final label is shift-independent.
Equivariance as a Commuting Diagram
Shift Then Filter Equals Filter Then Shift
When f scores every 2 \times 2 window against a 2 \times 2 patch of ones, both paths around the diagram produce the identical output grid:
Commutation invariant: Shifting the input by v = (3, 2) and then applying the filter f (bottom path) yields the exact same response map as applying f first and shifting the output by v (right path).
1D Intuition: From 64 Weights to 15, Then to 3
Toeplitz Structure & Locality in 1D
On an 8-sample 1D signal mapped to 8 hidden units (h_i = u_i + \sum_k W_{i,k} x_k), each geometric constraint prunes the 8 \times 8 weight matrix \mathbf{W}:
- (a) Fully connected (64 weights): Every entry W_{i,k} is independent.
- (b) Equivariant / Toeplitz (15 weights): Shifting a unit impulse at k to k+1 shifts the output down one row (W_{i+1,k+1} = W_{i,k}), making \mathbf{W} constant along each diagonal a = k - i \in \{-7,\dots,7\}.
- (c) Equivariant + Local (3 weights): Restricting each unit to neighbors within distance \Delta = 1 zeroes out all diagonals with |a| > 1, leaving one 3-tap kernel swept across every row.
Re-Indexing a 2D Dense Layer by Relative Offsets
From 4D Weight Tensors to Spatial Offsets
[\mathbf{H}]_{i, j} = [\mathbf{U}]_{i, j} + \sum_a \sum_b [\mathsf{V}]_{i, j, a, b} [\mathbf{X}]_{i+a, j+b}
- Change of variables: Substitute relative offset (a, b) = (k - i, l - j) and define [\mathsf{V}]_{i, j, a, b} = [\mathsf{W}]_{i, j, i+a, j+b}.
- No reduction yet: Re-indexing merely reads \mathsf{W} along spatial diagonals (10^{12} free weights until (i,j)-dependence is dropped).
Proposition: Shared Weights Yield Equivariance
Sufficiency Proof on the Infinite Grid
[f(T_v \mathbf{X})]_{i,j} = u + \sum_{a,b} [\mathbf{K}]_{a,b} [\mathbf{X}]_{i - v_1 + a, j - v_2 + b} = [T_v f(\mathbf{X})]_{i,j}
- Infinite zero-padded grid: Ensures shifts never clip nonzero pixels at a finite boundary.
- Sufficiency: Tying [\mathbf{U}]_{i,j} = u and [\mathsf{V}]_{i,j,a,b} = [\mathbf{K}]_{a,b} makes f commute with every shift T_v.
Converse Proof: Equivariance Forces Weight Sharing
Necessity Proof via Unit Impulse Probing
- 1. Constant bias: T_v \mathbf{0} = \mathbf{0} \implies \mathbf{U} = T_v \mathbf{U}, so [\mathbf{U}]_{i,j} = u.
- 2. Impulse probing: Feeding a single 1 at (k,l) and shifting by v = (i,j) forces [\mathsf{V}]_{i,j,a,b} = [\mathsf{W}]_{0,0,a,b} \equiv [\mathbf{K}]_{a,b}.
- Equivariance theorem: Linear layer commutes with shifts \iff it shares one kernel \mathbf{K} and scalar bias u (10^{12} \to 4 \times 10^6 weights).
Enforcing Locality Yields the Convolutional Layer
Pruning Distant Offsets to a (2\Delta+1) \times (2\Delta+1) Window
[\mathbf{H}]_{i, j} = u + \sum_{a = -\Delta}^{\Delta} \sum_{b = -\Delta}^{\Delta} [\mathbf{K}]_{a, b} [\mathbf{X}]_{i+a, j+b}
- Local window |a|, |b| \le \Delta: Zeroes out distant offsets so each unit inspects only a (2\Delta+1) \times (2\Delta+1) neighborhood.
- 11-order-of-magnitude collapse: For \Delta = 1 (3 \times 3 kernel), 10^{12} weights (4 TB) shrink to just 9 shared weights (36 B).
Inductive Bias: Sample Efficiency vs. Expressivity
What We Gained and What We Traded Away
- Sample efficiency: Pooling evidence across all H \times W positions prevents overfitting on local, shift-equivariant visual structures.
- Single-layer blind spot: Tied weights cannot tell identical local patches apart by absolute position (e.g., an eye vs. a nostril).
- Deep recovery: Stacking layers with nonlinearities expands the receptive field to capture whole-object layout.
Mathematical Convolution vs. Cross-Correlation
Why Deep Learning Libraries Omit the Kernel Flip
\sum_{a,b} [\mathbf{K}]_{a, b} [\mathbf{X}]_{i+a, j+b} = (\mathbf{K}' * \mathbf{X})(i, j)
- Sign convention: Mathematical convolution uses (i-a, j-b); cross-correlation uses (i+a, j+b).
- Identical hypothesis class: Because [\mathbf{K}']_{a,b} = [\mathbf{K}]_{-a,-b} is a 180° flip of learned weights, frameworks implement cross-correlation and call it a convolutional layer.
From Grayscale Grids to 3D Channel Tensors
Multi-Channel Inputs and Feature Maps
Real images carry multiple values per pixel (e.g., Red, Green, Blue), and hidden layers must detect dozens of distinct visual patterns in parallel:
- Input tensor \mathsf{X} \in \mathbb{R}^{c_\textrm{i} \times H \times W}: Attaches a c_\textrm{i}-dimensional vector to each spatial pixel (i, j) (c_\textrm{i} = 3 for RGB).
- Single-channel limitation: One kernel produces only one score map (like the single Waldo activation map on the right).
- Output tensor \mathsf{H} \in \mathbb{R}^{c_\textrm{o} \times H \times W}: Stacks c_\textrm{o} parallel feature maps, each detecting a different learned pattern (oriented edges, color contrasts, textures).
The 4D Convolutional Kernel and Parameter Count
All-to-All Channel Wiring with Spatial Weight Sharing
[\mathsf{H}]_{o, i, j} = u_o + \sum_{c=1}^{c_\textrm{i}} \sum_{a = -\Delta}^{\Delta} \sum_{b = -\Delta}^{\Delta} [\mathsf{K}]_{o, c, a, b} [\mathsf{X}]_{c, i+a, j+b}
- 4D kernel \mathsf{K} \in \mathbb{R}^{c_\textrm{o} \times c_\textrm{i} \times k_\textrm{h} \times k_\textrm{w}}: Total parameters = c_\textrm{o} c_\textrm{i} k_\textrm{h} k_\textrm{w} + c_\textrm{o}.
- Resolution independence: A 3 \times 3 RGB-to-64 layer uses 1{,}792 parameters on both 28 \times 28 and 4000 \times 4000 images.
Convolutions Beyond 2D Images: 1D, 2D, and 3D
The Same Symmetry Principle Across Modalities
- 1D, 2D, and 3D grids: Equivariance + locality yields 1D convolutions over time (c_\textrm{i}, T), 2D over images (c_\textrm{i}, H, W), and 3D over video/volumes (c_\textrm{i}, D, H, W).
- When equivariance breaks: In a linear-Hz spectrogram, shifting frequency alters harmonic ratios—requiring a log-frequency (mel) axis before applying 2D convolutions.
Concept Check: Deriving and Counting Convolutions
Concept Check
Work out these three questions from first principles:
- 28×28 weight collapse: On a 28 \times 28 single-channel image mapped to a 28 \times 28 grid, how many weights does a dense layer have, how many survive translation equivariance (zero-padded outside the frame), and how many remain with a 5 \times 5 local window (\Delta = 2)?
- Resolution vs. parameters: A 3 \times 3 layer maps c_\textrm{i}=3 to c_\textrm{o}=64 channels. How many parameters does it have on a 224 \times 224 image versus a 1024 \times 1024 image? Compare with the 200{,}704 first-layer weights of our Fashion-MNIST MLP (784 \to 256).
- Periodic boundary ring: On a 1D periodic ring of 4 samples (where index 4 wraps to 0), how many free weights does an equivariant 4 \times 4 matrix have before imposing locality?
Answers: (1) Dense: 784^2 = 614{,}656; Equivariant (offsets -27 \dots 27): 55^2 = 3{,}025; Local 5 \times 5: 25 weights (+1 bias). (2) Both resolutions use 64 \times 3 \times 9 + 64 = 1{,}792 parameters (< 0.9\% of the MLP’s first layer). (3) Exactly 4 free weights (a circulant matrix where each row shifts cyclicly by 1, vs. 2(4)-1 = 7 on a zero-padded line).
Summary & Key Takeaways
Section Synthesis
| Dense Layer on 2D Grid |
[\mathbf{H}]_{i,j} = [\mathbf{U}]_{i,j} + \sum_{a,b} [\mathsf{V}]_{i,j,a,b}[\mathbf{X}]_{i+a,j+b} |
c_\textrm{o} c_\textrm{i} H^2 W^2 + c_\textrm{o} HW |
Arbitrary spatial wiring; intractable at high resolution |
| Translation Equivariance |
f(T_v \mathbf{X}) = T_v f(\mathbf{X}) \iff [\mathsf{V}]_{i,j,a,b} = [\mathbf{K}]_{a,b} |
c_\textrm{o} c_\textrm{i} (2H-1)(2W-1) + c_\textrm{o} |
Ties weights across space so feature maps move with the input |
| Locality (\|a\|, \|b\| \le \Delta) |
[\mathbf{K}]_{a,b} = 0 outside (2\Delta+1) \times (2\Delta+1) |
c_\textrm{o} c_\textrm{i} (2\Delta+1)^2 + c_\textrm{o} |
Restricts each unit to local neighborhoods; independent of H, W |
| Cross-Correlation vs. Conv |
(\mathbf{X} \star \mathbf{K})_{i,j} = (\mathbf{K}' * \mathbf{X})_{i,j} |
Identical |
Unflipped indexing (i+a, j+b) implemented by all frameworks |
Next (§6.2): We implement 2D cross-correlation in code, hand-craft and learn an edge-detection kernel via gradient descent, recast convolution as matrix multiplication (im2col), and analyze multi-layer receptive fields.