# Deep convolutional GAN (DCGAN)

> Radford, Metz and Chintala (2016) made convolutional GANs train reliably: strided and transposed convolutions instead of pooling, batch norm except at G's output and D's input, ReLU and a tanh output in G, LeakyReLU(0.2) in D, and Adam with lr 2e-4 and β₁ = 0.5.

Source: https://www.mlfoundry.in/learn/deep-convolutional-gan-dcgan  
Section: Deep Learning › Generative Deep Learning Algorithms  
Level: core · 13 min read · updated 2026-10-12

**Deep convolutional GAN (DCGAN):** DCGAN is the convolutional architecture and training recipe from Radford, Metz and Chintala, "Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks" (ICLR 2016). The generator turns a noise vector into an image through a stack of **fractionally-strided (transposed) convolutions**. Each one doubles the resolution and halves the channel count. The discriminator mirrors it with **strided convolutions**. Both use batch normalization and avoid pooling and hidden fully connected layers. The loss is the standard GAN objective of **Generative adversarial network (GAN)**.

The original GANs of 2014 were multilayer perceptrons. Attempts to scale them up with the CNNs used for classification mostly failed: training diverged, or the generator produced noise. LAPGAN (Denton et al., 2015) worked around this by generating a Laplacian pyramid one scale at a time. Alec Radford, Luke Metz and Soumith Chintala instead searched for a single architecture that trained stably across datasets and at 64×64 resolution. Their paper is mostly an empirical recipe. It also showed that the trained networks learn useful representations. The discriminator's features are good for classification, and the generator's latent space supports smooth interpolation and vector arithmetic.

## The architecture guidelines

**Section 3 of the paper, verbatim in substance**

1. Replace pooling with **strided convolutions** in $D$ and **fractionally-strided convolutions** in $G$, so each network learns its own down- and up-sampling (the all-convolutional net of Springenberg et al., 2015).
2. Use **batch normalization** in both networks, **except** the generator's output layer and the discriminator's input layer. Applying it to every layer caused sample oscillation and instability.
3. Remove fully connected hidden layers. $G$'s first layer is a matrix multiplication of $z$ whose result is reshaped into a $4\times4$ feature map. $D$'s last convolution is flattened into a single sigmoid output.
4. Use **ReLU** in every generator layer except the output, which uses **tanh**. Images are scaled to $[-1,1]$ to match.
5. Use **LeakyReLU** with slope 0.2 in every discriminator layer.

A transposed convolution is the gradient of a convolution with respect to its input. Each input pixel stamps a scaled copy of the kernel into the output, the copies step $s$ pixels apart, and overlapping copies add up. With stride $s$, padding $p$ and output padding $o_p$, the two layer types change size as follows:

$$
H_{\text{out}}^{\text{convT}}=(H_{\text{in}}-1)\,s-2p+k+o_p\qquad\qquad H_{\text{out}}^{\text{conv}}=\Big\lfloor\frac{H_{\text{in}}+2p-k}{s}\Big\rfloor+1
$$

- $k=4,\ s=2,\ p=1$: the standard DCGAN resizing layer: $(H-1)\cdot2-2+4=2H$ for the transposed conv, $\lfloor(H+2-4)/2\rfloor+1=H/2$ for the conv
- $k=4,\ s=1,\ p=0$: G's first layer ($1\times1\to4\times4$, the "project and reshape" of $z$) and D's last ($4\times4\to1\times1$)
- $o_p$: extra rows on one side of a transposed conv's output, to pick among the input sizes that a strided conv would map to the same output

**Training details from the paper**

- **Optimiser:** Adam, learning rate $2\cdot10^{-4}$ (the suggested $10^{-3}$ was too high), $\beta_1=0.5$ (0.9 caused oscillation and instability)
- **Batches and init:** mini-batch SGD with batch size 128; all weights from $\mathcal N(0,\,0.02^2)$; no pre-processing beyond scaling to $[-1,1]$
- **Noise:** $z\in\mathbb R^{100}$ from a uniform distribution
- **Paper's generator:** $z\to4\times4\times1024\to8\times8\times512\to16\times16\times256\to32\times32\times128\to64\times64\times3$
- **Datasets:** LSUN bedrooms (about 3M images), ImageNet-1k (32×32 crops), and a scraped set of 3M face images of 10K people
- **Features for classification:** $D$ trained on ImageNet-1k, all layers max-pooled to $4\times4$ and fed to an L2-SVM: 82.8% on CIFAR-10. On SVHN with 1000 labels: 22.48% test error

