Skip to content
← Generative AI
Q33

Class-conditioned digit generation with cGAN

cGANClass LabelsMNISTConditioning
Explain how class labels can be incorporated into both the Generator and Discriminator of a cGAN. Illustrate the process using class-controlled handwritten-digit generation.

What's actually being asked

Walk through, mechanically, how a class label y gets fed into both G and D, using handwritten-digit generation (y ∈ {0,...,9}) as the running example.

Answer

Representing the label

The class label y (e.g. digit "7") is first turned into a vector the network can use:

  • One-hot encoding: a 10-dim vector with a 1 at index 7 and zeros elsewhere — simplest, works well for a small label set like MNIST.
  • Learned embedding: y is mapped through an embedding layer to a dense vector — more common in deeper/modern cGANs, scales better to many classes.

Feeding the label into the Generator

  1. Sample noise z (e.g. 100-dim) and the desired label y (e.g. "7").
  2. Embed or one-hot encode y into a label vector.
  3. Concatenate z with the label vector into one combined input (or, in convolutional architectures, project the label vector into a feature map and concatenate it along the channel dimension at an early layer).
  4. Feed the combined vector through G's usual upsampling stack (Linear or transposed-conv layers) → output G(z|y), a digit image that should specifically look like a "7".

Feeding the label into the Discriminator

  1. Take a sample x — either a real digit image with its true label y, or a fake G(z|y) paired with the same y it was generated for.
  2. Embed or one-hot encode y the same way as for G.
  3. Concatenate the label vector with x — either at the input (flattened image + label vector, for a vanilla cGAN) or at an intermediate conv feature map (the image's feature map concatenated with a spatially-tiled label embedding, for a conv cGAN).
  4. Feed through D's usual downsampling stack → D(x|y), a single score answering "is this a real '7', specifically?"

Diagram

See the reference diagram: z and y both flow into G via concatenation to produce the image; the same y and the image (real or fake) both flow into D via concatenation to produce the score.

Why this gives class control

Because D is explicitly trained to reject a correctly-drawn-but-mislabeled digit (say, a perfectly realistic "7" shown to D paired with label "3") just as hard as it rejects a blurry fake, G is forced to make its output's identity track y precisely — not just "look like some real digit." At inference time, setting y to whichever digit is wanted and sampling z makes G(z|y) reliably draw that digit.

Matlab, label concatenation ek "instruction channel" ki tarah kaam karti hai jo pure training loop mein har jagah carry hoti hai — G ke input se D ke input tak — taaki dono networks hamesha ek hi condition ke against consistent rahein.

Worked example

Say y = "3" → one-hot [0,0,0,1,0,0,0,0,0,0]. G receives [z ; one-hot(3)] → outputs an image expected to look like digit 3. D, shown this image concatenated with the same one-hot(3), should score it high if it genuinely looks like a 3. But a real "3" image concatenated with the wrong label, say one-hot(8), should be scored low by D even though the image itself is perfectly real — the mismatch alone is enough to fail.

Exam tip

Spell out the two standard injection points — "concatenation at the input" vs "concatenation at an intermediate feature map" — explicitly. Simply saying "the label is added" without specifying how or where it enters the network is a common way marks are lost here.

ARCHITECTUREhow the pieces talk
7 components. Noise z to Generator G(z|y); Class label y to Label embedding; Label embedding to Generator G(z|y) (concat); Generator G(z|y) to Generated digit; Generated digit to Discriminator D(x|y); Label embedding to Discriminator D(x|y) (concat); Discriminator D(x|y) to Real/Fake score.Noise zClass label yLabel embeddingGenerator G(z|y)Generated digitDiscriminator D(x|y)Real/Fake scoreconcatconcat