liveBook · Manning

Discriminator

As explained in Inside Deep Learning: Math, Algorithms, Models

In a GAN, the neural network that scores whether an example is real or generated.

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

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains binary and Wasserstein losses, alternating updates, gradient penalties, and convolutional design choices; defers broader architectural search.
Examples
Uses MNIST digits, a toy two-dimensional grid, BCEWithLogitsLoss, and a convolutional discriminator with average pooling.
What it assumes
Assumes neural-network and PyTorch basics while introducing GAN losses, alternating optimization, logits, and autograd gradient penalties.
What you can do after
Build and train fully connected or convolutional discriminators with PyTorch GAN and WGAN-GP loops.
How it connects
Connects the discriminator to the generator, GAN training, binary classification, Wasserstein GAN, gradient penalty, convolutional neural networks, and mode collapse.

Also explained in

Compare how 8 books explain discriminator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.