Ch 8 ▸ Upsampling and transposed convolution ▸ Module 3/4
Why “transposed”.
🔒 This module is locked
It builds on Transposed convolution. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.
The name “transposed convolution” comes from writing a convolution as a matrix. It’s worth doing once, because it shows that the transposed convolution and chapter 7’s input gradient are the same operation.
A convolution is a matrix
Chapter 1 flattened an image into a list of numbers, and a dense layer multiplied that list by a matrix of weights: one row per output, one column per input. A convolution is a matrix multiplication too, just with a very particular matrix.
Take a 3×3 convolution with no padding at stride 1 on a 4×4 image, as in chapter 2’s example. Call the kernel’s weights a to i, row by row. Flatten the image into a list of 16 pixels and the output into a list of 4. Then the convolution is
where has 4 rows, one per output, and 16 columns, one per pixel. Each row holds the nine weights in the columns of the nine pixels that output reads, and 0 in the other seven columns, because the output doesn’t use those pixels. The four rows hold the same nine weights, shifted along: that’s weight sharing, written as a matrix. A dense layer would have 64 different weights here. The convolution’s matrix uses 9, repeated, and fills the rest with zeros.
Its transpose
The transpose of a matrix swaps its rows and columns: row of is column of . is 4 × 16, so is 16 × 4. It takes 4 numbers and gives back 16.
Read row of . It’s column of : it lists every output that read pixel , and the weight it read it through. So multiplying a list of 4 values by gives pixel the sum of (value × weight) over the outputs that read it. That’s chapter 7’s input gradient exactly, with : in chapter 7’s terms, the input gradient is .
Now read it the other way, by columns of . Column of is row of : the kernel’s weights, placed on output ‘s window. Multiplying by adds times that column into the result, for every . That’s stamping: each value times the kernel, added onto its window. So a transposed convolution with a given kernel and stride is multiplication by , where is the convolution with the same kernel and stride.
KERNEL
IMAGE · 5×5
OUTPUT · 2×2
▸ ROW y00: OUTPUT (0, 0) = THE 9 OUTLINED PIXELS × WEIGHTS a TO i. EVERY OTHER PIXEL GETS 0.
Figure 8.3 starts with a 3×3 convolution at stride 2 on a 5×5 image, which also has a 2×2 output. is 4 × 25, and , 25 × 4, takes a 2×2 map to a 5×5 one: the transposed convolution of the last module. Click the rows of to see each output’s window. Then switch to and click the pixels. The middle pixel, (2, 2), is read by all four outputs, each through a different weight, and that’s why it collected all four stamps in the last module. The corners are read by one output each.
Not an inverse
A transposed convolution restores the size of a convolution’s input, not its values. Transposing a matrix isn’t the same as inverting it, and has no inverse to begin with: it turns 25 numbers into 4, so many different images give the same 4 outputs. A transposed convolution’s weights are learned to fill in whatever helps the network’s final answer. Some papers call this layer a “deconvolution”, but that name is misleading, since undoing a convolution is a different problem.
The size can’t always be restored exactly either. With a 3×3 kernel at stride 2, chapter 3’s formula sends both a 5×5 and a 6×6 image to a 2×2 output, because of the rounding down: ⌊(5 − 3) / 2⌋ + 1 = 2 and ⌊(6 − 3) / 2⌋ + 1 = 2. The transposed convolution can only give one size back, . Output padding is for exactly this: it picks the 6. The last module’s one-short case is the same rounding. A 3×3 convolution at stride 2 with padding 1 sends both a 31×31 and a 32×32 image to 16×16, since ⌊(31 + 2 − 3) / 2⌋ + 1 = 16 and ⌊(32 + 2 − 3) / 2⌋ + 1 = 16, so going back from 16 gives 31 unless output padding adds the missing row and column.
Knowledge check ▸ 5 questions
Before you move on.
Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.
1A 3×3 convolution with no padding at stride 1 runs on a 6×6 image. Written as a matrix M, how many rows does M have?
2How many entries of each row of that M are weights rather than structural zeros?
3In figure 8.3’s stride-2 convolution on a 5×5 image, how many outputs read pixel (0, 2)?
4Chapter 7’s input gradient and a transposed convolution with the same kernel are both multiplication by what?
5Does a transposed convolution undo a convolution with the same kernel?