Dive into Deep Learning · §16.6
Conditional generation
the game on pairs · how the condition enters · class-conditional CIFAR-10 · measuring condition alignment · translation
Data supplies sample–condition pairs (x, c); the generator draws c from the same label marginal and attaches a sample: x' = G(z, c). The critic scores the pairs:
V(D) = E_{(x,c) \sim p}[\log \sigma(D(x, c))] + E_{c,\, x' \sim q(\cdot \mid c)}[\log \sigma(-D(x', c))]
This is the log-loss game from :numref:sec_basic_gan on pairs rather than samples. The derivation below assumes that the real and generated pairs share the marginal p(c).
The pointwise argument applies to any classifier input space:
D^\star(x, c) = \log \frac{p(c)\, p(x \mid c)}{p(c)\, q(x \mid c)} = \log \frac{p(x \mid c)}{q(x \mid c)}
\max_D V = E_c\big[2\, \mathrm{JS}(p(\cdot \mid c), q(\cdot \mid c))\big] - 2 \log 2
Text conditions: cross-attention (GigaGAN). Our experiment: concatenation into G, projection head in D.
Bayes’ rule separates label consistency from unconditional realness:
\log \frac{p(x \mid c)}{q(x \mid c)} = \underbrace{\log \frac{p(c \mid x)}{q(c \mid x)}}_{\textrm{label consistency}} + \underbrace{\log \frac{p(x)}{q(x)}}_{\textrm{unconditional realness}}
Model both label posteriors as softmax classifiers over shared features \varphi; the normalizers are c-free, so
D(x, c) = e_c^\top \varphi(x) + \psi(x)
32×32, one fewer stage than :numref:sec_dcgan; class embedding concatenated at the 4×4 stage of G; projection head in D. Loss: d2l.rpgan_loss_D/G + both zero-centered penalties. Pairs share their condition: real batch’s labels condition the fakes.
A pilot sweep over \gamma \in \{0.05, 0.5, 5\} remained stable at all three values but showed progressively stronger damping. We use the intermediate value \gamma = 0.5. The appropriate value depends on the dataset and implementation: the sprite experiment uses 10, while R3GAN starts CIFAR-10 at 0.05 and decays it.
Each row fixes a requested class, and each column uses a new latent draw. Vehicle classes are usually recognizable from shape; animal classes rely more on palette and background. The variation across columns provides a visual check of within-class diversity.
class alignment classifier
airplane 0.73 0.81
automobile 0.73 0.84
bird 0.39 0.49
cat 0.28 0.38
deer 0.80 0.86
dog 0.47 0.58
frog 0.80 0.85
horse 0.69 0.81
ship 0.54 0.64
truck 0.78 0.84
overall 0.62 0.71
Alignment is the fraction of conditioned samples that the classifier assigns to the requested class. The classifier’s accuracy on real images provides a reference, not an upper bound. Because the classifier is evaluated under distribution shift, the comparison remains a proxy. The weakest classes differ between runs and score below their visual impression in the grid.
permuted labels: alignment 0.60 -> 0.06, FD 1.56, MMD^2 0.052 (both unchanged)
critic label check: mean D(x, y) - D(x, y+1) on real = 0.13
collapse baseline: alignment 0.80, per-class feature variance 0 (generator 0.188, real 0.218)
Three axes: fidelity, diversity, condition alignment.
The condition can be an image:
sec_gan_beyond develops the argument).Implementations: :numref:chap_cv.