liveBook · Manning

Convolutional layer

As explained in Learn Generative AI with PyTorch

A PyTorch convolution layer that applies learned 2D filters to an input tensor to produce feature maps.

A convolutional layer applies learned filters across an input image or tensor to extract spatial patterns and produce feature maps. Unlike a dense layer that connects every input value to every output, it reuses the same filter weights as the filter moves across different areas. section 4.2.1 section 4.5.1

In PyTorch, nn.Conv2d configures the input channels, output channels, kernel size, stride, padding, and optionally bias. Input channels describe the image data; output channels describe how many feature types the layer can extract. section 4.2.1 section 4.5.1

A two-by-two filter over a three-by-three image

Represent a grayscale image as shape (1, 1, 3, 3), then create nn.Conv2d(in_channels=1, out_channels=1, kernel_size=2, stride=1). Setting the layer weights to [[1, 2], [3, 4]] with zero bias produces a 2-by-2 output. Each output value comes from element-wise multiplication of the filter and the current image patch, followed by summation.

Tune the layer by changing these arguments:

  • Set in_channels to 1 for grayscale input or 3 for RGB input. section 4.2.1
  • Set out_channels to choose how many learned feature maps the layer produces. section 4.2.1
  • Increase stride to move the filter farther between positions and downsample the image; use padding to control border handling. section 7.4.1 section 4.5.1

Convolutional layers commonly appear in image classifiers and generative models. A DCGAN discriminator stacks them with batch normalization and LeakyReLU, while a VAE encoder uses them to compress images into latent vectors. Transposed convolutional layers reverse this spatial progression when a decoder or generator reconstructs images. section 4.5.1 section 7.4.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains sliding-filter arithmetic, tensor shapes, parameters, learned weights, stride, padding, and multi-layer architectures.
Examples
Uses a hand-weighted two-by-two filter, a DCGAN discriminator, and a VAE encoder to demonstrate outputs and architecture.
What it assumes
Assumes basic PyTorch tensors and image channels while teaching filter movement, convolution arithmetic, and layer parameters.
What you can do after
Build it from scratch using PyTorch nn.Conv2d, configure tensor channels and spatial movement, and inspect resulting feature maps.
How it connects
Connects convolutional layers with feature maps, filters, batch normalization, activation functions, dense layers, transposed convolution, GANs, and VAEs.

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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.