Implement a single-channel transposed convolution
Why it matters U-Net decoders in diffusion models and segmentation heads upsample with this op.
Transposed convolution upsamples feature maps in decoders and segmentation heads. Think of it as scattering each input pixel times the kernel into a bigger output.
Write conv_transpose2d(x, k, stride=1, padding=0). x is (H, W), k is (kh, kw). The full output has shape ((H - 1) * stride + kh, (W - 1) * stride + kw) where out[i * stride + a, j * stride + b] += x[i, j] * k[a, b]. Then crop padding rows and columns from every side. Return the result.
def conv_transpose2d(x, k, stride=1, padding=0)Example
x = [[1, 2]], k = [[1, 1]], stride 1 gives [[1, 3, 2]].
Understand the idea
A transposed convolution makes a feature map bigger. Each input pixel stamps a copy of the kernel, multiplied by its value, onto the output, and where stamps overlap their values add up. The stride sets how far apart the stamps land. Worked example: x = [[1, 2], [3, 4]], k = [[1, 1], [1, 1]], stride 1. The full output is 3 by 3. The centre cell is touched by all four stamps: 1 + 2 + 3 + 4 = 10. The full result is [[1, 3, 2], [4, 10, 6], [3, 7, 4]]. With padding 1 you crop one row and column from every side, leaving [[10]]. Rule: full shape is ((H - 1) * stride + kh, (W - 1) * stride + kw); for every input (i, j) and kernel (a, b), out[i * stride + a, j * stride + b] += x[i, j] * k[a, b]; then crop padding from all four sides. Always add, never assign, or overlaps lose contributions. Used in: decoders of autoencoders, U-Net upsampling, and older GAN generators.
Shapes and data flow
x (H, W), k (kh, kw) out ((H-1)s + kh, (W-1)s + kw), crop p each side x[i, j] * k -> window at (i s, j s)