Skip to content
E
Educatum

Generative Adversarial Network

By Educatum AI teamLast updated: Original walkthrough ↗
Key Intuition

A Generative Adversarial Network (GAN) trains a generator and a discriminator in an adversarial game where the discriminator distinguishes real samples from synthetic fakes and the generator learns to fool it. With binary cross-entropy loss and a sigmoid output p = sigma(z), the gradient with respect to the pre-sigmoid logit simplifies cleanly to p - y, while the generator typically minimizes a non-saturating objective with target y = 1.

Reference Diagram & Materials
Generative Adversarial Network matrix calculation diagram

Discriminator Loss Gradient Derivation

In a standard GAN, the discriminator DD solves a binary classification problem (real vs. fake) using the binary cross-entropy (BCE) loss (with natural logarithm ln⁡\ln, written here as log⁡\log):

L=−[ylog⁡(p)+(1−y)log⁡(1−p)]\mathcal{L} = -\bigl[y \log(p) + (1 - y) \log(1 - p)\bigr]

where:

  • y∈{0,1}y \in \{0, 1\} is the binary target (y=1y = 1 for real samples, y=0y = 0 for generated/fake samples),
  • zz is the discriminator’s pre-sigmoid logit output,
  • p=σ(z)=11+e−z∈(0,1)p = \sigma(z) = \frac{1}{1 + e^{-z}} \in (0, 1) is the predicted probability of being real.

To update weights via backpropagation, we compute ∂L∂z\frac{\partial \mathcal{L}}{\partial z} using the chain rule:

∂L∂z=∂L∂p⋅∂p∂z\frac{\partial \mathcal{L}}{\partial z} = \frac{\partial \mathcal{L}}{\partial p} \cdot \frac{\partial p}{\partial z}

  1. Derivative of BCE loss wrt probability pp: ∂L∂p=−(yp−1−y1−p)=p−yp(1−p)\frac{\partial \mathcal{L}}{\partial p} = -\left(\frac{y}{p} - \frac{1 - y}{1 - p}\right) = \frac{p - y}{p(1 - p)}

  2. Derivative of the sigmoid function wrt logit zz: ∂p∂z=σ(z)(1−σ(z))=p(1−p)\frac{\partial p}{\partial z} = \sigma(z)\bigl(1 - \sigma(z)\bigr) = p(1 - p)

  3. Combine both terms: ∂L∂z=p−yp(1−p)⋅p(1−p)=p−y=−(y−p)\frac{\partial \mathcal{L}}{\partial z} = \frac{p - y}{p(1 - p)} \cdot p(1 - p) = p - y = -(y - p)

Numerical Example (with prediction p=0.7p = 0.7)

  • For fake samples (y=0y = 0): ∂L∂z=0.7−0=+0.7\frac{\partial \mathcal{L}}{\partial z} = 0.7 - 0 = +0.7. Because the gradient is positive, gradient descent lowers zz and pushes pp down toward 00.
  • For real samples (y=1y = 1): ∂L∂z=0.7−1=−0.3\frac{\partial \mathcal{L}}{\partial z} = 0.7 - 1 = -0.3. Because the gradient is negative, gradient descent raises zz and pushes pp up toward 11.

Note on the Generator Gradient

In the pure minimax game, the generator GG minimizes log⁡(1−D(G(z)))\log(1 - D(G(z))), which can suffer from vanishing gradients early in training when DD easily spots fakes (p≈0p \approx 0). In practice, GANs almost always use the non-saturating generator loss −log⁡D(G(z))-\log D(G(z)), which applies the exact same BCE gradient derivation with the target flipped to y=1y = 1 (giving ∂LG∂z=p−1\frac{\partial \mathcal{L}_G}{\partial z} = p - 1), propagating stronger gradients back into GG to raise the discriminator’s score on generated samples.


Deriving the Sigmoid Derivative: ∂p∂z=p(1−p)\frac{\partial p}{\partial z} = p(1 - p)

Start with p=σ(z)=(1+e−z)−1p = \sigma(z) = (1 + e^{-z})^{-1} and apply the chain rule:

  1. Differentiate wrt zz: ∂p∂z=ddz(1+e−z)−1=−(1+e−z)−2⋅(−e−z)=e−z(1+e−z)2\frac{\partial p}{\partial z} = \frac{d}{dz}\left(1 + e^{-z}\right)^{-1} = -\left(1 + e^{-z}\right)^{-2} \cdot \left(-e^{-z}\right) = \frac{e^{-z}}{(1 + e^{-z})^2}

  2. Factor the fraction: ∂p∂z=11+e−z⋅e−z1+e−z\frac{\partial p}{\partial z} = \frac{1}{1 + e^{-z}} \cdot \frac{e^{-z}}{1 + e^{-z}}

  3. Rewrite in terms of pp: Since p=11+e−zp = \frac{1}{1 + e^{-z}} and 1−p=1−11+e−z=e−z1+e−z1 - p = 1 - \frac{1}{1 + e^{-z}} = \frac{e^{-z}}{1 + e^{-z}}, we obtain: ∂p∂z=p(1−p)\frac{\partial p}{\partial z} = p(1 - p)

GAN discriminator gradient diagram

More in GenAI by Hand

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.