liveBook · Manning

Conditional GAN

As explained in Learn Generative AI with PyTorch

A GAN that conditions generation on auxiliary information such as class labels so the output can be steered toward a desired attribute.

A conditional GAN (cGAN) extends a GAN by adding auxiliary information—such as class labels, data from another modality, or text—to generation. The generator uses random noise plus the condition to produce an output matching the requested attribute, while the discriminator or critic evaluates the output together with that condition. section 5.2 section 5.2.2

Unlike an ordinary GAN, which generates varied samples without an explicit target attribute, a cGAN directs generation through labeled training data. Wasserstein distance is a separate training technique; combining it with a cGAN can improve stability but does not define conditioning. section 5.2 section 5.6

Faces with or without eyeglasses

Train on labeled human-face images, using one label for eyeglasses and another for no eyeglasses. At generation time, concatenate a random noise vector with a one-hot label such as [1, 0] or [0, 1]. The generator then produces faces directed toward the selected attribute. Weighted label averages can create transitions in which eyeglasses gradually fade away. section 5.2.2 section 5.4.2 section 5.6

A practical cGAN implementation proceeds through these pieces:

  • Feed the generator a 100-dimensional noise vector and a two-value one-hot label, producing a 102-dimensional input. section 5.3.2
  • Condition the critic on the image and its label so it judges both realism and attribute alignment. section 5.2.2
  • Train the critic on real and generated images, then train the generator to produce convincing images with the requested label. section 5.4 section 5.4.2
  • Inspect labeled samples after each epoch, monitor critic and generator losses, and save the trained generator for later use. section 5.4.2
Figure 5.4 The training process for cGANs

Use the training-process diagram to follow how noise and labels enter the generator and how labeled real or fake data reaches the critic. section 5.2.2

A convolutional cGAN generator can use transposed-convolution blocks with batch normalization and ReLU, ending with Tanh so output pixels match training data scaled from −1 to 1. After training, discard the critic and retain the generator for targeted synthesis. section 5.3.2 section 5.4.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains conditioning, tensor construction, network roles, training batches, visual inspection, and WGAN stabilization without deriving the full theory.
Examples
Uses labeled human faces with or without eyeglasses and demonstrates one-hot labels, generated-image grids, and attribute transitions.
What it assumes
Assumes familiarity with the basic GAN framework and introduces conditional labels, critic inputs, and Wasserstein training incrementally.
What you can do after
Build it from scratch in a convolutional PyTorch model, train it with labeled batches, inspect outputs, and generate a chosen attribute.
How it connects
Connects conditional GANs to GANs, Wasserstein GANs, gradient penalty, latent space, label arithmetic, and vector arithmetic.

Also explained in

Compare how 2 books explain conditional GAN →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.