VOOZH about

URL: https://www.geeksforgeeks.org/deep-learning/conditional-generative-adversarial-network/

⇱ Conditional Generative Adversarial Network - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Conditional Generative Adversarial Network

Last Updated : 18 May, 2026

Conditional Generative Adversarial Networks (CGANs) are a type of GAN that generate data based on specific conditions such as labels or descriptions. Unlike standard GANs that produce random outputs, CGANs use additional information to control the generation process and create more targeted results.

  • Generates data based on given conditions or labels
  • Produces more controlled and precise outputs than standard GANs
  • Uses conditional information in both generator and discriminator
  • Can generate category-specific images or data

Architecture and Working

Conditional GANs (CGANs) extend traditional GANs by conditioning both the generator and discriminator on additional information such as labels or descriptions. This conditioning makes the generation process more controlled and targeted.

1. Generator in CGANs

The generator creates synthetic data such as images, text, or videos using two inputs

Inputs

  • Random Noise (z): A vector of random values that adds diversity to generated outputs.
  • Conditioning Information (y): Extra data like labels or context that guides what the generator produces for example a class label such as "cat" or "dog".

Working: The generator combines and to create realistic data matching the given condition.

Example: If the condition is β€œcat”, the generator produces an image of a cat.

2. Discriminator in CGANs

The discriminator determines whether the input data is real or generated while also checking if it matches the given condition.

Inputs

  • Real Data (x): Actual samples from the dataset.
  • Conditioning Information (y): The same condition given to the generator.

Working: The discriminator learns to verify both

  • Whether the data is real or fake
  • Whether it correctly matches the condition

Example: If an image is labeled β€œcat”, the discriminator checks whether it genuinely looks like a cat.

3. Interaction Between Generator and Discriminator

The generator and discriminator train together in a competitive process.

  • Generator Goal: Generate fake data that appears real to the discriminator
  • Discriminator Goal: Correctly distinguish between real and fake data using the condition

4. Loss Function and Training

The training process is guided by the adversarial loss function

  • The first term encourages the discriminator to classify real samples correctly.
  • The second term pushes the generator to produce samples that the discriminator classifies as real.

Here represents the expected value is the real data distribution and is the prior noise distribution.

πŸ‘ conditional_gan
Conditional GAN

Implementation

We will build and train a Conditional Generative Adversarial Network (CGAN) to generate class-specific images from the CIFAR-10 dataset. Below are the key steps involved:

Step 1: Importing Necessary Libraries

We will import TensorFlow, NumPy, Keras and Matplotlib libraries for building models, loading data and visualization.

Step 2: Loading Dataset and Declaring Variables

  • Load the CIFAR-10 dataset using TensorFlow datasets or tf.data.Dataset.
  • Define global variables such as number of epochs, batch size and image dimensions.
πŸ‘ cgans1
Downloading data

Step 3: Visualizing Sample Images

Now we will visualize the images from the dataset to understand class distributions and data shape.

Output:

πŸ‘ cgans2
Sample Image

Step 4: Defining Loss Functions and Optimizers

In the next step we need to define the Loss function and optimizer for the discriminator and generator networks in a Conditional Generative Adversarial Network(CGANS).

  • Use Binary Cross-Entropy Loss for both generator and discriminator.
  • Define discriminator loss as sum of real and fake losses.
  • The binary entropy calculates two losses: real_loss: Loss when the discriminator tries to classify real data as real and fake_loss : Loss when the discriminator tries to classify fake data as fake
  • d_optimizer and g_optimizer are used to update the trainable parameters of the discriminator and generator during training.
  • Use Adam optimizer for both networks.

Step 5: Building the Generator Model

  • Input is noise vector (latent space) and label.
  • Convert label to a vector using an embedding layer (size 50).
  • Process noise through dense layers with LeakyReLU activation.
  • Reshape and concatenate label embedding with noise features.
  • Use Conv2DTranspose layers to up-sample into 32Γ—32Γ—3 images.
  • Output layer uses tanh activation to scale pixels between -1 and 1.

Output:

πŸ‘ cgans3
Building the Generator Model

Step 6: Building the Discriminator Model

  • Input is our image and label.
  • Embed label into a 50-dimensional vector.
  • Reshape and concatenate label embedding with the input image.
  • Apply two Conv2D layers with LeakyReLU activations to extract features.
  • Flatten features, apply dropout to prevent overfitting.
  • Final dense layer with sigmoid activation outputs probability of real or fake.

Output:

πŸ‘ cgans4
Building the Discriminator Model

Step 7: Creating Training Step Function

  • Use TensorFlow’s Gradient Tape to calculate and apply gradients for both networks.
  • Alternate training discriminator on real and fake data.
  • Train generator to fool discriminator.
  • Use @tf.function for efficient graph execution.

Step 8: Visualizing Generated Images

  • After each epoch we will generate images conditioned on different labels.
  • Display or save generated images to monitor training progress.

Step 9: Train the Model

  • At the final step we will start training the model for specified epochs.
  • Print losses regularly to monitor performance.
  • Longer training typically results in higher quality images.

Output:

πŸ‘ cgans5
Output Images

We can see some details in these pictures. But for better result we can try to run this for more epochs.

Download full code from here

Comment