Local filters
Calculate a convolutional output and explain filters, stride, padding, pooling and residual connections.
THE IDEA
Look at a small window, then slide it
Looking at every pixel of an image at once needs a huge number of weights. A convolutional layer instead learns one tiny pattern detector, a kernel (also called a filter), and applies it at every position. At each position you multiply the kernel with the numbers underneath it and add the results. That single sum is one output value.
kernel [1,-1] sits on positions 0 and 1: 1·1 + -1·2 = -1
Deep-learning libraries call this convolution, but strictly it is cross-correlation: the kernel is not flipped. True mathematical convolution flips the kernel first. Because the kernel is learned, the difference does not matter in practice, but it matters when you compare with a maths textbook or check an answer by hand.
Predict first. Cross-correlate [1, 2, 3] with the kernel [1, −1], no padding. What do you get?
The same idea in two dimensions
On an image the kernel is a small grid, and the same weights are reused at every position. Different weights detect different things: the kernel with −1 on the left and +1 on the right responds where brightness jumps from left to right, which is a vertical edge. Try it: pick an image, a preset, then edit single weights.
window = image[i:i+3, j:j+3]
out[i, j] = (window * kernel).sum()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
Compute two sliding sums by hand, then design a kernel for the vertical edge image that stays silent on the horizontal edge image. The Python part implements 1-D and 2-D cross-correlation.
- To do: Slide the kernel [1, −1] over [1, 2, 3, 4] with no padding. Type the three outputs
- To do: Now the kernel [2, 0, −1] over [1, 2, 3, 4, 5]. Type the three outputs
- To do: Set a 3×3 kernel that responds strongly to the vertical edge image (total absolute response of 10 or more) and gives exactly zero response on the horizontal edge image
Part 2. Do it in code
- cross_correlate_1d(x, k): slide k over x without flipping it and return the valid outputs (length len(x) - len(k) + 1) as a numpy array. If k is longer than x raise ValueError.
- convolve_1d(x, k): true mathematical convolution: the same, but with the kernel flipped first.
- conv2d_valid(img, ker): 2-D cross-correlation of a 2-D array with a 2-D kernel, no padding, stride 1. Return an array of shape (H - kh + 1, W - kw + 1).
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Cross-correlate [1,2,3] with [1,-1] and no padding.
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.