A discriminator D is the GAN network that scores whether an input came from real training data or the generator. In the original setup, it performs binary classification: real examples receive label 1 and generated examples label 0. Training alternates between improving D’s predictions and improving G so that D classifies generated data as real. section 9.1.2 section 9.1.3
The discriminator’s practical training loop follows these steps:
- Compute D(real_data) against real labels, then compute D(fake.detach()) against fake labels. Add both BCEWithLogitsLoss values and update D with AdamW. section 9.1.3
- Reuse the generated batch without detach() for the generator step, calculate loss_func(D(fake), y_real), and update G so D is encouraged to call generated data real. section 9.1.3
- Interpret D as estimating the probability that an input is real: the adversarial objective increases its score on real data while decreasing its score on generated data. section 9.1.2
- In WGAN-GP, replace bounded binary scores with relative real and fake scores, using D(G(z)) - D(x) plus a gradient penalty. section 9.3.1 section 9.3.3
MNIST discriminator
A fully connected discriminator can accept the generator’s reshaped 28-by-28 output and return one value per example. The training code creates real_label = 1 and fake_label = 0, evaluates real and generated batches separately, and records D and G losses. During inspection, applying torch.sigmoid(D(fake_digits)) converts discriminator logits into displayed realness scores. A convolutional version instead uses average pooling, adaptive average pooling, flattening, and a final linear layer returning one score. section 9.1.3 section 9.4.2
D = nn.Sequential(
cnnLayer(1, n_filters, k_size, 28, leak),
cnnLayer(n_filters, n_filters, k_size, 28, leak),
nn.AvgPool2d(2), #1
cnnLayer(n_filters, n_filters, k_size, 28//2, leak),
cnnLayer(n_filters, n_filters, k_size, 28//2, leak),
nn.AvgPool2d(2),
cnnLayer(n_filters, n_filters, 3, 28//4, leak),
cnnLayer(n_filters, n_filters, 3, 28//4, leak),
nn.AdaptiveAvgPool2d(4), #2
nn.Flatten(),
nn.Linear(n_filters*4**2,256),
nn.LeakyReLU(leak),
nn.Linear(256,1),
)
The listing shows a convolutional discriminator that uses average pooling to share gradient contribution across pixels, adaptive pooling to reduce spatial dependence, and a final scalar output. section 9.4.2







