Generative Adversarial Network
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.

Discriminator Loss Gradient Derivation
In a standard GAN, the discriminator solves a binary classification problem (real vs. fake) using the binary cross-entropy (BCE) loss (with natural logarithm , written here as ):
where:
- is the binary target ( for real samples, for generated/fake samples),
- is the discriminator’s pre-sigmoid logit output,
- is the predicted probability of being real.
To update weights via backpropagation, we compute using the chain rule:
-
Derivative of BCE loss wrt probability :
-
Derivative of the sigmoid function wrt logit :
-
Combine both terms:
Numerical Example (with prediction )
- For fake samples (): . Because the gradient is positive, gradient descent lowers and pushes down toward .
- For real samples (): . Because the gradient is negative, gradient descent raises and pushes up toward .
Note on the Generator Gradient
In the pure minimax game, the generator minimizes , which can suffer from vanishing gradients early in training when easily spots fakes (). In practice, GANs almost always use the non-saturating generator loss , which applies the exact same BCE gradient derivation with the target flipped to (giving ), propagating stronger gradients back into to raise the discriminator’s score on generated samples.
Deriving the Sigmoid Derivative:
Start with and apply the chain rule:
-
Differentiate wrt :
-
Factor the fraction:
-
Rewrite in terms of : Since and , we obtain:
