Cross Correlation: the filter is not flipped, so the output is shifted by one pixel compared to convolution. In practice, most deep learning libraries implement cross-correlation instead of convolution, but they are often referred to as “convolutional layers” for simplicity.

Convolution Hyperparameters:

  • Kernel Size: the dimensions of the filter (e.g., 3x3, 5x5). This determines how many neighboring pixels are considered when computing the output for each position. A larger kernel can capture more complex patterns but also increases computational cost and may lead to overfitting.
  • Stride: the step size with which the filter moves across the input. A stride of 1 means the filter is applied at every pixel, while a stride of 2 means it is applied every other pixel, effectively downsampling the output.
  • Padding: adding extra pixels around the border of the input to control the spatial dimensions of the output. “Same” padding adds enough zeros to keep the output size the same as the input size, while “valid” padding means no padding is added, resulting in a smaller output size.
  • Number of Filters: the number of different filters applied to the input, which determines the depth of the output feature map. Each filter learns to detect a different feature in the input data, such as edges, textures, or more complex patterns in deeper layers.
  • Dilation: the spacing between elements in the kernel, which allows the filter to capture larger context without increasing the kernel size. A dilation rate of 1 means no spacing (standard convolution), while a dilation rate of 2 means there is a one-pixel gap between each element in the kernel, effectively increasing the receptive field.

Pooling Layers:

  • Why pool? Summarizes features in a local region, making the network less sensitive to the exact microsocpic location of a feature, thereby providing translation invariance.
  • Max pooling: takes the maximum value in each local region, which helps to capture the most prominent features and provides robustness to noise.
  • Average pooling: takes the average value in each local region, which can help to smooth the
  • Global average pooling: takes the average across the entire spatial dimensions, reducing each feature map to a single value. This is often used before the final classification layer to create a fixed-size output regardless of the input size.
import numpy as np
class CNN:
    def __init__(self, num_filters, filter_size):
        self.num_filters = num_filters
        self.filter_size = filter_size
        self.filters = np.random.randn(num_filters, filter_size, filter_size) / (filter_size * filter_size)
    def relu(self, x):
        return np.maximum(0, x)
    def convolve(self, input):
        h, w = input.shape
        output = np.zeros((h - self.filter_size + 1, w - self.filter_size + 1, self.num_filters))
        for f in range(self.num_filters):
            for i in range(h - self.filter_size + 1):
                for j in range(w - self.filter_size + 1):
                    output[i, j, f] = np.sum(input[i:i+self.filter_size, j:j+self.filter_size] * self.filters[f])
        return self.relu(output)