Start learning free

LESSON 145 OF 165 · MACHINE LEARNING: DEEP LEARNING WITH PYTORCH

A Network That Looks: Convolutions in Python

Practice

The plain network reads each digit as one row of 64 numbers. It doesn't know which pixels are neighbors in the picture, and it needs separate weights to spot a stroke in each place. A convolutional network (CNN) keeps the picture's shape and slides small filters over it.

The shape: a convolution reads images shaped (images, channels, rows, columns). X_train.reshape(-1, 1, 8, 8) gives back the same numbers in that shape: 1 channel because the images are gray, and -1 lets PyTorch work out the count, 1,347. X_test gets the same shape.

The layers: nn.Conv2d(1, 8, 3, padding=1) slides 8 filters of 3x3 over the picture; each gives back an 8x8 map of where its pattern shows up (padding=1 adds a border of zeros so the maps stay 8x8). nn.MaxPool2d(2) keeps the biggest number of every 2x2 square, so each map shrinks to half its size. nn.Flatten() lays everything out as one row per image, the shape nn.Linear reads. You can try a layer on its own:

pool = nn.MaxPool2d(2)
x = torch.ones(1, 3, 6, 6)            # 1 image, 3 channels, 6x6
print(pool(x).shape)                  # torch.Size([1, 3, 3, 3])
print(nn.Flatten()(pool(x)).shape)    # torch.Size([1, 27])

The starting code has train(model) and report(name, model), which trains a model, counts its weights and prints its test accuracy. When you're done, the program prints this. The shapes come from the lesson's check, which sends one test image through cnn:

Plain network: weights: 2410 test accuracy: 0.967
Convolutions:  weights: 1370 test accuracy: 0.984
One test image through cnn, layer by layer:
  Conv2d [1, 8, 8, 8]
  ReLU [1, 8, 8, 8]
  MaxPool2d [1, 8, 4, 4]
  Flatten [1, 128]
  Linear [1, 10]

Your task: reshape X_train and X_test into images, then build cnn with nn.Sequential: a Conv2d with 8 filters of 3x3 and padding=1, ReLU, MaxPool2d(2), Flatten and a Linear from 8 * 4 * 4 numbers to 10 scores. Report it as "Convolutions: ".


Assignment
Follow the Coding Tutorial to build a small convolutional network and compare it with the plain one.


Hint
Put one layer per line inside nn.Sequential( … ), each ending with a comma. After pooling, each image is 8 maps of 4x4, so the last layer reads 8 * 4 * 4 = 128 numbers.


STEP 1 OF 7
    Ctrl + Enter to check

    Pt Cnn in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free