A discriminator is the GAN network that receives real samples and generator-produced samples, then learns to classify each as real or fake. Its predictions supply the feedback the generator uses to improve its output, making the two networks adversaries that must learn at comparable rates. section 9.1.1 section 9.6
In a convolutional GAN, the discriminator is a separate image model. It progressively reduces the input with strided convolutional layers, combines LeakyReLU, dropout, batch normalization, and padding, and returns one classification value. Compile it with binary cross-entropy and its own optimizer before connecting it to the generator. section 9.1.2
Listing 9.4 EDL_9_1_GAN.ipynb: Building the discriminator
def build_discriminator(): model = Sequential() model.add(Conv2D(32, kernel_size=3, strides=2, input_shape=img_shape, padding="same")) #1 model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Conv2D(64, kernel_size=3, strides=2, padding="same")) #2 model.add(ZeroPadding2D(padding=((0,1),(0,1)))) model.add(BatchNormalization(momentum=0.8)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Conv2D(128, kernel_size=3, strides=2, padding="same")) #2 model.add(BatchNormalization(momentum=0.8)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Conv2D(256, kernel_size=3, strides=1, padding="same")) #2 model.add(BatchNormalization(momentum=0.8)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(1, activation='sigmoid')) #3 model.summary() img = Input(shape=img_shape) #1 validity = model(img) #4 return Model(img, validity)) #5
Inspect the discriminator’s staged convolutional architecture, including stride-based image reduction and regularization layers. section 9.1.2
Train the discriminator as part of an alternating adversarial loop:
- Label a batch of real images as valid and a batch generated from random latent noise as fake. section 9.1.2
- Update the discriminator on both batches, then freeze its weights while training the combined GAN to label generated images as valid. section 9.1.2
- Watch the balance: an overly strong discriminator can reduce feedback to the generator, while an overwhelmed one may make near-random real/fake guesses. section 9.2.1
Repurposing a trained discriminator
A discriminator trained on faces can be reused as a simple classifier that decides whether an input image is a face or not. This reuse is useful even though a discriminator trained separately is rarely the broader goal of GAN training. section 9.2
Training failures are often visible in discriminator behavior. Changing its optimizer can make it exceptionally good at detecting fakes; discriminator fake loss then stays within a narrow range while generator loss changes little, indicating vanishing gradients rather than improving generated output. section 9.2.2







