Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Types and applications of GANs

Types of GANs (standard, conditional, Wasserstein, LSGAN, CycleGAN, StyleGAN, etc.), their mathematical foundations, and common applications in image, video, audio and data augmentation.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Generative adversarial networks (GANs) let engineers create realistic synthetic data (images, audio, text features) that are useful for data augmentation, simulation, privacy-preserving data sharing, and creative applications. Understanding the types of GANs and their assumptions helps you pick the right GAN for a task (e.g., stable training with Wasserstein GAN, class-conditioned output with conditional GAN).

Key ideas

Start from first principles.

  • Generative model: a model that learns a probability distribution p_model(x) that approximates the unknown data distribution p_data(x). Here x can be an image, audio waveform, or any data vector.

  • Adversarial learning: training two models in opposition: a Generator G that maps a random vector z from a simple prior p_z(z) (e.g., standard normal) to generated samples x = G(z), and a Discriminator D (also called critic) that outputs a scalar score D(x) estimating whether x is real (from data) or generated.

  • Minimax game (standard GAN): G tries to make D classify generated samples as real, while D tries to distinguish real from fake. The original objective (Goodfellow et al. 2014) is a minimax problem: minimize over G and maximize over D a value function V(G, D).

  • Optimal discriminator: for a fixed generator G (hence fixed model distribution p_g), the discriminator that maximizes V has a closed-form: D*(x) = p_data(x) / (p_data(x) + p_g(x)). This is derived by pointwise optimisation of a binary cross-entropy loss.

  • Divergence interpretation: substituting D*(x) into the GAN value shows that training G (minimizing the objective) implicitly minimises the Jensen–Shannon divergence between p_data and p_g. This explains why at an ideal optimum p_g = p_data.

  • Practical instability: standard GANs suffer from training instability and mode collapse (generator maps many z to few x modes). Variants change loss functions or architectures to help.

  • Important GAN variants (brief definitions):

    • Conditional GAN (cGAN): both G and D receive an extra condition y (label, class, or other information); used for class-conditional generation, image-to-image translation with paired data.
    • Wasserstein GAN (WGAN): replaces Jensen–Shannon objective with an approximation to the Earth-Mover (Wasserstein-1) distance, improving training stability. Requires a 1-Lipschitz critic (enforced with weight clipping or gradient penalty).
    • Least Squares GAN (LSGAN): uses least-squares loss for D to provide smoother gradients to G.
    • CycleGAN: for unpaired image-to-image translation; uses two generators G: X->Y and F: Y->X and a cycle-consistency loss forcing F(G(x)) ≈ x.
    • DCGAN: a recommended convolutional architecture pattern (deep convolutional GAN) with specific design choices (strided convolutions, no max-pooling, batch norm, ReLU in generator, LeakyReLU in discriminator).
    • StyleGAN / Progressive GAN: architectures with special generator design (style mixing, progressive growing) for high-resolution images.
  • Assumptions: GAN theory assumes access to large samples from p_data and that models have enough capacity to represent distributions. The discriminator optimisation step is usually approximated by gradient ascent and not solved exactly.

  • Diagrams to draw in exams (describe exactly):

    1. For a standard GAN: draw two boxes labeled "Generator G(z)" and "Discriminator D(x)". On left, draw an arrow from a circle labeled z ~ p_z(z) into G, arrow from G to a box labeled x_fake. On top right, draw an arrow from a dataset box labeled x_real ~ p_data into D. From both x_real and x_fake draw arrows to D. Label D outputs a scalar D(x) ∈ (0,1). Annotate losses: D tries to maximise log D(x_real) + log(1 - D(x_fake)); G uses the non-saturating alternative maximise log D(x_fake) (the original minimises log(1 - D(x_fake)); they share the same optimum).
    2. For CycleGAN: draw domain X and domain Y, two generators G: X→Y and F: Y→X, two discriminators D_Y and D_X, arrows for cycle: x → G(x) → F(G(x)) ≈ x and y → F(y) → G(F(y)) ≈ y. Label cycle-consistency loss as L1 norm between original and reconstructed.

Formulas

V(G,D) = E_{x~p_data}[log D(x)] + E_{z~p_z}[log(1 - D(G(z)))] — V(G,D) is the GAN value function; D(x) ∈ (0,1) is a probability; the terms are dimensionless (log-probabilities); applies to the standard minimax GAN.

G^* = arg min_G max_D V(G,D) — G^* minimises the value when D is optimal.

D^*(x) = p_data(x) / (p_data(x) + p_g(x)) — optimal discriminator for fixed generator; p_data and p_g are probability density functions (unit: 1/(unit of x) when x continuous) and are non-negative and integrate to 1. Assumes both densities > 0 where evaluated.

V(G,D^*) = -log(4) + 2 · JSD(p_data || p_g) — value at optimal D expressed in terms of Jensen–Shannon divergence JSD; units: unitless; applies when using the standard GAN objective and integrals exist.

W(p_data, p_g) = inf_{γ ∈ Π(p_data, p_g)} E_{(x,y)~γ}[||x - y||] — Wasserstein-1 distance (Earth-Mover); ||·|| is a norm in sample space (e.g., L2, unit depends on x).

L_cycle(G,F) = E_{x~p_X}[||F(G(x)) - x||_1] + E_{y~p_Y}[||G(F(y)) - y||_1] — Cycle-consistency loss used in CycleGAN; ||·||_1 is L1 norm (units of x).

L_WGAN = E_{x~p_data}[D(x)] - E_{z~p_z}[D(G(z))] — critic objective for WGAN; D is not a probability but a scalar; applies when D is 1‑Lipschitz (enforced e.g., by weight clipping, gradient penalty, or spectral normalization); the critic maximizes this, and the generator minimizes −E_{z~p_z}[D(G(z))].

