DL0101 GAN Loss Functions

What loss functions do the GAN generator and discriminator optimize?

Answer

The discriminator optimizes an ordinary binary cross-entropy objective: label real samples 1, generated samples 0, and minimize the negative log-likelihood of that two-class problem. The generator is defined by the same value function played in the opposite direction, so the textbook formulation is the minimax game \min_{G} \max_{D} V(D,G), but almost nobody implements the generator that way. Minimizing \log(1 - D(G(z))) saturates exactly when the generator is bad, which is precisely when it needs signal, so Goodfellow’s original paper already proposed the non-saturating form: maximize \log D(G(z)), or equivalently minimize -\log D(G(z)). Both variants share the same fixed point, where the optimal discriminator is D^{*}(x) = p_{r}(x)/(p_{r}(x)+p_{g}(x)) and the outer objective reduces to the Jensen-Shannon divergence between data and model. In current practice the exact loss shape matters less than the regularizer attached to it, and the common defaults are the non-saturating logistic loss or the hinge loss combined with an R1 gradient penalty on real data.

(1) Discriminator Is Just A Classifier: its loss is standard BCE over a batch of half real and half fake samples, so any logit-based classification loss can be dropped in without changing the game structure.
(2) Saturating Vs Non-Saturating Generator: the two forms have identical optima but very different gradient magnitudes; the non-saturating loss keeps a large gradient when D(G(z)) is near 0.
(3) Optimal Discriminator Reveals A Divergence: substituting D^{*} turns the value function into 2\,\mathrm{JSD}(p_{r} \,\|\, p_{g}) - \log 4, which is why a perfect discriminator gives no useful gradient on disjoint supports.
(4) Alternatives Change The Metric: LSGAN uses a squared error, WGAN replaces the classifier with a 1-Lipschitz critic estimating the Earth-Mover distance, and hinge loss caps the penalty once a margin is met.
(5) Regularization Dominates Stability: zero-centered gradient penalties such as R1 are what make training converge locally, and swapping loss families with the same regularizer usually moves FID far less than practitioners expect.

Two panel chart: left panel plots the saturating generator loss log of one minus D and the non-saturating loss minus log D against the discriminator score on a fake sample, right panel plots the magnitude of their gradients on a logarithmic axis showing the non-saturating gradient is far larger when the score is near zero

Figure 1: Early in training the discriminator confidently rejects fakes, so D(G(z)) sits near 0. At D(G(z)) = 0.05 the saturating loss supplies a gradient magnitude of 1.05 while the non-saturating loss supplies 20, about 19x more signal in exactly the regime where the generator is weakest.

Mathematical Formulation:
\min_{G} \max_{D} V(D, G)
V(D,G) = \mathbb{E}_{x \sim p_{r}}[\log D(x)]
+ \mathbb{E}_{z \sim p_{z}}[\log (1 - D(G(z)))]
\mathcal{L}_{G}^{\mathrm{sat}} = \mathbb{E}_{z}[\log (1 - D(G(z)))]
\mathcal{L}_{G}^{\mathrm{ns}} = -\mathbb{E}_{z}[\log D(G(z))]
D^{*}(x) = \frac{p_{r}(x)}{p_{r}(x) + p_{g}(x)}
V(D^{*}, G) = 2\,\mathrm{JSD}(p_{r} \,\|\, p_{g}) - \log 4

Where:

  • V(D,G) is the value function; the discriminator maximizes it and the generator minimizes it, so \mathcal{L}_{D} = -V is the batch-level binary cross-entropy actually coded up.
  • x \sim p_{r} is a real sample and D(x) \in [0,1] is the estimated probability that its input is real.
  • z \sim p_{z} is a latent drawn from a fixed prior, usually \mathcal{N}(0, I); G(z) is the generated sample and p_{g} the distribution it induces.
  • \mathcal{L}_{G}^{\mathrm{sat}} is the literal minimax generator loss and \mathcal{L}_{G}^{\mathrm{ns}} the non-saturating heuristic; they share the same optimum but differ in gradient scale by a factor of (1 - D)/D.
  • D^{*} is the optimal discriminator for a fixed G, obtained by maximizing the integrand pointwise; it equals 1/2 everywhere when p_{g} = p_{r}.
  • \mathrm{JSD} is bounded in [0, \log 2], so V(D^{*},G) ranges over [-\log 4, 0] and hits its global minimum -\log 4 \approx -1.386 only at p_{g} = p_{r}.
  • Practical condition: D^{*} is never reached, since the discriminator takes k steps (typically k = 1) per generator step, and the whole derivation assumes optimization in function space rather than over network parameters.

The main alternatives keep the same alternating structure and change only the per-sample penalty. LSGAN replaces the log with a squared error, which pushes fake samples toward the decision boundary and stops the loss from flattening, at the cost of also penalizing samples that are already comfortably on the correct side. WGAN drops the sigmoid entirely: the critic maximizes \mathbb{E}[D(x)] - \mathbb{E}[D(G(z))] under a 1-Lipschitz constraint, enforced in WGAN-GP by a two-sided penalty on the gradient norm at interpolated points, and the generator simply maximizes \mathbb{E}[D(G(z))]. Hinge loss, the default in BigGAN and many diffusion-era discriminators, uses \max(0, 1 - D(x)) on reals and \max(0, 1 + D(G(z))) on fakes, so a correctly classified sample beyond the margin contributes exactly zero gradient and the discriminator stops overfitting easy examples. Independently of the family chosen, the R1 penalty \frac{\gamma}{2}\mathbb{E}_{x \sim p_{r}}[\|\nabla_{x} D(x)\|^{2}] is what turns the local dynamics from oscillatory into convergent.

Line chart of the discriminator loss contributed by a single fake sample as a function of its logit, comparing the smooth softplus curve of binary cross-entropy, the piecewise linear hinge loss that becomes exactly zero below minus one, and the unbounded linear Wasserstein critic loss

Figure 2: Loss contributed by one fake sample as a function of its logit t. BCE uses \log(1 + e^{t}) and decays smoothly but never quite to zero, hinge is exactly flat once the sample clears the -1 margin, and the Wasserstein critic stays linear so it never saturates and must be constrained by a Lipschitz penalty.

PropertyNon-saturating BCEHingeWGAN-GP
Discriminator term on a fakesoftplus of the logit, smooth and always positivemax(0, 1 + logit), exactly zero past the marginthe raw critic score, unbounded and linear
Generator termminus log of the fake probabilityminus the critic score, no hinge appliedminus the critic score
Behavior when D winsgradient shrinks but stays usable via the non-saturating formD stops learning from easy samples, which limits overfittingscore gap keeps growing, so the generator still gets signal
Extra cost per stepnone beyond an optional R1 penalty, often lazily applied every 16 stepssame as BCEa double backward pass for the penalty plus roughly 5 critic steps per generator step
Typical useStyleGAN family with R1, and most adversarial auxiliary lossesBigGAN, SAGAN, and many feature-space discriminatorssettings where a meaningful loss-versus-quality correlation is wanted

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *