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: (1⋅1+2⋅0+5⋅0+6⋅−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), 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)] 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] = [−5,−5], [(5∗1+6∗0+9∗0+10∗−1)+0,(6∗1+7∗0+10∗0+11∗−1)+0] = [−5,−5],
- ReLU: [max(0,−5),max(0,−5)] = [0,0], [max(0,−5),max(0,−5)] = [0,0]
- Max Pooling: [max(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
Background Knowledge
The 2D CNN forward pass involves a series of operations that are fundamental to Convolutional Neural Networks (CNNs). The first step is convolution, where a small kernel (or filter) slides over the entire input image, computing the dot product at each position to generate a feature map. This process is equivalent to applying a set of learnable filters to the input data. The bias term is added to the result of the dot product to shift the activation curve.
The output of the convolution operation then passes through an activation function, in this case, ReLU (Rectified Linear Unit). ReLU sets all negative values to 0 and all positive values remain unchanged, which helps introduce non-linearity into the model. This is crucial because it allows the model to learn more complex representations of the input data.
Following the activation function, max pooling is applied. Max pooling reduces the spatial dimensions of the feature map by taking the maximum value across each window (in this case, a 2x2 window with a stride of 2). This operation helps reduce the number of parameters and computations in the network, making it more efficient and less prone to overfitting.
Algorithm/Approach
The general approach to solving this problem involves implementing each of the three main operations in sequence: convolution, ReLU activation, and max pooling. This requires understanding how to slide a kernel over an input image, compute the dot product plus bias at each position, apply the ReLU function element-wise, and then perform max pooling over the resulting feature map.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.