This note zooms in specifically on how convolution handles channels โ both the input's original channels (like RGB) and the learned channels produced by each subsequent layer โ closing the gap left by Kernel and Filter's introduction of this concept.
How Multi-Channel Convolution Actually Works
For each output position, the convolution is computed independently for each input channel, using that channel's own kernel slice, and then all \(C_{\text{in}}\) channel results are summed together into one single value โ this is exactly why one filter, no matter how many input channels it processes, always collapses down to producing just one output feature map (not one per input channel).
Numerical Example โ 2-Channel Input
A tiny 2-channel input, each channel a single value at this position: channel 1 = 3, channel 2 = 5. A filter's two corresponding kernel weights (for this one position, simplified): channel 1 weight = 2, channel 2 weight = -1.
Both channels' contributions are combined into this single output value โ the filter has "looked at" both input channels simultaneously and produced one unified result.
Input Channels vs Output Channels
| Determined By | |
|---|---|
| Number of input channels (\(C_{\text{in}}\)) | Fixed by the previous layer's output (or the original image's channels, e.g. 3 for RGB) โ not a free choice for this layer |
| Number of output channels (\(C_{\text{out}}\)) | A design choice โ exactly equal to however many filters this layer uses |
Code
import torch
import torch.nn as nn
# Input has 3 channels (RGB); this layer uses 64 filters, producing 64 output channels
conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)
print(conv.weight.shape) # torch.Size([64, 3, 3, 3]) -- (out_channels, in_channels, kH, kW)
x = torch.randn(1, 3, 32, 32)
output = conv(x)
print(output.shape) # torch.Size([1, 64, 32, 32]) -- in_channels=3 collapsed away, out_channels=64 introduced
Common Mistakes
- Mismatching a layer's declared
in_channelswith the actual number of channels in its input tensor โ this raises a clear shape error, almost always meaning a previous layer's output channel count wasn't correctly tracked when defining the next layer. - Expecting a filter with multiple input-channel kernels to produce multiple output feature maps per filter โ a single filter, regardless of how many input channels it processes, always produces exactly one output feature map, since the per-channel results are summed together.
Interview Relevance
Q: "If a convolutional layer takes a 3-channel input and uses 64 filters, how many total output channels does it produce, and why isn't it 3ร64?" The output has exactly 64 channels โ one per filter. Each filter contains one kernel per input channel (3, in this case), but the per-channel convolution results are summed together into a single value at each spatial position, collapsing the 3 input channels down to 1 output value per filter, not multiplying them.
Practice Question
A layer takes a 16-channel input and applies 32 filters of size 3ร3. What is the shape of this layer's weight tensor, and how many output channels does it produce?