> Figure: The 64×64 DCGAN of the PyTorch reference implementation (ngf = ndf = 64, nz = 100, so 512 channels at 4×4 instead of the paper's 1024), layer by layer. Counts include BatchNorm's γ and β; the convolutions have no bias. With k = 4 the generator has 3,576,704 parameters and needs 104.63M multiply-accumulates per image. Each of the three middle upsampling layers costs 33.55M, and the first, 'up 1', holds 2,097,664 of the parameters. The discriminator has 2,765,568 parameters and 103.82M MACs. The grid shows how many kernel taps land on each output pixel of the selected transposed conv. With k = 4 and s = 2, every interior pixel receives exactly 4 (2 per axis), so the layer has no built-in pattern. Set k = 3 (p = 1, output padding 1) and the counts alternate 1, 2, 4. That is the checkerboard of Odena et al. (2016): even with constant weights and a constant input, the output already has a period-2 pattern with max/min = 4. k = 5 gives 4, 6, 9 (ratio 2.25). k = 6 is uniform again at 9. For the discriminator, the grid counts how many outputs read each input pixel, which is the same uneven pattern in the backward pass. k = 3 shrinks G to 2,371,136 parameters; k = 5 grows it to 5,126,720.

## Worked example: sizes, parameters and overlap

Generator of the figure at k = 4 (counts from the figure's code, totals checked with PyTorch)

| layer | in → out | k, s, p | weights | BN γ, β | params | MACs |
| --- | --- | --- | --- | --- | --- | --- |
| project z | 100×1×1 → 512×4×4 | 4, 1, 0 | 819,200 | 1,024 | 820,224 | 0.82M |
| up 1 | 512×4×4 → 256×8×8 | 4, 2, 1 | 2,097,152 | 512 | 2,097,664 | 33.55M |
| up 2 | 256×8×8 → 128×16×16 | 4, 2, 1 | 524,288 | 256 | 524,544 | 33.55M |
| up 3 | 128×16×16 → 64×32×32 | 4, 2, 1 | 131,072 | 128 | 131,200 | 33.55M |
| to RGB + tanh | 64×32×32 → 3×64×64 | 4, 2, 1 | 3,072 | none | 3,072 | 3.15M |

**Reading the table**

1. A transposed conv has $c_{\text{in}}\,c_{\text{out}}\,k^2$ weights: up 1 has $512\cdot256\cdot16=2{,}097{,}152$. Every input pixel multiplies the whole kernel, so it costs $c_{\text{in}}c_{\text{out}}k^2H_{\text{in}}^2$ MACs: $2{,}097{,}152\cdot4^2=33.55$M. Halving channels while doubling resolution keeps the middle layers at the same cost.
2. Total: $3{,}576{,}704$ parameters, which is what `sum(p.numel() for p in G.parameters())` prints for the PyTorch model below. More than half sit in up 1, where the channels are widest.
3. Overlap in 1-D. Upsampling a length-4 input of ones with a kernel of ones at $k=3$, $s=2$, $p=1$, $o_p=1$ gives `[1, 2, 1, 2, 1, 2, 1, 1]`. With $k=4$, $p=1$ it gives `[1, 2, 2, 2, 2, 2, 2, 1]`: uniform away from the border.
4. In 2-D the counts multiply, so the $k=3$ interior repeats the block $\begin{smallmatrix}1&2\\2&4\end{smallmatrix}$. The $k=4$ interior is all 4. The network can learn weights that cancel the $k=3$ pattern, but it must spend capacity doing so, and in practice the pattern often survives as checkerboard artefacts.
5. The backward pass of a strided conv with $k=3$, $s=2$ has the same `[1, 2, 1, 2, …]` pattern in its input gradient. So an uneven discriminator also pushes an uneven gradient into $G$.

## What the latent space learned

The paper's most cited figures probe $G$'s input space. **Walking** along a line between two $z$ gives smooth changes: a TV slowly turns into a window, and a room without a window gains a large one. Sudden jumps would suggest memorised images, and the walks showed none. **Vector arithmetic** on single $z$ was unstable, but averaging the $z$ of three samples per concept worked. $\bar z_{\text{smiling woman}}-\bar z_{\text{neutral woman}}+\bar z_{\text{neutral man}}$ produced smiling men. "Man with glasses" minus "man without glasses" plus "woman without glasses" produced women with glasses. The authors also used guided backpropagation to show that $D$'s filters respond to beds and windows. When they removed the generator's "window" feature maps, $G$ drew other objects where the windows had been.

$$
z_{\text{new}}=\tfrac13\sum_{i=1}^3 z_i^{\,A}-\tfrac13\sum_{i=1}^3 z_i^{\,B}+\tfrac13\sum_{i=1}^3 z_i^{\,C},\qquad x_{\text{new}}=G(z_{\text{new}})
$$

- $z_i^{A}$: latent vectors of three generated samples that show concept A (e.g. smiling woman), chosen by eye
- $\tfrac13\sum$: averaging the three made the arithmetic stable; single vectors often failed
- $x_{\text{new}}$: the paper also added small uniform noise to $z_{\text{new}}$ to show that the result is a region of latent space, not one point

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

nz, ngf, ndf, nc = 100, 64, 64, 3

class Generator(nn.Module):
    def __init__(self):
        super().__init__()
        def up(i, o):                                   # x2 upsampling: k=4, s=2, p=1
            return [nn.ConvTranspose2d(i, o, 4, 2, 1, bias=False), nn.BatchNorm2d(o), nn.ReLU(True)]
        self.net = nn.Sequential(
            nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False),   # (100,1,1) -> (512,4,4): "project and reshape"
            nn.BatchNorm2d(ngf * 8), nn.ReLU(True),
            *up(ngf * 8, ngf * 4),                                  # -> (256,8,8)
            *up(ngf * 4, ngf * 2),                                  # -> (128,16,16)
            *up(ngf * 2, ngf),                                      # -> (64,32,32)
            nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),       # -> (3,64,64), no BN on the output
            nn.Tanh(),                                              # images scaled to [-1, 1]
        )
    def forward(self, z):
        return self.net(z)

