Convolution output geometry
Quick answer
Convolution layers slide learned filters across an image. PyTorch Conv2d computes cross-correlation without flipping the kernel. Padding, stride and dilation determine the output geometry. Examples are static Python snippets tested locally on PyTorch CPU. ByteMind execution is unverified.
Tiny cross-correlation
import torchimport torch.nn.functional as Fx=torch.arange(1,10,dtype=torch.float32).reshape(1,1,3,3)k=torch.tensor([[[[1.,0.],[0.,-1.]]]])print(F.conv2d(x,k)[0,0].int().tolist())Expected output (locally tested): [[-4, -4], [-4, -4]]. First value is 1 minus 5; flipping reverses the sign.
How it works
For an axis of length L, kernel K, symmetric padding P, stride S and dilation D, effective kernel E=D*(K-1)+1. Output length is floor((L+2P-E)/S)+1, and must be positive. Apply the formula separately to height and width. At each output position multiply aligned input and kernel values, sum and add bias. Mathematical convolution flips the kernel; cross-correlation does not. Stride skips starts, dilation spaces samples, and padding extends boundaries (zeros by default). A 3x3 stride-1 dilation-1 filter with padding 1 preserves shape. String padding="same" in Conv2d is limited to stride 1. Dilation changes coverage without increasing filter parameters.
Dilated geometry
import torchm=torch.nn.Conv2d(2,4,(3,2),stride=(2,1),padding=(1,0),dilation=(2,1))print(list(m(torch.zeros(1,2,9,7)).shape))Expected output (locally tested): [1, 4, 4, 6]. Effective height 5 and width 2 produce 4 and 6.
Common mistakes
Do not omit the floor or final +1. Invalid geometry raises an error in Conv2d; the exercise uses 0 as a sentinel. Treat non-square axes separately.
Practice and next steps
Calculate the first value with a non-symmetric filter, then predict output shape with unequal strides.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Convolution output geometry — knowledge check
Apply convolution output geometry to small examples and debugging scenarios.
What does Conv2d compute?
Effective width of K=3,D=2?
Which skips spatial starts?
Does dilation add filter parameters?
Padding for 3x3 stride-1 dilation-1 same size?