Foundations to Frontier
Tests
H. Generative Models › H4 Generative Adversarial Networks

Conditional GANs

A conditional GAN (Mirza and Osindero, 2014) gives both players extra information y, such as a class label, a text embedding or an image. The generator learns p(x | y) and the discriminator judges whether a pair (x, y) is a real, matching pair. Then you can ask the generator for 'a cargo ship' rather than taking whatever comes out. The theory carries over label by label: for each y the optimal discriminator is p(x|y)/(p(x|y) + q(x|y)), and the equilibrium matches every conditional distribution. This page covers the ways to inject y: concatenation (the original), auxiliary classifiers (AC-GAN), projection discriminators and conditional batch norm (BigGAN). It also covers how to evaluate whether samples respect their labels. In the exercises you build one-hot conditioning, projection logits and a label-accuracy metric. You then train a conditional and an unconditional GAN on a ring of 8 Gaussians with identical networks. With labels, 99.9–100% of samples landed in the requested mode on all three seeds. Without labels the same budget gave 41%, 57% and 17% of samples on any mode, with 7, 7 and 2 of 8 modes covered.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Asking for something specific

A plain GAN is like a slot machine: you pull the lever (draw noise) and get some sample, but you cannot choose which kind. A conditional GAN adds a request. The generator receives the noise together with a label, such as ‘digit 7’ or ‘mode 3’, and the discriminator checks not only whether a sample looks real but also whether it matches its label. The demo shows a ring of eight clusters: a conditional generator puts its samples into the cluster you ask for, while an unconditional one scatters them, sometimes missing clusters entirely.

GAN lab
A conditional generator asked for label 2 puts its samples in mode 2; in the exercise 99.9% of samples landed in the requested mode.
Why engineers care

Conditioning is what makes generators useful in engineering: generate a radar return for a given target class and range, a satellite patch for a given land-cover type, or a sensor trace for a given fault, so that rare cases can be oversampled on purpose.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A conditional GAN gives the generator…
  2. 2. The conditional discriminator judges…
  3. 3. Conditioning lets you…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Game. min_G max_D E over p(x, y) of log D(x, y) + E over z and p(y) of log(1 − D(G(z, y), y)).
  • Concatenation (Mirza and Osindero 2014). One-hot y joined to z at the generator input and to x (or to a hidden layer) at the discriminator. Simple and still common for small label sets.
  • AC-GAN (Odena, Olah and Shlens 2017). D outputs real/fake and a class prediction; both players also minimise the classification loss. Strong label adherence, but it tends to favour easily classified samples and can reduce diversity within a class.
  • Projection discriminator (Miyato and Koyama 2018). logit = ψ(φ(x)) + e_yᵀ φ(x) with a learned class embedding e_y; motivated by the form of the optimal log-density ratio. Used in SNGAN and BigGAN.
  • Conditional batch norm (de Vries et al. 2017; Dumoulin et al. 2017). Per-class scale and shift in the generator’s normalisation layers; BigGAN computes them from a shared embedding (H4.12).
  • Other conditions. Text embeddings (Reed et al. 2016), images (pix2pix, H4.8), physical parameters such as range, angle or weather.
  • Evaluation. Label accuracy of a separate classifier on generated samples; Classification Accuracy Score (Ravuri and Vinyals 2019): train a classifier on generated data and test on real; per-class FID; intra-class diversity.
📝 Quick check (Level 1)
0/3 answered
  1. 1. AC-GAN's discriminator also…
  2. 2. A projection discriminator adds…
  3. 3. Classification Accuracy Score trains a classifier on…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Building conditioning in

Exercise 1.

  • One-hot encoding and concatenation of labels to the noise.
  • Projection discriminator logits ψ + ⟨e_y, φ⟩.
  • A label-accuracy metric that counts only realistic samples in the requested mode.

Practice.

  • Small label sets: concatenate one-hot labels (or a learned embedding) at the input of G and inside D.
  • Many classes: projection discriminator plus conditional batch norm, with spectral normalisation (BigGAN, H4.12).
  • Continuous conditions (range, angle, temperature): feed the scaled value or a Fourier-feature embedding; check interpolation between training values.
  • Check label adherence and diversity separately: a classifier’s accuracy on samples, plus per-class FID or recall.
  • Balance: sample y from the real label distribution, or deliberately oversample rare classes when generating training data.
import torch, torch.nn as nn
class CondG(nn.Module):
    def __init__(self, z_dim=64, n_cls=10, emb=16):
        super().__init__(); self.emb = nn.Embedding(n_cls, emb)
        self.net = nn.Sequential(nn.Linear(z_dim + emb, 256), nn.ReLU(), nn.Linear(256, 2))
    def forward(self, z, y): return self.net(torch.cat([z, self.emb(y)], 1))
class ProjD(nn.Module):                      # Miyato and Koyama (2018)
    def __init__(self, n_cls=10, h=256):
        super().__init__(); self.phi = nn.Sequential(nn.Linear(2, h), nn.LeakyReLU(0.2))
        self.psi = nn.Linear(h, 1); self.emb = nn.Embedding(n_cls, h)
    def forward(self, x, y):
        f = self.phi(x); return self.psi(f).squeeze(1) + (self.emb(y) * f).sum(1)
