TL;DR
A least squares GAN, or LSGAN, is a GAN that trains its discriminator with a squared-error loss instead of the usual sigmoid and log loss. The discriminator is asked to output 1 for real samples and for fakes, and the generator is asked to make its fakes score 0. Squared error keeps a strong training signal for fakes that the discriminator rejects by a wide margin, where the usual loss gives almost none. The paper reports that this makes training more stable. The code change is small, but it changes what the networks are asked to do.
Builds on: GAN, Mean Squared Error, Vanishing Gradient Problem · Leads to: HiFi-GAN
Where it's used
Image generation experiments. The paper that introduced LSGAN, by Xudong Mao and coauthors and published at ICCV in 2017, trained generators on image datasets such as LSUN, a large collection of scene images, and showed generated pictures to compare against standard GANs. Its main argument was about training behaviour, not a new kind of image.
Research baselines. When a GAN's training stalls, one cheap first experiment is to change only the loss. Because the switch touches the discriminator and generator loss functions and nothing else, it is a cheap way to test whether the stall comes from the loss.
How it works
We start from a standard GAN, which uses a sigmoid on the discriminator's output and a log loss. The least squares version keeps the same two-player contest and changes only the arithmetic. Here is one round with a single real sample and a single fake. The discriminator outputs a raw score, with no sigmoid, and we use the targets for real and for fake.
- Get the two samples. The real sample is an image from the training set, and the fake is the generator's output for some noise. For the tiny case, call the real score and the fake score . Check: the real sample scores above zero and the fake below zero, so the discriminator is on the right side of the boundary for both.
- Measure the real sample's error. The target is 1, so the error is and the squared error is . Check: a squared error is never negative.
- Measure the fake's error. The target is , so the error is and the squared error is . Check: the fake is still on the wrong side of its target, so the error is not zero.
- Add up the discriminator's loss. It is half the real error plus half the fake error: . The discriminator is trained to make this smaller. Check: a perfect discriminator would score exactly 1 on real and on fake, and the loss would be 0.
- Measure the generator's loss. The generator wants its fake to score , the boundary between the two targets. Its loss is . Check: the generator's loss is small only when its fake sits on the boundary.
- Take a step for each network. The discriminator moves its score for this fake toward . The generator changes its output so that the fake's score moves toward 0, which is an increase here. Check: the direction of both moves matches the targets above.
- Repeat. As the generator improves, fakes score closer to the boundary and then to the real side, and both losses fall.
The plot shows the two losses as a function of the score for a fake sample. The discriminator's loss is large when the fake scores far from , and the generator's loss is large when the fake scores far from 0. Both curves are parabolas, so they keep rising as the score moves away.
- discriminator loss on a fake, (s + 1)^2
- generator loss, s^2
The math
Step 1 in symbols: the setup
Let be the distribution of real samples , and let be noise drawn from a simple distribution , such as a standard normal. The generator maps noise to samples , and is the distribution of those outputs. The discriminator maps a sample to a real number, with no sigmoid. Three numbers set the targets: for fake samples, for real samples, and for what the generator wants the discriminator to say about its fakes.
Step 2 in symbols: the two objectives
The discriminator minimises
and the generator minimises
The expectation is the average over the distribution written under it. Step 2 of the walkthrough is the first term, and step 5 is the generator's term.
Step 3 in symbols: the discriminator's best response
Fix the generator, so is known. At each point the discriminator's loss is up to the factor . Setting its derivative in to zero gives
This is a weighted average of the two targets. Where real data is common and generated data is rare, is close to . Where generated data dominates, it is close to . Where the two densities are equal, it is exactly the midpoint, 0. That midpoint is the decision boundary the generator is aiming for.
Step 4 in symbols: why the gradient does not vanish
The generator's loss for one fake, as a function of its score , is . Its gradient is
The gradient grows linearly with the distance from the target, so a fake that scores gives a push of size 3, and one that scores gives a push of size 0.4.
Compare this with the sigmoid version. If the discriminator outputs a logit and the probability is , the standard generator loss is . Its derivative with respect to is , which tends to 0 as goes to . A fake that is rejected very confidently gives almost no gradient. That is the vanishing gradient problem in its GAN form, and the squared loss avoids it.
Worked examples
Use , and . The discriminator scores a real sample and a fake sample . Find both losses.
- Discriminator, real term: .
- Discriminator, fake term: .
- Discriminator's total: .
- Generator's loss on the fake: .
- Interpretation: the discriminator's loss is high because the fake's score is still 1.2 away from its target. The generator's loss is small because the fake's score is already close to the boundary at 0.
A fake sample has score . Compare the size of the training push under the two losses.
- Least squares: the gradient is , so the push has size .
- Sigmoid version: the probability is , and the gradient is , so the push has size about .
- Ratio: . The least squares push is about 63 times stronger.
- Now take a fake near the boundary, with score . The least squares push is . The sigmoid push is . The two are about the same.
- Interpretation: near the boundary the two losses behave alike. Far from it, the squared loss keeps pushing and the sigmoid loss has nearly stopped. This is the difference that the paper uses as its argument.
A discriminator scores a batch of four images for a generator that makes images of product defects. Two are real, with scores and . Two are fakes, with scores and . Use , and .
- Real errors squared: and . Their mean is .
- Fake errors squared, against : and . Their mean is .
- Discriminator's loss on the batch: .
- Generator's loss on the same two fakes, against 0: and . Their mean is , and half of it is .
- Interpretation: the second fake, at , contributes most of the generator's loss. The squared loss punishes it heavily, so the generator is pushed hardest on the image the discriminator rejects most strongly.
- Try it: the fake scored is replaced by a fake scored . What is the generator's loss on the two fakes now?
Answer: the squared errors against 0 are and . Their mean is , and half of it is . The generator's loss falls from to because its fakes are now close to the boundary.
Common mistakes and tradeoffs
It is tempting to think that the least squares discriminator becomes a regression model for the data. In fact it still separates real from fake. The output is a score, and the boundary between the two classes is where the score crosses 0. The targets and are how far apart the classes are asked to be.
It is tempting to think a squared loss always gives better images. In fact the paper's gains are on its own experiments, and the result depends on the architecture, the data and the training schedule. A squared loss does not guarantee that the generator covers the whole data distribution, and mode collapse, where the generator produces only a few kinds of output, can still happen.
It is tempting to think the choice of , and is arbitrary. In fact it sets where the decision boundary sits and what the generator aims at. The paper's choice , , also satisfies conditions under which the generator's objective matches a Pearson chi-square divergence between and , a measure of how different two distributions are. Changing these numbers changes that connection.
When to use it. Use it when the standard loss stalls, when generated samples that the discriminator rejects strongly give almost no learning signal, or when you want a single-line change to test the loss.
When not to use it. If your architecture already assumes a sigmoid output, switching to a raw score takes more work than the loss change suggests. If you need a distance with clear mathematical properties between distributions, a Wasserstein based loss is the alternative the field often reaches for.
| Loss | Discriminator output | Generator signal for a fake the discriminator rejects strongly |
|---|---|---|
| Standard GAN | A probability from a sigmoid | Weak: the derivative is the probability, which is near 0 |
| Least squares GAN | A raw score | Strong: the derivative is the distance from the target |
History
In 2014 Ian Goodfellow and coauthors introduced generative adversarial networks, with a discriminator that outputs a probability and a log-based objective. The idea worked, but training was often unstable, and much of the research that followed was about making it more reliable.
In November 2016, Xudong Mao and coauthors posted the least squares version. It was published at ICCV in 2017. It caught on because it changed one part of the loss and kept the rest of the architecture, which made it easy to compare against the standard GAN in the same experiments. Its argument, that the sigmoid loss can stop giving useful gradients for confidently rejected samples, is the reason it is still taught as a stability fix rather than as a new model.
Recap
- A GAN trains a generator and a discriminator against each other.
- The standard loss uses a sigmoid and a log, and its generator gradient can vanish for fakes that are rejected strongly.
- The least squares version uses a raw score with targets for fake, for real, and for the generator.
- The squared error's gradient grows with the distance from the target, so far-away fakes still produce a useful push.
- Read next: HiFi-GAN, a later model that works on speech, and Wasserstein distance, the other common route to more stable GAN training.
Check yourself
QuestionWhat are the three numbers , and in the least squares GAN, and what does each one set?Show answer
QuestionA discriminator scores a real sample 0.8 and a fake sample , with and . What is the discriminator's least squares loss on these two samples, using the one-half form?Show answer
QuestionWhy does the sigmoid version of the generator's loss give a weak signal for a fake that the discriminator rejects with high confidence?Show answer
QuestionA team's GAN has stopped improving, and its discriminator gives fakes very negative scores. They switch to least squares with . What changes for the generator on those fakes, and what should they still check?Show answer
Sources
- Mao, X. et al. (2017). Least Squares Generative Adversarial Networks. arXiv: https://arxiv.org/abs/1611.04076
- Goodfellow, I. et al. (2014). Generative Adversarial Networks. arXiv: https://arxiv.org/abs/1406.2661