Convolutions and Local Patterns
Goal
By the end of this lesson, you can compute one small convolution output by hand, explain weight sharing, and predict how a local pattern's response shifts when the pattern moves.
Start with one local patch
Suppose an image patch is:
1 0
1 0
and the filter is:
1 -1
1 -1
Multiply matching positions and add:
1*1 + 0*(-1) + 1*1 + 0*(-1) = 2
The large positive response says this patch matches what the filter is looking for: values on the left and lower values on the right, like a vertical edge.
A convolution feature map is built by repeating this same local weighted-sum calculation at many positions.
Why reuse the same filter?
An edge can appear on the left side of an image or the right side.
If the same filter slides across the image, one learned pattern detector can respond wherever that local pattern appears.
This is weight sharing.
Without sharing, a model could require separate parameters for “vertical edge at column 1,” “vertical edge at column 2,” and so on.
Output size comes from valid filter positions
For a one-dimensional width of 5 and a kernel width of 3, stride 1, and no padding, the kernel can start at positions 1, 2, or 3.
So the output width is 3.
The same reasoning extends to height. Shape calculations are not separate bookkeeping; they describe how many local windows the filter can inspect.
Slide a filter across the Lab image
The Lab applies a 2×2 edge filter to this 4×4 image:
0 0 1 1
0 0 1 1
0 0 1 1
0 0 1 1
- Click Run and inspect the printed 3×3
feature map. - Hand-check output row 0, column 1. Its 2×2 input patch is
[[0, 1], [0, 1]]. Withedge_filter = [[1, -1], [1, -1]], the weighted sum is-2. - Move the edge one column to the right by changing every image row from:
[0., 0., 1., 1.]
to:
[0., 0., 0., 1.]
- Before running, predict that the strongest negative response should move from the middle output column to the rightmost output column.
- Click Run and inspect the full
feature map, not onlystrongest absolute response:. - Restore each image row to
[0., 0., 1., 1.].
Loading lab…
After the guided pass, flip the sign of every filter weight—[[1., -1.], [1., -1.]] to [[-1., 1.], [-1., 1.]]—and predict how the sign of the strong response changes while its location stays tied to the edge.
A filter can be perfectly valid and completely useless
If every filter weight is zero, the code runs and every local output becomes zero.
That is a useful reminder: convolution is not valuable because it is called convolution. It is valuable when the learned or designed local features preserve useful signal.
Debug from one cell outward
If a feature map looks wrong, do not start by changing the whole network.
Check one output cell:
- Which local patch was used?
- Are filter values aligned with the intended orientation?
- Is stride/padding what you think it is?
- Does the hand-computed weighted sum match the program?
- Does the overall output shape match the number of valid positions?
Quick Check
Key Takeaways
- Convolution repeats a local weighted sum across positions.
- Shared weights let one detector respond to the same pattern in different locations.
- Output shape follows from kernel size, stride, padding, and input size.
- Debug convolution at the level of one patch and one output cell first.
Next Lesson
Next, you will connect transparent local image features to a final class decision and trace the entire pixels-to-label path.
References
- PyTorch, nn.Conv2d.
Completion is stored locally on this device.