class Discriminator(nn.Module):
    def __init__(self):
        super().__init__()
        def down(i, o, bn=True):                        # x1/2 downsampling, no pooling
            return [nn.Conv2d(i, o, 4, 2, 1, bias=False), *([nn.BatchNorm2d(o)] if bn else []), nn.LeakyReLU(0.2, True)]
        self.net = nn.Sequential(
            *down(nc, ndf, bn=False),                   # (3,64,64) -> (64,32,32), no BN on the input layer
            *down(ndf, ndf * 2),                        # -> (128,16,16)
            *down(ndf * 2, ndf * 4),                    # -> (256,8,8)
            *down(ndf * 4, ndf * 8),                    # -> (512,4,4)
            nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False), # -> (1,1,1): one logit
        )
    def forward(self, x):
        return self.net(x).view(-1)

def init_weights(m):                                    # DCGAN: N(0, 0.02); BN gamma ~ N(1, 0.02), beta = 0
    if isinstance(m, (nn.Conv2d, nn.ConvTranspose2d)):
        nn.init.normal_(m.weight, 0.0, 0.02)
    elif isinstance(m, nn.BatchNorm2d):
        nn.init.normal_(m.weight, 1.0, 0.02); nn.init.zeros_(m.bias)

G, D = Generator(), Discriminator()
G.apply(init_weights); D.apply(init_weights)
opt_g = torch.optim.Adam(G.parameters(), lr=2e-4, betas=(0.5, 0.999))
opt_d = torch.optim.Adam(D.parameters(), lr=2e-4, betas=(0.5, 0.999))
bce = nn.BCEWithLogitsLoss()

z = torch.randn(128, nz, 1, 1)
x_fake = G(z)
print(x_fake.shape, D(x_fake).shape)                   # torch.Size([128, 3, 64, 64]) torch.Size([128])
print(sum(p.numel() for p in G.parameters()), sum(p.numel() for p in D.parameters()))  # 3576704 2765568

def train_step(x_real):                                 # x_real (B,3,64,64) in [-1, 1]
    B = x_real.size(0)
    fake = G(torch.randn(B, nz, 1, 1))
    loss_d = bce(D(x_real), torch.ones(B)) + bce(D(fake.detach()), torch.zeros(B))
    opt_d.zero_grad(); loss_d.backward(); opt_d.step()
    loss_g = bce(D(fake), torch.ones(B))                # non-saturating generator loss
    opt_g.zero_grad(); loss_g.backward(); opt_g.step()
    return loss_d.item(), loss_g.item()

print(len(train_step(torch.rand(128, 3, 64, 64) * 2 - 1)))   # 2

# latent arithmetic: average 3 z's per concept, then combine
z_smile_w, z_neutral_w, z_neutral_m = (torch.randn(3, nz, 1, 1).mean(0, keepdim=True) for _ in range(3))
with torch.no_grad():
    G.eval()
    print(G(z_smile_w - z_neutral_w + z_neutral_m).shape)  # torch.Size([1, 3, 64, 64])
