Channels
Calculate a convolutional output and explain filters, stride, padding, pooling and residual connections.
THE IDEA
Channels: many input maps, many output maps
Real images have channels (red, green, blue) and every layer's output has channels too. A kernel is therefore a small block: k × k in space and as deep as the input has channels. One filter sums over all input channels and produces one output map. To get C_out output maps you learn C_out different filters.
Predict first. Does a three-channel input automatically give three output channels?
out[o, i, j] = (x[:, i:i+k, j:j+k] * w[o]).sum() + b[o]Chapter context
A convolutional layer reuses a local filter across positions. Weight sharing encodes a structural assumption about repeated local patterns while greatly reducing parameters compared with a fully connected map over all pixels.
TRY IT YOURSELF
Part 1. Do it hands-on
Count parameters by hand (the calculator above checks the formula), then implement a multi-channel convolution and a 1×1 convolution.
- To do: A 3×3 convolution from 3 input channels to 16 output channels, with bias: how many parameters? Type the number
- To do: A 1×1 convolution from 64 channels to 16 channels, with bias: how many parameters?
- To do: Your layer takes 8 input channels with a 3×3 kernel. Choose the number of output channels so the layer has between 500 and 600 parameters
Part 2. Do it in code
- conv_params(c_in, c_out, k, bias=True): the number of parameters of a k×k convolution layer: k*k*c_in*c_out plus c_out when bias is True.
- conv2d_multi(x, w, b=None): x has shape (c_in, H, W), w has shape (c_out, c_in, k, k) and b has shape (c_out,) or is None. Valid convolution (no padding, stride 1). Every output channel sums over ALL input channels. Return shape (c_out, H-k+1, W-k+1). Raise ValueError if w's c_in does not match x's channels.
- conv1x1(x, w): a 1×1 convolution, w of shape (c_out, c_in). Return shape (c_out, H, W): every pixel's channel vector is multiplied by w.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Does a three-channel input automatically imply three output channels?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
For one spatial dimension, output size is
Here n is input size, k kernel size, p padding, d dilation and s stride. Cross-correlation is . A residual block has form when shapes agree.
Worked example
For input [1,2,3,4] and kernel [1,−1], stride-one valid cross-correlation returns [−1,−1,−1]. Separately, n=7,k=3,p=1,s=2,d=1 gives floor((7+2−2−1)/2)+1=4 output positions.
Reference code
The following example uses the Python standard library and does not call a remote service.
x,w = [1,2,3,4],[1,-1]
y = [sum(x[t+i]*w[i] for i in range(len(w))) for t in range(len(x)-len(w)+1)]
assert y == [-1,-1,-1]
n,k,p,s,d = 7,3,1,2,1
assert (n+2*p-d*(k-1)-1)//s+1 == 4
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
Padding does not reveal missing real pixels. Kernel flipping conventions change calculations. A residual shortcut needs a defined shape-compatible path.