2D CNN Forward Pass
Implement a simplified 2D CNN forward pass: convolution + ReLU + max pooling.
Given a 2D input image, a single 2D kernel, and a bias:
- Convolution (valid, no padding): slide kernel, compute dot product + bias
- ReLU: apply max(0, x) element-wise
- 2x2 Max Pooling with stride 2
Return the final output matrix, rounded to 4 decimal places.
Example
image = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] kernel = [[1, 0], [0, -1]] bias = 0
[[-4]]
- First, we perform the convolution operation: slide the kernel over the image, computing the dot product at each position and adding the bias. For the given image and kernel, the resulting matrix will be computed as follows:
- For the top-left position:
- This process is repeated for all valid positions, resulting in a convolution output.
- Then, we apply the ReLU activation function: , which sets all negative values to 0.
- Next, we apply 2x2 Max Pooling with stride 2: we divide the ReLU output into 2x2 sub-matrices and take the maximum value from each, resulting in a single value.
- The final output is is not the correct step, instead after convolution we get [[-5, -4], [-13, -12]], then after ReLU we get [[0, 0], [0, 0]], and after max pooling we get [[0]] which is not the answer, re-evaluating the steps:
- Convolution: = , = ,
- ReLU: = , =
- Max Pooling: = is also incorrect. Re-checking the math:
- Convolution for the first position: $(1*1 +
Constraints
- image: 2D list (H x W)
- kernel: 2D list (kH x kW)
- bias: scalar
- Return 2D list after conv + ReLU + 2x2 max pool
- Round to 4 decimal places