Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #487

Conditional GAN

A Conditional GAN (cGAN) makes one targeted addition to the standard GAN setup: feed extra conditioning information — commonly a class label — into both the generator and discriminator, giving direct, controllable influence over what gets generated.

The Modified Formula

\[ \min_G \max_D \ \mathbb{E}_{x}[\log D(x \mid y)] + \mathbb{E}_{z}[\log(1-D(G(z\mid y) \mid y))] \]

Every term now conditions on \(y\) (e.g. a class label) — the generator receives both random noise \(\mathbf{z}\) and the desired class \(y\), and must produce a sample matching that specific class; the discriminator receives both a sample and its claimed class \(y\), and must judge whether that sample genuinely looks real for that specific class.

Why This Enables Controlled Generation

A plain, unconditional GAN produces samples from the overall data distribution with no control over which specific category comes out — you get a random cat or a random dog, with no way to request one specifically. A conditional GAN lets you specify "generate a dog" and reliably get a dog-like output, since both networks were trained with that class information available throughout.

Code

import torch
import torch.nn as nn

class ConditionalGenerator(nn.Module):
    def __init__(self, noise_dim, num_classes, embed_dim=10):
        super().__init__()
        self.label_embedding = nn.Embedding(num_classes, embed_dim)
        self.net = nn.Sequential(
            nn.Linear(noise_dim + embed_dim, 256), nn.ReLU(),
            nn.Linear(256, 784), nn.Tanh()
        )

    def forward(self, z, labels):
        label_emb = self.label_embedding(labels)
        combined = torch.cat([z, label_emb], dim=1)   # noise AND class info, concatenated together
        return self.net(combined)

generator = ConditionalGenerator(noise_dim=100, num_classes=10)
z = torch.randn(4, 100)
labels = torch.tensor([3, 3, 7, 7])   # explicitly REQUEST classes 3 and 7
generated = generator(z, labels)
print(generated.shape)   # (4, 784) -- generated images conditioned on the requested class

Common Mistakes

  • Feeding conditioning information to only the generator and not the discriminator (or vice versa) — the discriminator also needs the class label to correctly judge whether a sample genuinely looks like a real, plausible example of that specific class, not just whether it looks generically real.
  • Assuming conditioning is limited to class labels — the same idea generalizes to any auxiliary information (text descriptions, sketches, segmentation maps), a pattern that reappears directly in Diffusion Conditioning for text-to-image diffusion models.

Interview Relevance

Q: "Why must the discriminator, not just the generator, also receive the conditioning label in a conditional GAN?" If only the generator saw the label, the discriminator would have no way to check whether a generated sample actually matches its claimed class — it could only judge generic realism, not class-consistency. Giving the discriminator the label too lets it penalize the generator specifically for producing realistic-looking but wrong-class outputs, which is essential for the conditioning to actually work.

Practice Question

How would you adapt a conditional GAN to generate images conditioned on a text description instead of a class label?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →