6.1 From Fully Connected Layers to Convolutions
The multilayer perceptrons of Chapter 4 flatten every image into an unordered vector of numbers: appropriate for tabular data, but wasteful for images because flattening discards which pixels sit next to one another. On a one-megapixel (\(1000 \times 1000\)) grayscale photograph, even a modest 1,000-unit hidden layer demands \(10^6 \times 10^3 = 10^9\) weights (4 GB in fp32), and a full-resolution \(1000 \times 1000\) hidden map demands \(10^{12}\) weights. Yet natural images are nothing like arbitrary million-dimensional vectors: neighboring pixels are strongly correlated (locality), and a visual feature such as a whisker carries the same meaning wherever it appears in the frame (translation equivariance).
What happens if we impose those two geometric principles directly on a dense layer mapping a 2D pixel grid to a 2D hidden grid? Translation equivariance alone slashes \(10^{12}\) weights down to roughly \(4 \times 10^6\), and restricting each hidden unit to a small local neighborhood cuts them further to a few hundred. What survives both constraints is a convolutional layer.
6.1.1 Equivariance and Invariance
The children’s game “Where’s Waldo” (Figure 6.1.1) captures the exact challenge of visual perception. Each busy scene teems with distractions, with Waldo hiding somewhere unexpected, yet what Waldo looks like does not depend on where he stands. We can therefore sweep a single Waldo detector across the image, scoring every patch by how strongly it matches Waldo using the exact same rule at every position. Modern object detection (Girshick et al. 2014) and semantic segmentation (Long et al. 2015) pipelines build directly on this idea.
This intuition points to two guiding principles for the early layers of a vision network:
- They should respond identically to the same local patch regardless of where it appears, so shifting the input shifts the output by the same amount.
- They should attend only to small local neighborhoods without worrying about distant parts of the image—the locality principle.
Subsequent layers can then compose these local responses into progressively larger, more abstract patterns, culminating in a whole-image prediction.
To formalize the first principle, let \(T_v\) denote the shift operator that translates an image by an offset \(v = (v_1, v_2)\), moving pixel \((i, j)\) to \((i + v_1, j + v_2)\) so that \([T_v \mathbf{X}]_{i,j} = [\mathbf{X}]_{i-v_1, j-v_2}\). An image-to-image mapping \(f\) is translation equivariant if shifting its input shifts its output by the exact same vector, whereas an image-to-label mapping is translation invariant if shifting its input leaves its output completely unchanged:
\[ \begin{aligned} f(T_v \mathbf{X}) &= T_v f(\mathbf{X}) && \textrm{(equivariance)},\\ f(T_v \mathbf{X}) &= f(\mathbf{X}) && \textrm{(invariance)}.\end{aligned} \tag{6.1.1}\]
Figure 6.1.2 illustrates translation equivariance as a commuting diagram. Here \(f\) matches a small template against every window of the image—the exact operation we are about to derive. Shifting first and matching second produces the identical map as matching first and shifting second.
Notice the clean division of labor: equivariance preserves where a pattern appears, whereas invariance discards location. A Waldo detector should be equivariant so its score map tracks Waldo across the scene; answering the yes-or-no question “is Waldo in this picture?” should be invariant, achieved at the end by aggregating the equivariant map across space (for instance, taking its maximum).
6.1.2 Constraints on the Fully Connected Layer
Both constraints become crystal clear in one dimension, where we can draw the entire weight matrix. Consider an eight-sample signal \(x_0, \ldots, x_7\) mapped by a fully connected layer to eight hidden units, \(h_i = u_i + \sum_k W_{i,k} x_k\). Its \(8 \times 8\) weight matrix has 64 independent entries, shown in panel (a) of Figure 6.1.3.
What does translation equivariance impose on \(\mathbf{W}\)? Ignoring biases for a moment (our proof below shows all biases must coincide), feed in a unit impulse with a single 1 at sample \(k\): the layer outputs column \(k\) of \(\mathbf{W}\). Shift that impulse to \(k + 1\), and the layer outputs column \(k + 1\). Equivariance insists that column \(k + 1\) must equal column \(k\) shifted down by one row, i.e., \(W_{i+1, k+1} = W_{i, k}\). Consequently, \(\mathbf{W}\) must be constant along every diagonal (a Toeplitz matrix), as in panel (b). Each diagonal corresponds to a single relative offset \(a = k - i\), collapsing 64 free weights down to just 15 (offsets \(-7\) through \(7\)).
Locality prunes almost all of those remaining 15 weights. Restricting each hidden unit to look only at neighbors within distance 1 zeroes out every offset outside \(\{-1, 0, 1\}\), leaving just 3 shared weights repeated across every row in panel (c). The layer now sweeps one tiny 3-weight template across the entire signal. That is the entire derivation in miniature; moving to 2D images simply adds a second spatial index.
For images, both the input \(\mathbf{X}\) and the hidden representation \(\mathbf{H}\) are two-dimensional grids, with input pixel \([\mathbf{X}]_{k,l}\) at \((k, l)\) and hidden unit \([\mathbf{H}]_{i,j}\) at \((i, j)\). Connecting every hidden unit to every pixel turns the weights into a fourth-order tensor \(\mathsf{W}\) alongside a bias matrix \(\mathbf{U}\):
\[[\mathbf{H}]_{i, j} = [\mathbf{U}]_{i, j} + \sum_k \sum_l [\mathsf{W}]_{i, j, k, l} [\mathbf{X}]_{k, l}.\]
Because both constraints depend on where a pixel sits relative to hidden unit \((i, j)\), we re-index by offsets \(a = k - i\) and \(b = l - j\), defining \([\mathsf{V}]_{i, j, a, b} = [\mathsf{W}]_{i, j, i+a, j+b}\):
\[[\mathbf{H}]_{i, j} = [\mathbf{U}]_{i, j} + \sum_a \sum_b [\mathsf{V}]_{i, j, a, b} [\mathbf{X}]_{i+a, j+b}. \tag{6.1.2}\]
Here \(a\) and \(b\) span both positive and negative offsets. This re-indexing is purely cosmetic: we are simply reading \(\mathsf{W}\) along its diagonals without reducing parameters yet. On a \(1000 \times 1000\) image mapped to a \(1000 \times 1000\) hidden grid, this single layer still demands \(10^6 \times 10^6 = 10^{12}\) weights.
6.1.2.1 Translation Equivariance
Just as in one dimension, a layer of the form Equation 6.1.2 is translation equivariant if and only if its weights and bias are independent of location \((i, j)\). Checking that shared weights yield equivariance takes one line; the converse is what makes convolution unavoidable, since equivariance leaves no other linear option.
To handle boundaries cleanly (where a shift pushes pixels off the edge of a finite frame), we embed the image on an infinite grid padded with zeros outside its support so no information ever leaves the grid. On finite images, equivariance holds strictly away from borders, a detail we address in Section 6.3.
Proposition. Let the input (with finitely many nonzero pixels) and the hidden representation live on the infinite grid. The layer Equation 6.1.2 is translation equivariant for every shift \(v\) if and only if there exist a scalar \(u\) and a matrix \(\mathbf{K}\) such that \([\mathbf{U}]_{i,j} = u\) and \([\mathsf{V}]_{i,j,a,b} = [\mathbf{K}]_{a,b}\) for all \(i, j, a, b\).
Proof. If neither weights nor bias depend on \((i, j)\), then shifting the input by \(v\) directly shifts the output by \(v\):
\[ \begin{aligned} [f(T_v \mathbf{X})]_{i,j} &= u + \sum_a \sum_b [\mathbf{K}]_{a,b} [\mathbf{X}]_{i - v_1 + a, j - v_2 + b} \\ &= [f(\mathbf{X})]_{i - v_1, j - v_2} = [T_v f(\mathbf{X})]_{i,j}. \end{aligned} \]
Conversely, assume the layer is equivariant. The all-zero image is invariant under every shift and maps to \(\mathbf{U}\), forcing \(\mathbf{U} = T_v \mathbf{U}\) for all \(v\), so the bias must be a constant \(u\). Subtracting \(u\) from both sides shows that the linear part alone is also equivariant. Feed in an image with a single 1 at \((k, l)\) and zeros elsewhere: its response at \((i, j)\) is \([\mathsf{W}]_{i,j,k,l}\). Shifting that impulse by \(v\) moves it to \((k + v_1, l + v_2)\), and equivariance requires \([\mathsf{W}]_{i, j, k+v_1, l+v_2} = [\mathsf{W}]_{i-v_1, j-v_2, k, l}\). Setting \((k, l) = (a, b)\) and \(v = (i, j)\) yields \([\mathsf{V}]_{i,j,a,b} = [\mathsf{W}]_{i,j,i+a,j+b} = [\mathsf{W}]_{0,0,a,b}\), which is independent of \((i, j)\). \(\blacksquare\)
In short, probing the layer with a single bright pixel forces every location to share the exact same weights, collapsing the layer to
\[[\mathbf{H}]_{i, j} = u + \sum_a\sum_b [\mathbf{K}]_{a, b} [\mathbf{X}]_{i+a, j+b}. \tag{6.1.3}\]
Every hidden unit now applies the same pattern \(\mathbf{K}\) centered at its own coordinates. On a \(1000 \times 1000\) zero-padded image, only offsets \(a, b \in [-999, 999]\) ever touch a nonzero pixel, slashing \(10^{12}\) weights down to \(1999^2 \approx 4 \times 10^6\).
6.1.2.2 Locality
Four million weights for a single filter is still far too many, and most of those weights link a hidden unit to pixels clear across the image. Enforcing locality (requiring \([\mathbf{H}]_{i,j}\) to depend only on pixels within distance \(\Delta\) along each axis) sets \([\mathbf{K}]_{a, b} = 0\) whenever \(|a| > \Delta\) or \(|b| > \Delta\), giving
\[[\mathbf{H}]_{i, j} = u + \sum_{a = -\Delta}^{\Delta} \sum_{b = -\Delta}^{\Delta} [\mathbf{K}]_{a, b} [\mathbf{X}]_{i+a, j+b}. \tag{6.1.4}\]
Each hidden unit now computes a weighted sum over a \((2\Delta+1) \times (2\Delta+1)\) local window centered at \((i, j)\), using just \((2\Delta+1)^2\) shared weights and one bias: 9 weights for \(\Delta = 1\) (a \(3 \times 3\) window), or 121 for \(\Delta = 5\). Because \(\Delta\) is typically well below 10, a few dozen or hundred weights replace \(4 \times 10^6\) while preserving the full spatial resolution of the hidden map.
Equation Equation 6.1.4 defines a convolutional layer, the building block of convolutional neural networks (CNNs). The shared matrix \(\mathbf{K}\) is called the convolution kernel or filter. Early pioneers of spatial and temporal weight sharing included time-delay neural networks for speech recognition (Waibel et al. 1989) and the shift-invariant networks of Zhang et al. (1988) .
Crucially, the parameter count shrank from \(10^{12}\) to \((2\Delta + 1)^2\) by restricting the hypothesis class to local, translation-equivariant mappings. This built-in inductive bias (Section 4.5) dramatically improves sample efficiency whenever features are local and position-independent (such as edges and textures), though a single layer cannot distinguish identical patches by absolute position alone (e.g., an eye near the top of a passport photo versus a nostril lower down). Stacking multiple layers with nonlinearities allows deeper units to integrate context across progressively wider regions.
6.1.3 Convolution and Cross-Correlation
Strictly speaking, the name “convolution” in deep learning is a slight misnomer. In mathematics, the discrete convolution of two 1D functions is \((f * g)(i) = \sum_a f(a) g(i-a)\) (with an integral on \(\mathbb{R}^d\)), measuring the overlap between \(f\) and a flipped, shifted copy of \(g\). In two dimensions,
\[(f * g)(i, j) = \sum_a\sum_b f(a, b) g(i-a, j-b). \tag{6.1.5}\]
Compare this with Equation 6.1.4, which indexes \(\mathbf{X}\) at \((i + a, j + b)\) rather than \((i - a, j - b)\). Substituting \(-a\) for \(a\) and \(-b\) for \(b\) reveals that Equation 6.1.4 is the mathematical convolution of \(\mathbf{X}\) with the flipped kernel \([\mathbf{K}']_{a,b} = [\mathbf{K}]_{-a,-b}\):
\[\sum_{a}\sum_{b} [\mathbf{K}]_{a, b} [\mathbf{X}]_{i+a, j+b} = \sum_{a}\sum_{b} [\mathbf{K}']_{a, b} [\mathbf{X}]_{i-a, j-b} = (\mathbf{K}' * \mathbf{X})(i, j). \tag{6.1.6}\]
Flipping reverses the kernel along both axes, so that its top-left weight becomes the bottom-right one (Figure 6.1.4).
The unflipped operation is properly called cross-correlation. Because CNN kernels are learned from data, the distinction has no effect on expressive power: a layer that cross-correlates with \(\mathbf{K}\) learns the exact same family of functions as one that convolves with \(\mathbf{K}'\). Deep learning frameworks compute cross-correlation and call it convolution, and we follow that standard convention throughout this book.
6.1.4 Channels
So far we treated both inputs and hidden maps as single grayscale grids. Real color photographs have three intensity values per pixel (red, green, and blue), so an image is a third-order tensor \(\mathsf{X}\) of shape \(3 \times \textrm{height} \times \textrm{width}\) with entries \([\mathsf{X}]_{c,i,j}\). The leading channel axis attaches a feature vector to every spatial pixel, and a kernel sums over both the local spatial window and all input channels.
Just as importantly, a single kernel produces only a single response map (like the Waldo score map in Figure 6.1.5), capturing just one visual pattern. Rich visual representations require detecting many patterns in parallel (oriented edges, corners, color contrasts, textures), so the hidden representation \(\mathsf{H}\) also becomes a third-order tensor stacked along an output channel axis. Each output channel \(o\) has its own learned kernel slice and bias \(u_o\), turning \(\mathsf{K}\) into a fourth-order tensor:
\[[\mathsf{H}]_{o,i,j} = u_o + \sum_c \sum_{a = -\Delta}^{\Delta} \sum_{b = -\Delta}^{\Delta} [\mathsf{K}]_{o, c, a, b} [\mathsf{X}]_{c, i+a, j+b}. \tag{6.1.7}\]
Here \(c\) indexes input channels and \(o\) indexes output channels; we keep this output-before-input channel convention throughout the chapter. Because each output channel maps where a particular learned feature occurs across the grid, hidden channels are also called feature maps, and the entire stack feeds into the next layer. With \(c_\textrm{i}\) input channels and \(c_\textrm{o}\) output channels, the layer requires \((2\Delta+1)^2 c_\textrm{i} c_\textrm{o}\) weights and \(c_\textrm{o}\) biases. A \(3 \times 3\) layer mapping an RGB image to 64 feature maps therefore has just \(9 \cdot 3 \cdot 64 + 64 = 1{,}792\) parameters, completely independent of the image’s resolution.
Nothing in this derivation is tied to two dimensions: the exact same logic yields 1D convolutions sliding across audio samples or text tokens, and 3D convolutions sliding across video frames or volumetric medical scans.
Equation Equation 6.1.7 is the foundation for the rest of this chapter. Because we derived it on an idealized infinite grid, three practical issues remain: handling finite image borders, expanding how much of the image deeper units can see, and aggregating spatial feature maps into a single image-level decision (like whether Waldo appears anywhere in the scene). First, however, we bring Equation 6.1.4 to life in code.
6.1.5 Summary
Flattening an image into an unstructured vector makes dense layers intractably large (\(10^{12}\) weights to map a megapixel image to an equal-sized hidden grid). Two natural assumptions about images collapse that count to a few hundred: translation equivariance forces every spatial position to share one kernel (as proved on the infinite grid), and locality restricts that kernel to a small window. The resulting layer computes cross-correlation (universally called convolution in deep learning), and stacking multiple input and output channels lets a single layer detect many visual patterns simultaneously while keeping parameter counts independent of image size. Those gains stem directly from inductive bias: a convolutional layer trades away arbitrary position-dependent mappings for strong generalization on spatially structured data.
6.1.6 Exercises
- Weight sharing on a ring. Repeat the argument of Figure 6.1.3 for a signal of four samples on a periodic grid, where the sample after the last one is the first one again, so that a shift loses nothing.
- Feed the layer a single 1 at each sample in turn and write down the \(4 \times 4\) weight matrix of a translation-equivariant layer. How many free weights does it have? Compare with the \(2 \cdot 4 - 1 = 7\) offsets of four samples surrounded by zeros.
- Impose locality with offsets \(-1\), \(0\), and \(1\). Which entries of the matrix are nonzero, and which of them have no counterpart in panel (c) of the figure?
- Commutativity of convolution.
- Prove that the convolution of Equation 6.1.5 is commutative, i.e., \(f * g = g * f\).
- Define the cross-correlation \((f \star g)(i, j) = \sum_a \sum_b f(i+a, j+b)\, g(a, b)\), the operation of Equation 6.1.4 with input \(f\) and kernel \(g\). Express \((g \star f)(i, j)\) through values of \(f \star g\), and decide from this expression whether cross-correlation is commutative.
- Convolutions for audio. Audio data is often represented as a one-dimensional sequence of samples.
- Derive the one-dimensional analog of Equation 6.1.4.
- Name one audio task for which a layer that is local and translation equivariant in time is a good fit, and one for which it is not. Justify each choice.
- A spectrogram represents a recording as a two-dimensional array whose axes are time and frequency, with frequency on a linear scale in hertz. For each axis, state whether translation equivariance is a reasonable assumption, and justify your answer from what a shift along that axis does to the sound. Then answer the same question for a frequency axis on a logarithmic scale.
- Equivariance versus invariance. A convolutional classifier computes feature maps \(h(\mathsf{X})\) with stacked layers of Equation 6.1.7 and nonlinearities in between, and then a prediction \(g(h(\mathsf{X}))\) with a head \(g\).
- Assume a periodic grid, which wraps around so that the column after the last one is the first one again; on it, \(h\) is translation equivariant by the argument of the proposition. Let \(g\) average each channel of \(h(\mathsf{X})\) over all locations. Using Equation 6.1.1, show that the prediction is translation invariant.
- An image shows a single digit in one of the nine cells of a \(3 \times 3\) grid, and the task is to name the cell. Explain why the classifier of the first part cannot solve this task. Describe a head \(g\) that solves it from the same feature maps \(h(\mathsf{X})\).
- Rotation equivariance. ● The proposition of this section derived weight sharing from equivariance to shifts. Ask the same of rotations. On the infinite grid of the proposition, let \(R\) rotate an image by \(90^\circ\) about the origin, \([R\mathbf{X}]_{i,j} = [\mathbf{X}]_{-j,i}\).
- Show that the convolutional layer Equation 6.1.4 satisfies \(f(R\mathbf{X}) = R f(\mathbf{X})\) for every image \(\mathbf{X}\) if and only if \([\mathbf{K}]_{a,b} = [\mathbf{K}]_{-b,a}\) for all offsets \((a, b)\).
- For \(\Delta = 1\), how many free weights does such a kernel have? Show that its response to an image does not change when the image is rotated by \(90^\circ\) about the pixel being evaluated, so that it cannot tell a vertical edge from a horizontal one.
- Instead of constraining one kernel, use four output channels whose kernels are copies of one unconstrained kernel \(\mathbf{K}\), rotated by \(0^\circ\), \(90^\circ\), \(180^\circ\), and \(270^\circ\). Show that rotating the input rotates every output map and permutes the four channels cyclically. What does this layer keep that the constrained kernel of the first part loses?