Simple Convolutional 2D Layer

MediumML Coding

Problem statement

Implement simple_conv2d(input_matrix, kernel, padding, stride), the forward pass of a single-channel 2D convolutional layer.

  • input_matrix: a 2D numpy array of shape $H \times W$.
  • kernel: a 2D numpy array of shape $k_h \times k_w$.
  • padding: the number of rows and columns of zeros added on every side of the input.
  • stride: the step, in both directions, between consecutive kernel positions.

As in deep-learning frameworks, the kernel is not flipped (this is cross-correlation): each output entry is the sum of the elementwise product of the kernel with the input window it covers. The output has shape

$$H_{out} = \left\lfloor \frac{H + 2p - k_h}{s} \right\rfloor + 1, \qquad W_{out} = \left\lfloor \frac{W + 2p - k_w}{s} \right\rfloor + 1.$$

Return the output as a 2D numpy array of floats.

Example

Input:

import numpy as np
input_matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
kernel = np.array([[1, 0], [-1, 1]])
print(simple_conv2d(input_matrix, kernel, 1, 2).tolist())

Output:

[[1.0, 1.0, -4.0], [9.0, 7.0, -4.0], [0.0, 14.0, 16.0]]

Reasoning: padding by 1 gives a 6x6 input, so the output is $\lfloor (6 - 2)/2 \rfloor + 1 = 3$ on each side. The top-left window is [[0, 0], [0, 1]], and its product with the kernel sums to $1 \cdot 1 = 1$.

Show hint

How many valid kernel positions fit along one dimension once the input has been padded and the kernel moves in fixed steps?

Relevant roles: ML Engineer, Deep Learning Engineer, Research Scientist and Applied Scientist.

Related free problems