liveBook · Manning

Convolutional layer

As explained in Deep Learning with PyTorch, Second Edition

A PyTorch 2D convolution layer module that applies learned filters over spatial input regions.

A convolutional layer looks for local patterns in an image instead of connecting every output to every input pixel. This helps a classifier use fewer parameters and recognize a pattern when it appears in different positions. In PyTorch, `nn.Conv2d` is the module for two-dimensional images. section 7.2.6 section 8.1.1 section 8.2

The layer moves a small weight matrix, called a kernel, across the input. At each position, it multiplies nearby input values by kernel weights and adds the results to make an output value. It reuses those weights at other positions. For an RGB image, each filter has weights for all three input channels; its weights and bias can be learned through backpropagation. section 8.1.1 section 8.2.2

One layer and its shapes

`conv = nn.Conv2d(3, 16, kernel_size=3)` takes three input channels and makes 16 output channels using 3 × 3 filters. Its weight shape is `[16, 3, 3, 3]`, and its bias shape is `[16]`. Given an input shaped `[1, 3, 32, 32]`, this unpadded layer returns `[1, 16, 30, 30]`: the first dimension is the batch size. section 8.2

A filter's weights determine what its output highlights:

  • Equal weights averaging a neighborhood blur the image. section 8.2.2
  • Weights that subtract pixels on the left from pixels on the right highlight vertical edges. These hand-set filters illustrate effects that a network normally learns for its task. section 8.2.2

Small kernels need help seeing larger patterns. A convolutional network can stack layers, with activations and pooling between them, so later layers work with features covering larger portions of the original image. In a bird-versus-airplane classifier, two padded convolutions and two max-pooling layers reduce a 32 × 32 image to 8-channel, 8 × 8 features. Flatten those features before passing them to `nn.Linear`; connecting the linear layer without flattening causes a shape error. section 8.2.3 section 8.2.4 section 8.3.1

# In[27]:
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
        self.act1 = nn.Tanh()
        self.pool1 = nn.MaxPool2d(2)
        self.conv2 = nn.Conv2d(16, 8, kernel_size=3, padding=1)
        self.act2 = nn.Tanh()
        self.pool2 = nn.MaxPool2d(2)
        self.fc1 = nn.Linear(8 * 8 * 8, 32)
        self.act3 = nn.Tanh()
        self.fc2 = nn.Linear(32, 2)

    def forward(self, x):
        out = self.pool1(self.act1(self.conv1(x)))
        out = self.pool2(self.act2(self.conv2(out)))
        out = out.view(-1, 8 * 8 * 8)     #1
        out = self.act3(self.fc1(out))
        out = self.fc2(out)
        return out

The complete `nn.Module` example shows where the two `nn.Conv2d` layers sit and how `view` flattens pooled features before the linear layers. section 8.3.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through neighborhood weighting, learned parameters, tensor shapes, hand-set filters, and a two-layer network; leaves continuous convolution aside.
Examples
Uses a CIFAR-10 bird image, averaging and edge-detection kernels, tensor-shape checks, and a bird-versus-airplane network.
What it assumes
Assumes familiarity with tensors, image channels, and neural-network layers while developing the kernel operation from nearby-pixel intuition.
What you can do after
Apply it via PyTorch `nn.Conv2d` to assemble image-classification layers and track their channel and spatial shapes.
How it connects
Links convolution to fully connected layers, translation invariance, feature detection, activation functions, max pooling, and linear layers.

Also explained in

Compare how 14 books explain convolutional layer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.