def corr2d_multi_in(X, K):
"""Cross-correlate each channel of X with its channel of K and add."""
return sum(d2l.corr2d(x, k) for x, k in zip(X, K))Dive into Deep Learning · §6.4
Multiple Input and Multiple Output Channels
multi-channel kernels · feature maps · compute & memory scaling · 1×1 bottlenecks
Motivation & The Quadratic Channel Scaling Wall
One Spatial Filter Slice per Input Channel, Then Sum
When the input \mathsf{X} has c_\textrm{i} channels of shape (c_\textrm{i}, n_\textrm{h}, n_\textrm{w}), a single output channel’s kernel \mathsf{K} must have shape (c_\textrm{i}, k_\textrm{h}, k_\textrm{w}). We cross-correlate each channel \mathbf{X}_c with its filter slice \mathbf{K}_c and sum across all c_\textrm{i} channels:
\mathbf{Y} = \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_c
Why Spatial Filtering Alone Cannot See Equal-Luminance Edges
[\mathbf{Y}]_{i,j} = [\mathbf{X}_{\textrm{R}}]_{i,j} - [\mathbf{X}_{\textrm{G}}]_{i,j}
Summing 2D Cross-Correlations Over the Channel Axis
In Python, corr2d_multi_in zips the channel slices of X and K, calls d2l.corr2d(x, k) on each pair, and sums the resulting 2D grids:
tensor([[ 56., 72.],
[104., 120.]])
Extracting c_o Parallel Feature Maps
\mathbf{Y}_o = u_o + \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_{o, c}, \qquad o = 1, \ldots, c_\textrm{o}
Stacking Output Feature Maps Along Axis 0
Iterating corr2d_multi_in(X, k) over each 3D kernel k in the 4D tensor K and stacking along axis 0 produces the 3D output tensor (c_\textrm{o}, h_\textrm{o}, w_\textrm{o}):
Why Adding +1 to All Kernel Weights Adds 20 to Position (0,0)
Parameters, Multiply-Adds (MACs), and Activation Memory
Early Stem Layers vs. Deep High-Channel Layers
Mixing Channels Without Spatial Aggregation
At k_\textrm{h} = k_\textrm{w} = 1, the spatial window covers just a single pixel (i, j)—so what does a 1 \times 1 convolution actually compute?
[\mathsf{Y}]_{o, i, j} = u_o + \sum_{c=1}^{c_\textrm{i}} [\mathbf{K}]_{o, c}\, [\mathsf{X}]_{c, i, j}
Reshaping (c_i, H, W) to (c_i, H*W) Without im2col Duplication
0.0
Breaking the c_i * c_o Cost of Wide 3×3 Convolutions
Factorizing All-to-All Channel Wiring in Modern CNNs
Concept Check
Work out these three calculations:
Answers: (1) Channel 0 top-right window: 1{\cdot}0 + 2{\cdot}1 + 4{\cdot}2 + 5{\cdot}3 = 25. Channel 1 top-right window: 2{\cdot}1 + 3{\cdot}2 + 5{\cdot}3 + 6{\cdot}4 = 47. Sum: 25 + 47 = 72. (2) Output shape: 16 \times 10 \times 10. Parameters: 16 \times 6 \times 25 + 16 = 2{,}416. MACs: 10^2 \times 2{,}400 = 240{,}000. (3) (a) Parameters: 1\times (unchanged); (b) Multiply-adds: 4\times (h_\textrm{o} w_\textrm{o} quadruples); (c) Activation memory: 4\times.
Section Synthesis
| Concept / Mechanism | Mathematical / Tensor Form | Scaling Cost | Architectural Role |
|---|---|---|---|
| Multi-Input Conv | \mathbf{Y} = \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_c | \mathsf{K} \in \mathbb{R}^{c_\textrm{i} \times k_\textrm{h} \times k_\textrm{w}} | Fuses color or multi-feature cues across channels |
| Multi-Output Conv | \mathbf{Y}_o = u_o + \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_{o,c} | c_\textrm{o} c_\textrm{i} k_\textrm{h} k_\textrm{w} + c_\textrm{o} params | Extracts c_\textrm{o} parallel feature maps; grows as H, W shrink |
| Compute & Memory | \textrm{MACs} = h_\textrm{o} w_\textrm{o} c_\textrm{o} c_\textrm{i} k_\textrm{h} k_\textrm{w} | \textrm{Activations} = h_\textrm{o} w_\textrm{o} c_\textrm{o} | Early layers dominate RAM; deep wide layers dominate params |
| 1 \times 1 Convolution | \mathbf{Y} = \textrm{reshape}(\mathbf{K}\,\textrm{reshape}(\mathsf{X})) | c_\textrm{o} c_\textrm{i} + c_\textrm{o} params | Per-pixel channel MLP; bottleneck projections cut cost 8.5\times |
Next (§6.5): How to summarize local neighborhoods, absorb small spatial jitters, and collapse spatial feature maps into translation-invariant vectors via pooling layers.