🐍 Exercise 1: labels in, labels checked
Encode labels, concatenate them to the noise, compute projection logits and measure label accuracy.
📝 Quick check (Level 2)
0/3 answered
  1. 1. For a continuous condition such as range, feed…
  2. 2. BigGAN conditions D with…
  3. 3. To check diversity within a class use…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Conditional optimum. For fixed G and each label y, the discriminator’s objective splits as ∫ p(y) ∫ [p(x|y) log D(x, y) + q(x|y) log(1 − D(x, y))] dx dy. Maximising pointwise gives D*(x, y) = p(x|y)/(p(x|y) + q(x|y)). Substituting, C(G) = −log 4 + 2 E over p(y) of JSD(p(·|y) ‖ q(·|y)), minimised exactly when q(x|y) = p(x|y) for every y with p(y) > 0.

Why conditioning helps. The generator no longer has to discover the cluster structure and keep all clusters populated by itself: each label pins down a much simpler conditional distribution, and the discriminator punishes samples in the wrong cluster for that label. Mode coverage over labels is guaranteed by construction, because the user chooses y.

Projection form. If the conditional log-densities are log-linear in features, log p(x|y)/q(x|y) = e_yᵀφ(x) + ψ(φ(x)) up to terms absorbed by ψ. This is the structure the projection discriminator hard-codes, instead of letting D learn how to combine x and y from concatenation.

AC-GAN’s bias. Adding −log Q(y|G(z, y)) to the generator objective rewards samples the auxiliary classifier finds easy, which pushes them away from class boundaries and can reduce diversity; later variants (TAC-GAN, Gong et al. 2019) add a twin classifier to correct this.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The conditional GAN optimum requires…
  2. 2. Conditioning guarantees coverage of…
  3. 3. AC-GAN can reduce diversity because it rewards…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much does knowing the label help a small GAN on a multi-modal toy problem?

History.

  • M. Mirza and S. Osindero, “Conditional generative adversarial nets” (arXiv 2014).
  • A. Odena, C. Olah and J. Shlens, “Conditional image synthesis with auxiliary classifier GANs” (ICML 2017).
  • T. Miyato and M. Koyama, “cGANs with projection discriminator” (ICLR 2018).

Exercise 2. A ring of 8 Gaussians (radius 2, std 0.05).

  • Models: G 10 → 32 → 32 → 2 and D 10 → 32 → 32 → 1 (LeakyReLU 0.2). In the conditional model the one-hot mode label is concatenated to the 2-D noise and to D’s input; in the unconditional model the label slots are zero. Both are trained side by side as stacked networks.
  • Training: non-saturating loss, Adam (lr 0.002, β1 = 0.5), batch 64, 1000 steps.
  • Measures on 2000 samples (250 per label): share within 0.3 of a mode centre (“on a mode”), modes holding at least a quarter of their fair share, and for the conditional model the share in the requested mode.
model seed modes covered on a mode in the requested mode
conditional 0 / 1 / 2 8 / 8 / 8 0.999 / 1.0 / 0.999 0.999 / 1.0 / 0.999
unconditional 0 / 1 / 2 7 / 7 / 2 0.409 / 0.57 / 0.17 –
  • Labels solved the problem: every conditional run put essentially all samples in the requested mode within 1000 steps.
  • The unconditional GAN was slow and erratic: under 60% of samples on a mode, and on seed 2 it collapsed onto 2 modes.
  • Caveat: here the label determines the mode completely, the easiest possible conditioning. With labels that only partly constrain the data (for example ‘ship’ covering many ship types), mode collapse within a class can still happen.
🐍 Exercise 2 (research): how much does knowing the label help a small GAN on a multi-modal toy problem?
Train a conditional and an unconditional GAN with the same networks on a ring of 8 Gaussians and compare mode coverage and label accuracy.

Conditional GANs in AI, robotics and aerospace

  • Targeted synthetic data: generate examples of rare classes (unusual aircraft types, defects, fault signatures) on demand to rebalance training sets.
  • Physics-conditioned simulation: conditional generators emulate expensive simulators, such as calorimeter showers in particle physics (CaloGAN, Paganini et al. 2018) or radar returns given geometry.
  • Image-conditioned generation: pix2pix and its descendants (H4.8) map maps to satellite images, sketches to renders, or SAR to optical imagery.

References

  1. M. Mirza and S. Osindero (2014). “Conditional generative adversarial nets.” arXiv.
  2. A. Odena, C. Olah and J. Shlens (2017). “Conditional image synthesis with auxiliary classifier GANs.” ICML.
  3. T. Miyato and M. Koyama (2018). “cGANs with projection discriminator.” ICLR.
  4. S. Ravuri and O. Vinyals (2019). “Classification accuracy score for conditional generative models.” NeurIPS.
  5. M. Paganini, L. de Oliveira and B. Nachman (2018). “CaloGAN: simulating 3D high energy particle showers in multilayer electromagnetic calorimeters with generative adversarial networks.” Physical Review D.