PythiloLesson 22 of 30 · progress kept in this browser

Pooling and a small CNN

A convolutional network alternates convolutions, which detect local patterns, with pooling, which shrinks the image. nn.MaxPool2d(2) keeps the largest value of each 2 by 2 patch, halving height and width, so later layers see a wider area for the same cost.

import torch
from torch import nn

x = torch.randn(1, 8, 12, 12)
print(nn.MaxPool2d(2)(x).shape)
pool = nn.AdaptiveAvgPool2d(2)
for size in (12, 20):
    x = torch.randn(1, 8, size, size)
    print(size, pool(x).shape)

nn.AdaptiveAvgPool2d(k) averages into a fixed k by k grid whatever the input size, so the linear head behind it works for images of any size. With k = 1 each channel becomes one number: the network knows what it saw, but no longer where. That is right for "is there a cat?", wrong when the position is the answer.

Zero padding gives a network a hint of where the edges are. padding_mode="circular" wraps the image around instead, so every position is treated alike.

Your task

Using the given quadrant_data and train, write SmallCNN(classes): self.features is nn.Sequential(nn.Conv2d(1, 8, 3, padding=1, padding_mode="circular"), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(8, 16, 3, padding=1, padding_mode="circular"), nn.ReLU(), nn.AdaptiveAvgPool2d(2)) and self.head is nn.Linear(64, classes); forward flattens the features before the head. Write count_parameters(model).

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.