Definition
Adversarial loss is the training signal of a generative adversarial network (GAN). A discriminator is trained to separate real samples from generated ones , . The generator is trained to make wrong. Neither network has a fixed target: is the generator's loss function, and it is learned at the same time.
Pixel and feature losses compare an output with one specific target. Many tasks have no single right answer: there are countless plausible faces, textures or completions of an image. An adversarial loss asks a different question: does this output look like it came from the data distribution? That lets a model commit to one sharp sample instead of averaging the possibilities into blur, which is why adversarial terms are added to reconstruction and perceptual losses in super-resolution, image translation and the autoencoders behind latent diffusion.
Goodfellow et al. (2014) introduced the minimax game and its analysis. Radford, Metz & Chintala's DCGAN (2016) made it trainable with convolutional networks. Training was notoriously unstable, and much of the following work changed the loss. Mao et al. (2017) proposed least squares (LSGAN). Arjovsky, Chintala & Bottou (2017) used the Wasserstein distance (WGAN) and Gulrajani et al. (2017) its gradient penalty (WGAN-GP). Lim & Ye (2017) introduced the hinge loss that spectral normalization (Miyato et al., 2018) and BigGAN (Brock et al., 2019) adopted. Nowozin, Cseke & Tomioka (2016) showed that a family of such games minimizes any chosen -divergence.
The minimax game
- the discriminator's probability that is real; in code with a logit
- a generated sample; its distribution is
- minimizes binary cross-entropy with label 1 for real and 0 for fake, which is the same as maximizing
The optimal discriminator. For a fixed , write and maximize pointwise. The function peaks at , so
- Jensen–Shannon divergence with ; it lies in
- the value at the global optimum , where everywhere
So with a perfect discriminator, training the generator minimizes the JS divergence to the data, and the unique global minimum is . The logit of is the log density ratio . That is the deeper reading of every GAN: the discriminator estimates a density ratio, and the generator moves its samples toward regions where the ratio says the data is denser.
Saturation and the non-saturating loss
Write the generator's loss on one fake as a function of the discriminator's logit . The minimax loss is , with gradient . Early in training the fakes are bad, rejects them confidently, , and the gradient vanishes. Goodfellow et al. therefore suggested a different generator objective with the same fixed point: maximize .
- minimax gradient (saturates); non-saturating gradient (full strength)
- binary cross-entropy of the fakes with the real label 1; in code
BCEWithLogits(D(G(z)), ones)
The fix has a cost. With , Arjovsky & Bottou (2017) showed that the non-saturating gradient is the gradient of . The reverse KL heavily penalizes fakes where there is no data but barely penalizes missing modes, one explanation for mode collapse. The figure checks the identity: for , , so the non-saturating gradient in is .
Other adversarial losses
When and barely overlap, as for distributions on thin manifolds in pixel space, JS is stuck near and the optimal discriminator is nearly perfect, so its gradient carries little information about which way to move. The common variants change the divergence, the shape of the loss, or both. Here and are the raw discriminator outputs (logits or critic scores):
| loss | discriminator / critic | generator | what it minimizes |
|---|---|---|---|
| minimax (Goodfellow 2014) | |||
| non-saturating (NS) | same fixed point; gradient of at | ||
| LSGAN (Mao 2017) | Pearson when ; is common | ||
| WGAN (Arjovsky 2017) | , with 1-Lipschitz | Wasserstein-1 distance | |
| hinge (Lim & Ye 2017) | a margin version of the game; used by SNGAN, SAGAN, BigGAN |
is softplus, so : the first two rows are just BCE on logits. LSGAN uses a linear output and squared error, so a fake that is on the correct side of the decision boundary but far from it still gets a gradient proportional to its distance. WGAN replaces the classifier with a critic and estimates (Kantorovich–Rubinstein duality). keeps growing with the distance between the distributions even when their supports are disjoint, so its gradient does not vanish. The Lipschitz constraint was first imposed by weight clipping, then by a gradient penalty on random interpolates (WGAN-GP, ), or by spectral normalization of each layer. Hinge stops pushing on samples once they are classified with margin 1, which in practice is stable and works well with spectral normalization.
Worked example: one real and one fake
The discriminator outputs on a real image and on a fake, so and (all values checked in Node):
| loss | D loss | G loss | ∂(G loss)/∂a_f |
|---|---|---|---|
| minimax | |||
| non-saturating | |||
| LSGAN (0, 1, 1) | |||
| WGAN | |||
| hinge |
- 1The discriminator's BCE gradients are (push the real logit up) and (push the fake logit down).
- 2Every generator loss has a negative gradient in : all of them push the fake toward "real". They differ in strength. Non-saturating is stronger than minimax here, and the gap grows without bound as .
- 3The hinge discriminator loss is already 0: the real is past margin and the fake sits exactly at . The discriminator stops changing on this pair, while the generator still receives gradient .
- 4WGAN's critic values are unbounded, so its loss can be negative. Only differences between real and fake scores are meaningful, and estimates when the critic is Lipschitz.
1import torch, torch.nn.functional as F23bce = F.binary_cross_entropy_with_logits45def d_loss_ns(real_logits, fake_logits): # standard (minimax / NS) discriminator6 return (bce(real_logits, torch.ones_like(real_logits))7 + bce(fake_logits, torch.zeros_like(fake_logits)))89def g_loss_ns(fake_logits): # non-saturating: fakes labelled real10 return bce(fake_logits, torch.ones_like(fake_logits)) # = softplus(-a).mean()1112def d_loss_hinge(real, fake):13 return F.relu(1 - real).mean() + F.relu(1 + fake).mean()1415def g_loss_hinge(fake): # also the WGAN generator loss16 return -fake.mean()1718def gradient_penalty(D, real, fake, lam=10.0): # WGAN-GP, images (B, C, H, W)19 eps = torch.rand(real.size(0), 1, 1, 1, device=real.device)20 x_hat = (eps * real + (1 - eps) * fake).requires_grad_(True)21 (g,) = torch.autograd.grad(D(x_hat).sum(), x_hat, create_graph=True)22 return lam * ((g.flatten(1).norm(dim=1) - 1) ** 2).mean()2324d_loss_ns(torch.tensor([2.0]), torch.tensor([-1.0])) # 0.440225g_loss_ns(torch.tensor([-1.0])) # 1.31332627def train_step(G, D, opt_g, opt_d, real, z_dim=128):28 z = torch.randn(real.size(0), z_dim, device=real.device)29 fake = G(z)30 opt_d.zero_grad()31 d_loss_ns(D(real), D(fake.detach())).backward() # detach: no generator update here32 opt_d.step()33 opt_g.zero_grad()34 g_loss_ns(D(fake)).backward() # gradient flows through D into G35 opt_g.step()Use when
- +Learns a loss for "looks real" where no hand-written distance exists
- +Allows sharp, committed outputs instead of the blurry mean of pixel losses
- +Fast sampling: one generator forward pass per sample
- +Flexible: conditional, patch-based and auxiliary versions plug into almost any image model
Avoid when
- −A two-player game, not a minimization: oscillation, divergence and sensitivity to hyperparameters
- −Mode collapse: the generator can cover only part of the data distribution
- −Vanishing or uninformative gradients when real and fake supports do not overlap (minimax/JS)
- −Loss curves say little about sample quality; no likelihood to compare models
Check yourself
Early in training, the discriminator rejects every fake with D(G(z)) ≈ 0.001. Which generator loss still gives a useful gradient?
Check yourself
For a fixed generator, what does the optimal discriminator's logit equal?
Remember
- ✓GAN: ; is trained with BCE (real 1, fake 0).
- ✓ and ; 's logit is the log density ratio.
- ✓The minimax generator loss saturates when is confident. Use the non-saturating (BCE with label 1 on the fakes).
- ✓LSGAN (squared error), WGAN ( with a Lipschitz critic) and hinge losses change the divergence and keep gradients alive.
- ✓Implement on logits with
binary_cross_entropy_with_logits, detach fakes in the D step, and judge progress by samples, not loss values.