All problems
Conv2D Single Channel Forward
Problem
Forward pass of a 2D convolution with one input channel and one output channel, no bias. Slide the kernel over the optionally zero-padded image using the given stride and return the output feature map.
The signature is conv2d_forward(image, kernel, stride=1, padding=0). Assume a square image and square kernel with a valid output size.
Hint
Zero-pad the image first, then slide the kernel with the stride and sum element-wise products.
Test cases
case 1
in: [[[1,2,3],[4,5,6],[7,8,9]],[[1,1],[1,1]],1,0]
expected: [[12,16],[24,28]]
case 2
in: [[[1,2,3],[4,5,6],[7,8,9]],[[1,0],[0,-1]],1,0]
expected: [[-4,-4],[-4,-4]]
case 3
in: [[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],[[1,1],[1,1]],2,0]
expected: [[14,22],[46,54]]
case 4
in: [[[1,1],[1,1]],[[1,0],[0,1]],1,1]
expected: [[1,1,0],[1,2,1],[0,1,1]]
Starter codePython
def conv2d_forward(image, kernel, stride=1, padding=0):
# Your code here
pass4 test cases · run your code against them in the editor