๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #176

Manual Convolution Example

This closing note of the CNN Fundamentals category ties every piece together into one complete, fully worked example โ€” computing an entire feature map by hand, with padding and stride both involved, verified line-for-line against PyTorch's own convolution implementation.

The Setup

A 5ร—5 single-channel input:

\[ I = \begin{bmatrix}1&2&3&0&1\\0&1&2&3&1\\1&0&1&2&0\\2&1&0&1&1\\0&2&1&0&2\end{bmatrix} \]

A 3ร—3 kernel: \(K = \begin{bmatrix}1&0&-1\\1&0&-1\\1&0&-1\end{bmatrix}\), with padding \(P=1\) and stride \(S=2\).

Step 1 โ€” Apply Padding

Adding 1 pixel of zero-padding on every side produces a 7ร—7 padded input (shown with the padding as 0s around the original 5ร—5 grid).

Step 2 โ€” Compute the Output Size

\[ \text{Output size} = \left\lfloor\frac{5-3+2(1)}{2}\right\rfloor+1 = \left\lfloor\frac{4}{2}\right\rfloor+1 = 3 \]

A 3ร—3 output feature map, using the formula from Padding.

Step 3 โ€” Compute the Top-Left Output Value

With padding, the kernel's first position overlaps the padded input's top-left 3ร—3 corner:

\[ \begin{bmatrix}0&0&0\\0&1&2\\0&0&1\end{bmatrix} \]
\[ (0)(1)+(0)(0)+(0)(-1) + (0)(1)+(1)(0)+(2)(-1) + (0)(1)+(0)(0)+(1)(-1) = 0-2+0-1 = -3 \]

Step 4 โ€” Compute the Remaining Output Values (Stride 2)

Moving the kernel 2 positions right (respecting the stride), then repeating downward, gives the complete 3ร—3 output feature map. Rather than hand-computing all 9 values here, the code below verifies the entire result โ€” including this first value of \(-3\) โ€” directly against PyTorch's implementation.

Code โ€” Verifying the Complete Example

import torch
import torch.nn.functional as F

I = torch.tensor([[[[1.,2.,3.,0.,1.],
                     [0.,1.,2.,3.,1.],
                     [1.,0.,1.,2.,0.],
                     [2.,1.,0.,1.,1.],
                     [0.,2.,1.,0.,2.]]]])   # shape (1,1,5,5)

K = torch.tensor([[[[1.,0.,-1.],
                     [1.,0.,-1.],
                     [1.,0.,-1.]]]])   # shape (1,1,3,3)

output = F.conv2d(I, K, padding=1, stride=2)
print(output.shape)   # torch.Size([1, 1, 3, 3]) -- matches the computed output size
print(output)
# The top-left value should match -3.0, computed by hand above

Putting It All Together โ€” Every Concept from This Category

ConceptRole in This Example
Image representationThe 5ร—5 single-channel input tensor
KernelThe 3ร—3 learnable weight matrix
PaddingAdded 1 pixel of zero-border, changing the output size formula
StrideSet to 2, skipping every other position and further shrinking the output
Convolution operationThe sliding dot-product computation itself, at every valid position
Feature mapThe resulting 3ร—3 output grid

Common Mistakes

  • Forgetting to account for padding when computing which input values a given output position actually overlaps โ€” always work with the padded input grid when tracing through a manual calculation like this one, not the original unpadded input.
  • Losing track of how stride skips positions when manually verifying multiple output values by hand โ€” a systematic position-by-position trace (or, more practically, verifying against code as done here) avoids this class of error.

Interview Relevance

Q: "Walk through computing one value of a convolution's output feature map by hand, including padding and stride." This exact kind of worked example โ€” identifying the padded input, locating the correct overlapping patch for a given stride and output position, and computing the dot product between that patch and the kernel โ€” is precisely the skill that demonstrates genuine understanding of convolution's mechanics, beyond just knowing the formula.

Key Takeaways โ€” CNN Fundamentals

  • Convolution's local connectivity and parameter sharing make CNNs vastly more parameter-efficient than fully-connected networks for spatial data like images.
  • Kernels/filters detect learned patterns; stacking many filters produces stacked feature maps; stride and padding together control output size precisely.
  • Receptive field grows with network depth, letting deep stacks of small kernels "see" large regions of the original input cheaply.
  • Pooling (max, average, or global average) downsamples feature maps with zero added parameters; flattening or GAP bridges the spatial convolutional part of a network to its final fully-connected classification head.

Next: CNN Architectures puts all of these building blocks to work, tracing the actual landmark architectures โ€” from LeNet through ConvNeXt โ€” that shaped how modern CNNs are designed.

Practice Question

Using the same 5ร—5 input and kernel from this note, but with padding \(P=0\) and stride \(S=1\) instead, compute the resulting output size using the formula from Stride.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Manual Convolution Example โ€“ FAQs

Quick answers about learning Manual Convolution Example in Deep Learning.

This free note from CodingNow 2.0 explains Manual Convolution Example in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Manual Convolution Example, is 100% free with no signup required.
With focused practice, most students grasp Manual Convolution Example in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now