L_LSGAN_D = 0.5 · E_{x~p_data}[(D(x) - b)^2] + 0.5 · E_{z~p_z}[(D(G(z)) - a)^2] — least-squares discriminator loss where a and b are target labels for fake and real (e.g., a=0, b=1); unitless. The generator typically minimizes L_LSGAN_G = 0.5 · E_{z~p_z}[(D(G(z)) - c)^2] with c = b (e.g., c=1).

Worked examples

  1. Derive the optimal discriminator and the JSD expression (exam-style derivation).

Question: Show that for the standard GAN value function V(G,D), the optimal discriminator is D*(x) = p_data(x) / (p_data(x) + p_g(x)). Then show that V(G,D*) = -log(4) + 2·JSD(p_data||p_g).

Model answer (step-by-step):

  • Start from V(G,D) = E_{x~p_data}[log D(x)] + E_{x~p_g}[log(1 - D(x))] where we changed variable in second expectation to x ~ p_g.
  • For each x, the integrand depends only on D(x). Maximise pointwise: for fixed x, maximise f(D) = p_data(x)·log D + p_g(x)·log(1 - D) over D ∈ (0,1).
  • Compute derivative: f'(D) = p_data(x)/D - p_g(x)/(1 - D). Set f'(D)=0 → p_data(x)/D = p_g(x)/(1 - D).
  • Solve: D*(x) = p_data(x) / (p_data(x) + p_g(x)). This is the optimal discriminator.
  • Substitute D* into V:
    • V(G,D*) = ∫ [p_data(x) log( p_data/(p_data + p_g) ) + p_g(x) log( p_g/(p_data + p_g) )] dx.
    • Let m(x) = (p_data(x) + p_g(x))/2. Rearrange each term to relate to Kullback–Leibler divergences: V(G,D*) = -log(4) + 2·JSD(p_data || p_g). (Detailed algebra: express terms as -log 4 + KL(p_data || m) + KL(p_g || m) and recall JSD = (1/2)(KL(p_data||m)+KL(p_g||m)).)
  • Conclusion: Minimising V(G,D*) over G is equivalent to minimising JSD(p_data||p_g). At global minimum JSD = 0 ⇒ p_g = p_data almost everywhere.

Notes: This derivation assumes densities are defined and integrals converge; in practice models give samples and optimisation is approximate.

  1. Model answer: Choose a GAN variant for unpaired image translation (exam-style comparison and architecture sketch).

Question: You must do image translation between domain X (photos) and domain Y (art paintings) without paired data. Which GAN variant do you choose and why? Describe the losses and draw the architecture.

Model answer:

  • Choose CycleGAN because it works for unpaired image-to-image translation by enforcing cycle-consistency in addition to adversarial losses.
  • Architecture sketch to draw: two generators G: X→Y and F: Y→X; two discriminators D_Y that discriminates Y vs G(X) and D_X that discriminates X vs F(Y). Draw arrows: x → G → G(x) → D_Y, y → F → F(y) → D_X, plus cycle arrows G(x) → F(G(x)) ≈ x and F(y) → G(F(y)) ≈ y.
  • Losses:
    • Adversarial: for G and D_Y, use standard GAN or LSGAN loss comparing G(x) to real samples y.
    • Cycle-consistency: L_cycle = E_x[||F(G(x)) - x||_1] + E_y[||G(F(y)) - y||_1] to force invertibility.
    • Full loss: weighted sum: L = L_adv(G,D_Y) + L_adv(F,D_X) + λ·L_cycle where λ balances cycle loss (typical λ=10 in literature).
  • Explain: This removes the need for paired data because the network learns to map across domains while preserving content via cycle loss.

Common mistakes

  • Skipping the pointwise maximisation step when deriving D*(x); must show derivative = 0 and solve.
  • Confusing KL divergence with Jensen–Shannon divergence; standard GAN minimises JSD (not KL) when discriminator is optimal.
  • Forgetting to state Lipschitz constraint when writing WGAN; without it the theoretical connection to Wasserstein distance does not hold.
  • Drawing discriminator output as a real number in standard GAN instead of a probability (0–1) (unless using WGAN critic).

For your exam

Kinds of questions and what full-marks answers include:

  • Definitions: define Generator, Discriminator, p_g, p_data, and minimax objective. Include formulas and short intuition.
  • Derivations: derive D*(x) and show V(G,D*) relates to JSD. Show algebra and assumptions about densities.
  • Short notes: compare GAN variants (cGAN, WGAN, LSGAN, CycleGAN, DCGAN, StyleGAN); give one-sentence description, main advantage, and a typical use-case each.
  • Architectures/diagrams: draw and label standard GAN pipeline and CycleGAN architecture; annotate losses and data flows.
  • Applications question: list applications (data augmentation, super-resolution, image-to-image translation, anonymisation, audio synthesis) and justify which GAN variant to pick.
  • Practical question: discuss training issues (mode collapse, vanishing gradients, non-convergence) and remedies (WGAN, gradient penalty, feature matching, minibatch discrimination). Also be able to name evaluation metrics (Inception Score, Fréchet Inception Distance) and what they indicate.

A full-marks answer includes formulas, assumptions, derivations, a clear diagram where needed, and a short practical note on limitations.

Quick check

  1. What is the optimal discriminator D*(x) for a fixed generator?
  2. Which divergence does the original GAN minimise when D is optimal?
  3. Name one GAN variant used for unpaired image-to-image translation.

Answers: D*(x) = p_data(x) / (p_data(x) + p_g(x)); Jensen–Shannon divergence; CycleGAN (uses cycle-consistency).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?