# Transposed convolution

> The learnable upsampling layer: each input pixel stamps a scaled copy of the kernel into the output at stride s. It is the transpose (adjoint) of a strided convolution, not its inverse, and when s does not divide k the uneven overlaps cause checkerboard artifacts.

Source: https://www.mlfoundry.in/learn/transposed-convolution  
Section: Deep Learning › Convolutional Neural Networks  
Level: core · 12 min read · updated 2026-10-11

**Transposed convolution:** (also **fractionally-strided convolution**, often mis-called **deconvolution**) is the linear map whose matrix is the transpose of a strided convolution's matrix. If a stride-$s$ convolution maps a large input $z$ to a small output $Cz$, the transposed convolution with the same kernel maps a small input $x$ to a large output $C^{\top}x$. Mechanically, every input value $x[i]$ multiplies the whole kernel and is **added** into the output starting at position $s\,i$.

Encoder-decoder networks need a layer that goes the other way: from a coarse feature map back to a fine one. Fixed interpolation (nearest, bilinear) can do this but cannot learn. A transposed convolution is the learnable version, and it also appears automatically in every CNN, because it is exactly what backpropagation computes for the gradient of a strided convolution with respect to its input.

The name "deconvolution" comes from Zeiler, Krishnan, Taylor and Fergus's *Deconvolutional Networks* (2010) and from Zeiler and Fergus's 2014 "deconvnet" visualisations, and the term stuck. It is a misnomer: deconvolution in signal processing means *inverting* a convolution, while $C^{\top}$ is only the adjoint and does not undo $C$. Long, Shelhamer and Darrell's FCN (CVPR 2015) called it "backwards strided convolution" and used it, initialised to bilinear interpolation, to upsample segmentation scores; Dumoulin and Visin's *A guide to convolution arithmetic for deep learning* (2016) fixed the modern name and the shape rules. Radford, Metz and Chintala's DCGAN (ICLR 2016) built its generator entirely from them, and U-Net (Ronneberger, Fischer and Brox, 2015) uses $2\times2$ "up-convolutions" in its decoder.

## Formula and output size

$$
y[o]=\sum_{i}\sum_{t=0}^{k-1}\mathbb 1\big[s\,i+t-p=o\big]\;w[t]\,x[i],\qquad n_{\text{out}}=(n-1)\,s-2p+k+\text{output\_padding}
$$

- $s$: stride: how far apart consecutive input pixels stamp their kernel copies in the output (the upsampling factor)
- $p$: padding: here it **crops** $p$ pixels from each side of the full output (the opposite of padding in a convolution)
- $k$: kernel size; each input pixel touches $k$ outputs per axis
- $\text{output\_padding}$: extra rows/columns added on one side only, $0\le$ value $<s$, to choose among the input sizes that a stride-$s$ conv maps to the same $n$

For 2-D the same holds per axis. Common configurations: $k=4,\ s=2,\ p=1$ gives exactly $2n$ ($4\to8$; the DCGAN layer); $k=2,\ s=2,\ p=0$ also gives $2n$ with no overlap (U-Net). The $3\times3$, stride-2, padding-1 convolution maps both $13$ and $14$ to $7$, so its transpose is ambiguous: $(7-1)\cdot2-2+3=13$, and `output_padding=1` selects $14$.

## Three equivalent views

**The same operation, three ways**

1. **Scatter.** For each input pixel, add $x[i]\cdot w$ into the output at offset $s\,i$; overlapping stamps are summed; then crop $p$ from each side.
2. **Insert zeros, then convolve.** Put $s-1$ zeros between input pixels (length $(n-1)s+1$), pad each side with $k-1-p$ zeros (plus `output_padding` on one side), and run an ordinary stride-1 convolution with the **flipped** kernel. The length works out to $(n-1)s-2p+k+\text{op}$. This is why it is called fractionally strided: it behaves like a convolution with stride $1/s$.
3. **Matrix transpose.** Write the stride-$s$ conv as a sparse matrix $C$ (one row per output, the kernel shifted by $s$ per row). The transposed conv multiplies by $C^{\top}$. For any $z$ and $x$, $\langle Cz,\,x\rangle=\langle z,\,C^{\top}x\rangle$.

The third view gives the gradients for free. If $y=C^{\top}x$ and $g=\partial L/\partial y$, then $\partial L/\partial x=C\,g$: the backward pass of a transposed convolution is an ordinary strided convolution with the same kernel, and vice versa. The weight gradient is $\partial L/\partial w[t]=\sum_i x[i]\,g[s\,i+t-p]$.

$$
y=C^{\top}x\quad\Longrightarrow\quad \frac{\partial L}{\partial x}=C\,\frac{\partial L}{\partial y},\qquad \frac{\partial L}{\partial w[t]}=\sum_i x[i]\;\frac{\partial L}{\partial y[s\,i+t-p]}
$$

- $C$: the matrix of the stride-$s$ convolution with kernel $w$, shape $n\times n_{\text{out}}$
- $C^{\top}$: its transpose: the transposed convolution, shape $n_{\text{out}}\times n$

## Worked example in 1-D

Let $x=(1,2,3)$, $w=(1,2,1)$, $s=2$, $p=0$. The output length is $(3-1)\cdot2+3=7$. Input 0 stamps $(1,2,1)$ at positions 0–2, input 1 stamps $(2,4,2)$ at 2–4, input 2 stamps $(3,6,3)$ at 4–6:

Scatter-add of three kernel stamps (verified in Node against the figure's library)

| output position | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
| --- | --- | --- | --- | --- | --- | --- | --- |
| x[0]·w | 1 | 2 | 1 |  |  |  |  |
| x[1]·w |  |  | 2 | 4 | 2 |  |  |
| x[2]·w |  |  |  |  | 3 | 6 | 3 |
| y (sum) | 1 | 2 | 3 | 4 | 5 | 6 | 3 |
| stamps overlapping | 1 | 1 | 2 | 1 | 2 | 1 | 1 |

Positions 2 and 4 receive two stamps, the others one: with $k=3$ and $s=2$ the overlap alternates $1,2,1,2$. With $p=1$ the output is cropped to $(2,3,4,5,6)$, and with $p=1$, `output_padding=1` it is $(2,3,4,5,6,3)$. The zero-insertion view gives the same numbers: $(0,0,1,0,2,0,3,0,0)$ correlated with the (here symmetric) flipped kernel.

**Adjoint check and gradients, upstream gradient g = (1, 0, 2, 1, 0, 1, 3)**

1. The stride-2 conv of $g$ with $w$ reads windows $g[0..2],\,g[2..4],\,g[4..6]$: $Cg=(3,4,5)$.
2. Adjoint identity: $\langle Cg,x\rangle=3+8+15=26$ and $\langle g,C^{\top}x\rangle=1+0+6+4+0+6+9=26$.
3. So for $L=\langle g,y\rangle$ the input gradient is $\partial L/\partial x=Cg=(3,4,5)$: a strided convolution.
4. Weight gradient: $\partial L/\partial w[t]=\sum_i x[i]\,g[2i+t]$ gives $(1+4+0,\;0+2+3,\;2+0+9)=(5,5,11)$.

## Checkerboard artifacts

Feed an all-ones input through an all-ones kernel and every output pixel counts how many stamps land on it. Away from the border these counts take the values $\lfloor k/s\rfloor$ and $\lceil k/s\rceil$ per axis, so they are uniform only if $s$ divides $k$. In 2-D the count map is the outer product of the 1-D counts: for $k=3,\ s=2$ the interior alternates $1,2,4$, a checkerboard with a 4:1 ratio. Odena, Dumoulin and Olah ("Deconvolution and Checkerboard Artifacts", Distill 2016) showed that GAN and super-resolution outputs inherit exactly this pattern, and that stacking layers compounds it at several scales.

> Figure: A 4×4 all-ones input through ConvTranspose2d with an all-ones kernel: each output cell shows how many kernel stamps overlap there. The default k = 3, s = 2, p = 0 gives a 9×9 output, (4−1)·2 − 0 + 3 = 9, and the dashed interior alternates between 1, 2 and 4 overlaps (max/min 4.00): the checkerboard. Click an input cell to see the 3×3 block of outputs it stamps, or an output cell to see which inputs it sums. Set k = 4 (k mod s = 0) and the interior becomes a uniform 4; the k4 s2 p1 preset gives an exact 8×8 doubling. Switch to resize-conv (nearest-neighbour ×2, then a 3×3 conv) and every interior output reads exactly 9 taps.

Choosing $k$ divisible by $s$ ($k=4,\ s=2$; $k=2,\ s=2$) evens out the overlap, but the network can still learn uneven kernels that bring the artifacts back. Odena et al. recommend **resize-convolution** instead: upsample with nearest-neighbour or bilinear interpolation, then apply an ordinary stride-1 convolution. Every output then reads the same number of taps by construction. A third option is **sub-pixel convolution** (pixel shuffle, Shi et al. 2016): a stride-1 conv produces $s^2$ times the channels and `nn.PixelShuffle` rearranges them into an $s\times$ larger map; it is equivalent to a transposed convolution with a kernel of size $s\cdot k'$, and benefits from an initialisation that makes the $s^2$ sub-kernels identical (ICNR, Aitken et al. 2017).

```python
import torch, torch.nn as nn, torch.nn.functional as F

# Note the weight layout: (in_channels, out_channels // groups, kH, kW), reversed vs Conv2d
up = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1)
print(up.weight.shape)                           # torch.Size([512, 256, 4, 4])
print(up(torch.randn(1, 512, 8, 8)).shape)       # torch.Size([1, 256, 16, 16])
print(sum(p.numel() for p in up.parameters()))   # 2097408

# output_padding picks between the sizes a stride-2 conv would collapse
t = nn.ConvTranspose2d(8, 8, 3, stride=2, padding=1)
print(t(torch.randn(1, 8, 7, 7)).shape)                     # 13 x 13
print(t(torch.randn(1, 8, 7, 7), output_size=(14, 14)).shape)  # 14 x 14 (sets output_padding=1)

# 1-D worked example
x = torch.tensor([[[1., 2, 3]]])
w = torch.tensor([[[1., 2, 1]]])
print(F.conv_transpose1d(x, w, stride=2).flatten().tolist())  # [1, 2, 3, 4, 5, 6, 3]

# Adjoint: <conv(g), x> == <g, conv_transpose(x)>
g = torch.tensor([[[1., 0, 2, 1, 0, 1, 3]]])
print((F.conv1d(g, w, stride=2) * x).sum(), (g * F.conv_transpose1d(x, w, stride=2)).sum())  # 26, 26

# Checkerboard-free alternative (Odena et al., 2016): resize, then convolve
resize_conv = nn.Sequential(nn.Upsample(scale_factor=2, mode="nearest"),
                            nn.Conv2d(512, 256, kernel_size=3, padding=1))
print(resize_conv(torch.randn(1, 512, 8, 8)).shape)  # torch.Size([1, 256, 16, 16])

# DCGAN-style generator (PyTorch-tutorial widths): 1x1 -> 4 -> 8 -> 16 -> 32 -> 64
G = nn.Sequential(
    nn.ConvTranspose2d(100, 512, 4, 1, 0), nn.BatchNorm2d(512), nn.ReLU(True),
    nn.ConvTranspose2d(512, 256, 4, 2, 1), nn.BatchNorm2d(256), nn.ReLU(True),
    nn.ConvTranspose2d(256, 128, 4, 2, 1), nn.BatchNorm2d(128), nn.ReLU(True),
    nn.ConvTranspose2d(128, 64, 4, 2, 1),  nn.BatchNorm2d(64),  nn.ReLU(True),
    nn.ConvTranspose2d(64, 3, 4, 2, 1),    nn.Tanh())
print(G(torch.randn(2, 100, 1, 1)).shape)  # torch.Size([2, 3, 64, 64])
```

nn.ConvTranspose2d: shapes, the worked example, the adjoint, and a resize-conv alternative

- **Segmentation decoders:** FCN upsamples coarse class scores with transposed convolutions initialised to bilinear interpolation (for $\times2$: $k=4$, weights $0.25,0.75,0.75,0.25$ per axis). U-Net's decoder uses $2\times2$, stride-2 up-convolutions that halve the channels before concatenating the skip connection.
- **Generators:** DCGAN's generator is a stack of $4\times4$, stride-2 transposed convolutions with **Batch normalization** and ReLU; many later GANs and VAE decoders kept the pattern or switched to resize-convolution to avoid checkerboards.
- **Backprop of every strided conv:** Autograd computes the input gradient of a strided `Conv2d` with a transposed convolution, so checkerboard patterns also appear in gradient-based visualisations and adversarial perturbations of strided networks.

> **It does not invert the convolution:** $C^{\top}C\neq I$: running a conv and then its transpose with the same weights does not recover the input, it blurs and rescales it. "Deconvolution" in the deep-learning sense is a learned upsampler that shares the convolution's *connectivity*, nothing more.

> **Shape surprises:** `padding` in `ConvTranspose2d` crops the output, so increasing it makes the output **smaller**. When matching a decoder to an encoder with odd sizes, pass `output_size=` to the forward call (or set `output_padding`, which must be smaller than the stride or the dilation). And remember the weight tensor is `(C_in, C_out/groups, kH, kW)`.

> **Why s ∤ k gives a checkerboard:** Output pixel $o$ receives one tap from every input $i$ with $0\le o+p-s\,i<k$. That is $\lceil k/s\rceil$ inputs for some residues of $o$ mod $s$ and $\lfloor k/s\rfloor$ for others. Even if the learned weights compensate, the network has to spend capacity on it, and it often does so imperfectly.

> **Related:** **Stride and padding** gives the forward shape rule $\lfloor(n+2p-k)/s\rfloor+1$ that this layer inverts. **Dilated convolution** has its own transposed version (`dilation=` in `ConvTranspose2d`), used in its backward pass.

**Strengths**

- Learnable upsampling with the same parameter count as a conv
- Exactly the adjoint of a strided conv: clean shape and gradient algebra
- One fused op, fast on GPUs; k = 4, s = 2, p = 1 is a reliable exact ×2

**Limitations**

- Checkerboard artifacts when s does not divide k, and sometimes even when it does
- Confusing size rules: padding crops, output_padding disambiguates
- Not an inverse, despite the name 'deconvolution'
- Resize-conv or pixel shuffle are often preferred in modern generators and super-resolution

**Question:** A ConvTranspose2d has kernel 4, stride 2, padding 1. What output size does it produce from a 16 × 16 input?

- A. 31 × 31
- B. 32 × 32
- C. 33 × 33
- D. 34 × 34

**Answer:** B. 32 × 32 — $(16-1)\cdot2-2\cdot1+4=32$. This configuration doubles the size exactly, and since $s=2$ divides $k=4$ the overlap is uniform.

**Question:** Which kernel/stride pair gives non-uniform overlap (a checkerboard) for an all-ones input?

- A. k = 2, s = 2
- B. k = 4, s = 2
- C. k = 3, s = 2
- D. k = 3, s = 3

**Answer:** C. k = 3, s = 2 — Uniform overlap needs $s\mid k$. With $k=3,\ s=2$ outputs receive alternately 1 and 2 stamps per axis, so 1, 2 or 4 in 2-D.

**Key points**

- A transposed conv scatters $x[i]\cdot w$ into the output at stride $s$: $n_{\text{out}}=(n-1)s-2p+k+\text{output\_padding}$.
- It is the matrix transpose of a strided conv; its backward pass is that strided conv. It is not an inverse.
- Equivalent to inserting $s-1$ zeros between inputs, padding by $k-1-p$, and convolving with the flipped kernel.
- Overlap counts are uniform only when $s$ divides $k$; otherwise you get checkerboard artifacts (Odena et al., 2016).
- Alternatives: resize-convolution (upsample, then conv) and pixel shuffle. PyTorch: `nn.ConvTranspose2d`, weight shape $(C_{in},C_{out},k,k)$.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
