Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 17 — Lab: Convolutional Layers

Open in Colab

Lab Overview

You will write the arithmetic and the operations of convolutional layers in NumPy, and check them against PyTorch.

ExerciseTopicPoints
1Output size10
2A 2D convolution with padding and stride20
3A convolutional layer with channels20
4Max pooling15
5Counting parameters: dense against convolutional10
6The receptive field15
7Equivariance and global pooling10
Challengeim2col+10

How it works. Each exercise has a function to complete where you see # TODO. Run the check cell under it: ✅ means your function is right, ❌ tells you what is wrong. Use NumPy only; PyTorch appears only in the checks. Do not change the check cells.


Exercise 1: Output Size (10 points)

out_size(n, k, p, s) is the output size of a convolution (or pooling) with kernel k, padding p and stride s on an input of size n: ⌊(n+2p−k)/s⌋+1\lfloor (n + 2p - k)/s \rfloor + 1.


Exercise 2: A 2D Convolution (20 points)

conv2d(img, K, stride, padding) takes a 2D image and a square kernel, pads the image with padding zeros on every side, and returns out[i,j]=∑u,vK[u,v] img[is+u, js+v]\text{out}[i, j] = \sum_{u,v} K[u, v]\, \text{img}[i s + u, \, j s + v] (no kernel flip, as in deep learning libraries).


Exercise 3: A Convolutional Layer (20 points)

conv_layer(x, W, b, stride, padding): x has shape (C_in, H, W), the filters W have shape (C_out, C_in, k, k) and the biases b shape (C_out,). Output channel o is the sum over the input channels c of conv2d(x[c], W[o, c]), plus b[o]. You may use your conv2d.


Exercise 4: Max Pooling (15 points)

max_pool(x, k, stride): x has shape (C, H, W); return the maximum of every k × k window, moved by stride, for each channel.


Exercise 5: Counting Parameters (10 points)

dense_params(n_in, n_out) counts a dense layer’s weights and biases; conv_params(c_in, c_out, k) a convolutional layer’s.


Exercise 6: The Receptive Field (15 points)

receptive_field(layers) takes a list of (kernel, stride) pairs, from the input up, and returns the receptive field (in input pixels, along one side) of a unit after the last layer. Start from 1; each layer adds (kernel - 1) * jump, where jump is the product of the strides of the layers before it.


Exercise 7: Equivariance and Global Pooling (10 points)

global_max_pool(x) takes feature maps (C, H, W) and returns a vector of length C: each channel’s maximum. Then run the check: shifting an image shifts a convolution’s output (equivariance), and the global maximum does not change (invariance).


Challenge: im2col (+10 points)

im2col(x, k) takes (C, H, W) and returns a matrix with one row per output position (row by row, stride 1, no padding), each row the flattened C × k × k patch. Then the whole layer is im2col(x, k) @ W.reshape(C_out, -1).T + b.


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: Exercise 7’s last check shows that stride-2 pooling gives a different result when the image moves by one pixel. Explain why, and what it means for a network that uses stride-2 pooling.