← back

Simple Convolutional 2D Layer

#41 · Deep Learning · Medium

⊣ Solve on deep-ml.com

Problem

Implement a simple 2D convolutional layer. Given an input matrix (single-channel 2D image) and a kernel (filter), perform a valid 2D convolution by sliding the kernel over the input and computing element-wise products.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np

def simple_conv2d(input_matrix, kernel, stride=1, padding=0):
    input_matrix = np.array(input_matrix, dtype=np.float64)
    kernel = np.array(kernel, dtype=np.float64)

    if padding > 0:
        input_matrix = np.pad(input_matrix, padding, mode='constant', constant_values=0)

    in_h, in_w = input_matrix.shape
    k_h, k_w = kernel.shape

    out_h = (in_h - k_h) // stride + 1
    out_w = (in_w - k_w) // stride + 1

    output = np.zeros((out_h, out_w))

    for i in range(out_h):
        for j in range(out_w):
            row_start = i * stride
            col_start = j * stride
            region = input_matrix[row_start:row_start + k_h, col_start:col_start + k_w]
            output[i][j] = np.sum(region * kernel)

    return output.tolist()

Explanation

  1. Optionally pad the input with zeros to control the output spatial dimensions.
  2. Compute the output height and width from the input size, kernel size, and stride.
  3. Slide the kernel across the input. At each position, extract the region and compute the element-wise product sum.
  4. Store each result in the output matrix.

Complexity

  • Time: O(out_h out_w k_h * k_w)
  • Space: O(out_h * out_w) for the output