๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #297

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?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Conditional GAN โ€“ FAQs

Quick answers about learning Conditional GAN in Deep Learning.

This free note from CodingNow 2.0 explains Conditional GAN in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Conditional GAN, is 100% free with no signup required.
With focused practice, most students grasp Conditional GAN in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now