```

DCGAN generator and discriminator for 64×64 RGB images (PyTorch reference widths), with a training step

> **Batch norm leaks information between samples:** In training mode, BatchNorm normalises each sample with statistics of its whole mini-batch. Feeding $D$ one all-real batch and one all-fake batch, as the code does, lets $D$ use batch statistics, and $G$'s samples within a batch become correlated. This is one reason later GANs replaced BN in $D$ with spectral normalization or none, and in $G$ with pixel norm or conditional BN. If you keep DCGAN's BN, never mix real and fake samples in one BN batch by accident, and generate in `eval()` mode only if the running statistics are trustworthy.

> **Checkerboards come from the kernel–stride ratio:** If $k$ is not divisible by $s$, a transposed conv covers alternate output pixels unevenly ($k=3$, $s=2$: 1, 2, 4 taps in the figure). Odena, Dumoulin and Olah (2016) showed that this produces checkerboard artefacts. Their remedy is to resize with nearest-neighbour or bilinear upsampling and then apply a stride-1 conv. DCGAN's $k=4$, $s=2$ avoids the uneven overlap, though artefacts can still appear when weights differ across the stamped copies.

> **Why β₁ = 0.5 stuck:** Adam's first moment averages gradients over about $1/(1-\beta_1)$ steps: 10 steps at 0.9, 2 at 0.5. In a two-player game the gradient direction rotates as the opponent moves. Heavy momentum keeps pushing in the stale direction and amplifies oscillation, which the authors observed at 0.9. $\beta_1=0.5$, or even 0 in later GANs such as SAGAN, BigGAN and StyleGAN, became standard.

**Strengths**

- Simple, symmetric recipe that trained stably across bedrooms, faces and ImageNet at 64×64
- Learned up- and down-sampling; no pooling or hidden dense layers
- Meaningful latent space: smooth walks and vector arithmetic on averaged codes
- $D$'s features transfer: 82.8% on CIFAR-10 with a linear SVM

**Limitations**

- Still mode-collapses and still uses the JS-type GAN loss; long runs can oscillate
- Batch norm couples samples in a batch and the network to the batch size
- Transposed convs with $k$ not divisible by $s$ create checkerboards
- Does not scale well beyond 64×64 without progressive growing, better losses or normalisation

**Question:** Which layers in DCGAN do NOT use batch normalization?

- A. All discriminator layers
- B. The generator's output layer and the discriminator's input layer
- C. The generator's first layer and the discriminator's last layer
- D. Only the generator's output layer

**Answer:** B. The generator's output layer and the discriminator's input layer — The paper found that applying BN to all layers caused sample oscillation and instability. Leaving it out of $G$'s tanh output (so the output can take the data's mean and scale) and out of $D$'s first layer (so $D$ sees raw pixel statistics) fixed it.

**Question:** A transposed conv with stride 2 and kernel size 3 upsamples 8×8 to 16×16. Why does its output tend to show a checkerboard?

- A. Because transposed convs cannot learn biases
- B. Because 3 is not divisible by 2, so interior output pixels receive 1, 2 or 4 kernel taps in a period-2 pattern
- C. Because of the tanh at the output
- D. Because the padding is too large

**Answer:** B. Because 3 is not divisible by 2, so interior output pixels receive 1, 2 or 4 kernel taps in a period-2 pattern — Each input pixel stamps a $3\times3$ kernel copy every 2 pixels. Per axis, alternate pixels are covered once and twice, so in 2-D the counts are 1, 2, 4. With $k=4$ every interior pixel gets 2 per axis (4 in 2-D) and the structural pattern disappears.

**Key points**

- DCGAN rules: strided convs in $D$, transposed convs in $G$, no pooling and no hidden dense layers; BN everywhere except $G$'s output and $D$'s input.
- Activations and optimiser: ReLU + tanh output in $G$, LeakyReLU(0.2) in $D$; Adam with lr $2\cdot10^{-4}$, $\beta_1=0.5$, batch 128, init $\mathcal N(0,0.02^2)$.
- Resizing layer $k=4$, $s=2$, $p=1$ doubles or halves resolution exactly; transposed conv output $(H-1)s-2p+k+o_p$.
- $k$ not divisible by $s$ gives uneven kernel overlap: checkerboard artefacts.
- Latent space is smooth; averaged-z vector arithmetic (smiling woman − neutral woman + neutral man) works; D's features transfer to classification.

---

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