297. Write a 2D convolution with stride and padding
A conv layer slides a small kernel over an image and takes a dot product at each position. (Like PyTorch, this is technically cross-correlation: the kernel is not flipped.)
Write conv2d(img, kernel, stride=1, padding=0) for a 2D image (H, W) and kernel (kh, kw). Zero-pad the image on all sides first. Output size is (H + 2p - kh) // stride + 1 by (W + 2p - kw) // stride + 1.
def conv2d(img, kernel, stride=1, padding=0)Example 1
Input: img = [[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0, 9.0], [10.0, 11.0, 12.0, 13.0, 14.0], [15.0, 16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0, 24.0]], kernel = [[1.0, 0.0], [0.0, -1.0]]
Output: [[-6.0, -6.0, -6.0, -6.0], [-6.0, -6.0, -6.0, -6.0], [-6.0, -6.0, -6.0, -6.0], [-6.0, -6.0, -6.0, -6.0]]
Example 2
Input: img = [[0.0, 1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0, 9.0], [10.0, 11.0, 12.0, 13.0, 14.0], [15.0, 16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0, 24.0]], kernel = [[1.0, 0.0], [0.0, -1.0]], stride = 2, padding = 1
Output: [[0.0, -2.0, -4.0], [-10.0, -6.0, -6.0], [-20.0, -6.0, -6.0]]
Example 3
Input: img = [[0, 0, 9, 9]] * 3, kernel = [[-1, 1]]
Output: [[0.0, 9.0, 0.0], [0.0, 9.0, 0.0], [0.0, 9.0, 0.0]]
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 3 tests to be accepted.
Hint 1
Every output cell is the sum of an elementwise product between the kernel and one patch of the padded image, with patches spaced stride apart.
Hint 2
Zero-pad the image with np.pad, compute the output height and width from the stated formula, then loop over output positions and fill each with (patch * kernel).sum().
Hint 3
Output cell (i, j) uses the patch starting at row i * stride and column j * stride: x[i*stride : i*stride + kh, j*stride : j*stride + kw]. Compute the output size from the padded shape, (x.shape[0] - kh) // stride + 1.
Common mistakes
- The buggy version flips it, which is true convolution and gives different numbers.
Understand the idea
A convolution slides a small kernel over an image and, at each position, multiplies the overlapping numbers and adds them up. Deep learning libraries do not flip the kernel (strictly this is cross-correlation). Padding adds a border of zeros first, and stride says how many pixels to jump between positions.
image [[1, 2, 3], [4, 5, 6], [7, 8, 9]], kernel [[1, 0], [0, -1]], stride 1, padding 0. Output size is (3 - 2) // 1 + 1 = 2 by 2. Top-left patch [[1, 2], [4, 5]] gives 1 x 1 + 5 x (-1) = -4. The other patches give 2 - 6, 4 - 8 and 5 - 9, so the output is [[-4, -4], [-4, -4]]. With padding 1 and stride 2 the size would be (3 + 2 - 2) // 2 + 1 = 2.
pad, then out[r, c] = sum of kernel times the patch starting at (r x stride, c x stride)
Size = (H + 2p - kh) // stride + 1. Every CNN and vision encoder is built from this operation.
Topics
Companies2
- Google a candidate reported this question Google AI/ML, RAG and agentic AI interview (AI Engineering Insider)
- Microsoft the topic appears in interview reports Microsoft Interview Prep: Questions, Process & Study Plan
From public interview reports and guides. Not affiliated with or endorsed by these companies.