liveBook · Manning

Transposed convolution

As explained in Learn Generative AI with PyTorch

The 2D computation performed by a transposed convolutional layer to expand an image or feature map.

A 2D transposed convolution expands an image or feature map by applying a learned filter as it places input values into a larger output. Standard convolution is used here to extract features and can reduce spatial size; transposed convolution instead builds higher-resolution feature maps. It is also called deconvolution or an upsampling layer. section 4.3 section 4.2

Upsample a 2×2 image

Start with [[1, 0], [2, 3]] and a 2×2 filter [[2, 3], [4, 5]]. In PyTorch, use nn.ConvTranspose2d(1, 1, kernel_size=2, stride=2) and set its weights to that filter with a zero bias. Each input value multiplies the whole filter and contributes a block to the output: the top-left 1 contributes [[2, 3], [4, 5]], while the top-right 0 contributes zeros. The result has shape (1, 1, 4, 4). A larger stride produces more upsampling. section 4.3.1 section 4.3

tensor([[[[ 2.,  3.,  0.,  0.],
         [ 4.,  5.,  0.,  0.],
         [ 4.,  6.,  6.,  9.],
         [ 8., 10., 12., 15.]]]], grad_fn=<ConvolutionBackward0>)

Inspect the complete 4×4 output to see where each input value contributes its filter-sized block. section 4.3.1

For a DCGAN image generator, stack five ConvTranspose2d layers to mirror the discriminator’s convolutional layers. A 100-value noise vector enters the generator; its final transposed convolution outputs three channels for an RGB image. Batch normalization and ReLU follow intermediate layers, and Tanh brings the final values into the training images’ [−1, 1] range. section 4.5.1

Listing 4.5 Designing a generator in DCGAN
G=nn.Sequential(
    nn.ConvTranspose2d(100, 512, 4, 1, 0, bias=False),    #1
    nn.BatchNorm2d(512),
    nn.ReLU(inplace=True),
    nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),    #2
    nn.BatchNorm2d(256),
    nn.ReLU(inplace=True),
    nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
    nn.BatchNorm2d(128),
    nn.ReLU(inplace=True),
    nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False),
    nn.BatchNorm2d(64),
    nn.ReLU(inplace=True),
    nn.ConvTranspose2d(64, 3, 4, 2, 1, bias=False),       #3
    nn.Tanh()).to(device)                                 #4

Follow the generator listing from its 100 input channels through successive transposed convolutions to three output channels. section 4.5.1

The same operation can reconstruct images or expand other structured outputs. A VAE decoder uses three transposed convolutions to turn an encoding back into a 256×256 color image; a music generator uses them to extend noise vectors across bars. The practical choice is to check each layer’s channels and output shape against the result the generator needs. section 7.4.1 section 13.4.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Works through a hand-weighted 2×2 calculation and output shape, then shows a multilayer PyTorch generator.
Examples
Uses a 2×2 numerical image and a DCGAN generator for RGB anime faces.
What it assumes
Assumes basic GAN familiarity while teaching the upsampling calculation from a small image.
What you can do after
Apply it via PyTorch nn.ConvTranspose2d to expand feature maps and assemble an image generator.
How it connects
Links convolution, stride, GAN generators, VAE decoders, and batch normalization.

Also explained in

Compare how 5 books explain transposed convolution →

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.