PIXELBANKv9.1.0
Menu

Implement a simplified 2D CNN forward pass: convolution + ReLU + max pooling.

Given a 2D input image, a single 2D kernel, and a bias:

  1. Convolution (valid, no padding): slide kernel, compute dot product + bias
  2. ReLU: apply max(0, x) element-wise
  3. 2x2 Max Pooling with stride 2

Return the final output matrix, rounded to 4 decimal places.

Example:

Input:
image = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
kernel = [[1, 0], [0, -1]]
bias = 0
Output:
[[-4]]
Reasoning:
  • 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: (1⋅1+2⋅0+5⋅0+6⋅−1)+0=1−6=−5(1 \cdot 1 + 2 \cdot 0 + 5 \cdot 0 + 6 \cdot -1) + 0 = 1 - 6 = -5
    • This process is repeated for all valid positions, resulting in a convolution output.
  • Then, we apply the ReLU activation function: result=max⁡(0,x)result = \max(0, x), 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 [max⁡(0,−5)][\max(0, -5)] 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: [(1∗1+2∗0+5∗0+6∗−1)+0,(2∗1+3∗0+6∗0+7∗−1)+0][(1*1 + 2*0 + 5*0 + 6*-1) + 0, (2*1 + 3*0 + 6*0 + 7*-1) + 0] = [−5,−5][-5, -5], [(5∗1+6∗0+9∗0+10∗−1)+0,(6∗1+7∗0+10∗0+11∗−1)+0][(5*1 + 6*0 + 9*0 + 10*-1) + 0, (6*1 + 7*0 + 10*0 + 11*-1) + 0] = [−5,−5][-5, -5],
    • ReLU: [max⁡(0,−5),max⁡(0,−5)][\max(0, -5), \max(0, -5)] = [0,0][0, 0], [max⁡(0,−5),max⁡(0,−5)][\max(0, -5), \max(0, -5)] = [0,0][0, 0]
    • Max Pooling: [max⁡(0,0)][\max(0, 0)] = [0][0] 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
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
2D CNN Forward Pass - Medium | PixelBank