Convolutions
Images have structure: a pixel means most next to its neighbours. A convolution slides a small filter over the image and responds where its pattern appears, with the same few weights everywhere.
import torch
from torch import nn
conv = nn.Conv2d(1, 4, kernel_size=3)
# batch, channels, height, width
images = torch.zeros(8, 1, 12, 12)
print(conv(images).shape)
# torch.Size([8, 4, 10, 10])Without padding a 3×3 filter loses one pixel on each side: the size becomes (size - kernel + 2 * padding) // stride + 1. A pooling layer then shrinks the map, and a final linear layer turns it into class scores.
Your task
Write conv_size(size, kernel, padding=0, stride=1) giving a convolution's output size. Write stripes(n, seed) returning (images, labels): n images of shape (1, 12, 12) with a random-noise background (0.1 * torch.rand) and one bright line of 1.0 at a random position, horizontal for label 0 and vertical for label 1. Write train_cnn(images, labels, steps=150) returning a trained small CNN that tells them apart.
The explanation opens here once every check passes.