Search

Search papers and lessons

Least Squares GAN

Level 3 · 12 min read

A least squares GAN trains its discriminator with squared error instead of a sigmoid and log loss, which keeps a useful training signal for fakes the discriminator rejects strongly.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Neural network basics
  • Generative models
  • Linear algebra
  • ReadReady to readNot yet

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 −1-1 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 b=1b = 1 for real and a=−1a = -1 for fake.

  1. 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 D(x)=0.6D(x) = 0.6 and the fake score D(G(z))=−0.4D(G(z)) = -0.4. 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.
  2. Measure the real sample's error. The target is 1, so the error is 0.6−1=−0.40.6 - 1 = -0.4 and the squared error is 0.160.16. Check: a squared error is never negative.
  3. Measure the fake's error. The target is −1-1, so the error is −0.4−(−1)=0.6-0.4 - (-1) = 0.6 and the squared error is 0.360.36. Check: the fake is still on the wrong side of its target, so the error is not zero.
  4. Add up the discriminator's loss. It is half the real error plus half the fake error: 12(0.16)+12(0.36)=0.26\tfrac{1}{2}(0.16) + \tfrac{1}{2}(0.36) = 0.26. The discriminator is trained to make this smaller. Check: a perfect discriminator would score exactly 1 on real and −1-1 on fake, and the loss would be 0.
  5. Measure the generator's loss. The generator wants its fake to score c=0c = 0, the boundary between the two targets. Its loss is 12(−0.4−0)2=0.08\tfrac{1}{2}(-0.4 - 0)^2 = 0.08. Check: the generator's loss is small only when its fake sits on the boundary.
  6. Take a step for each network. The discriminator moves its score for this fake toward −1-1. 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.
  7. 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 −1-1, 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.

−4−3−2−101201234score s of a fakesquared error
  • discriminator loss on a fake, (s + 1)^2
  • generator loss, s^2
Both losses grow as a fake's score moves away from its target. The two marked points are the values at score -0.4 from the walkthrough.

The math

Step 1 in symbols: the setup

Let pdatap_{\text{data}} be the distribution of real samples xx, and let zz be noise drawn from a simple distribution pzp_z, such as a standard normal. The generator GG maps noise to samples G(z)G(z), and pgp_g is the distribution of those outputs. The discriminator DD maps a sample to a real number, with no sigmoid. Three numbers set the targets: aa for fake samples, bb for real samples, and cc for what the generator wants the discriminator to say about its fakes.

Step 2 in symbols: the two objectives

The discriminator minimises

min⁡D  VLS(D)=12 Ex∼pdata[(D(x)−b)2]+12 Ez∼pz[(D(G(z))−a)2],\min_D \; V_{\text{LS}}(D) = \tfrac{1}{2}\,\mathbb{E}_{x \sim p_{\text{data}}}\big[(D(x) - b)^2\big] + \tfrac{1}{2}\,\mathbb{E}_{z \sim p_z}\big[(D(G(z)) - a)^2\big],

and the generator minimises

min⁡G  VLS(G)=12 Ez∼pz[(D(G(z))−c)2].\min_G \; V_{\text{LS}}(G) = \tfrac{1}{2}\,\mathbb{E}_{z \sim p_z}\big[(D(G(z)) - c)^2\big].

The expectation E\mathbb{E} 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 pgp_g is known. At each point xx the discriminator's loss is pdata(x)(D−b)2+pg(x)(D−a)2p_{\text{data}}(x)(D - b)^2 + p_g(x)(D - a)^2 up to the factor 12\tfrac{1}{2}. Setting its derivative in DD to zero gives

D∗(x)=b pdata(x)+a pg(x)pdata(x)+pg(x).D^*(x) = \frac{b\, p_{\text{data}}(x) + a\, p_g(x)}{p_{\text{data}}(x) + p_g(x)}.

This is a weighted average of the two targets. Where real data is common and generated data is rare, D∗D^* is close to b=1b = 1. Where generated data dominates, it is close to a=−1a = -1. 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 s=D(G(z))s = D(G(z)), is ℓ(s)=12(s−c)2\ell(s) = \tfrac{1}{2}(s - c)^2. Its gradient is

ℓ′(s)=s−c.\ell'(s) = s - c.

The gradient grows linearly with the distance from the target, so a fake that scores −3-3 gives a push of size 3, and one that scores −0.4-0.4 gives a push of size 0.4.

Compare this with the sigmoid version. If the discriminator outputs a logit ss and the probability is σ(s)=1/(1+e−s)\sigma(s) = 1/(1 + e^{-s}), the standard generator loss is log⁡(1−σ(s))\log(1 - \sigma(s)). Its derivative with respect to ss is −σ(s)-\sigma(s), which tends to 0 as ss goes to −∞-\infty. 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

Example
1. One real and one fake score (tiny)

Use a=−1a = -1, b=1b = 1 and c=0c = 0. The discriminator scores a real sample 0.50.5 and a fake sample 0.20.2. Find both losses.

  1. Discriminator, real term: (0.5−1)2=0.25(0.5 - 1)^2 = 0.25.
  2. Discriminator, fake term: (0.2−(−1))2=1.22=1.44(0.2 - (-1))^2 = 1.2^2 = 1.44.
  3. Discriminator's total: 12(0.25)+12(1.44)=0.125+0.72=0.845\tfrac{1}{2}(0.25) + \tfrac{1}{2}(1.44) = 0.125 + 0.72 = 0.845.
  4. Generator's loss on the fake: 12(0.2−0)2=12(0.04)=0.02\tfrac{1}{2}(0.2 - 0)^2 = \tfrac{1}{2}(0.04) = 0.02.
  5. 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.
Example
2. A confidently rejected fake (standard case)

A fake sample has score s=−3s = -3. Compare the size of the training push under the two losses.

  1. Least squares: the gradient is s−c=−3−0=−3s - c = -3 - 0 = -3, so the push has size 33.
  2. Sigmoid version: the probability is σ(−3)=1/(1+e3)≈1/21.09≈0.047\sigma(-3) = 1/(1 + e^{3}) \approx 1/21.09 \approx 0.047, and the gradient is −σ(−3)-\sigma(-3), so the push has size about 0.0470.047.
  3. Ratio: 3/0.047≈633 / 0.047 \approx 63. The least squares push is about 63 times stronger.
  4. Now take a fake near the boundary, with score s=−0.4s = -0.4. The least squares push is 0.40.4. The sigmoid push is σ(−0.4)=1/(1+e0.4)≈1/2.49≈0.40\sigma(-0.4) = 1/(1 + e^{0.4}) \approx 1/2.49 \approx 0.40. The two are about the same.
  5. 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.
Example
3. A batch of four images (realistic)

A discriminator scores a batch of four images for a generator that makes images of product defects. Two are real, with scores 0.90.9 and 0.70.7. Two are fakes, with scores −0.2-0.2 and −1.6-1.6. Use a=−1a = -1, b=1b = 1 and c=0c = 0.

  1. Real errors squared: (0.9−1)2=0.01(0.9 - 1)^2 = 0.01 and (0.7−1)2=0.09(0.7 - 1)^2 = 0.09. Their mean is 0.050.05.
  2. Fake errors squared, against −1-1: (−0.2+1)2=0.64(-0.2 + 1)^2 = 0.64 and (−1.6+1)2=0.36(-1.6 + 1)^2 = 0.36. Their mean is 0.500.50.
  3. Discriminator's loss on the batch: 12(0.05)+12(0.50)=0.275\tfrac{1}{2}(0.05) + \tfrac{1}{2}(0.50) = 0.275.
  4. Generator's loss on the same two fakes, against 0: (−0.2)2=0.04(-0.2)^2 = 0.04 and (−1.6)2=2.56(-1.6)^2 = 2.56. Their mean is 1.301.30, and half of it is 0.650.65.
  5. Interpretation: the second fake, at −1.6-1.6, 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.
  6. Try it: the fake scored −1.6-1.6 is replaced by a fake scored 0.40.4. What is the generator's loss on the two fakes now?

Answer: the squared errors against 0 are 0.040.04 and 0.160.16. Their mean is 0.100.10, and half of it is 0.050.05. The generator's loss falls from 0.650.65 to 0.050.05 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 −1-1 and 11 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 aa, bb and cc is arbitrary. In fact it sets where the decision boundary sits and what the generator aims at. The paper's choice a=−1a = -1, b=1b = 1, c=0c = 0 also satisfies conditions under which the generator's objective matches a Pearson chi-square divergence between pdata+pgp_{\text{data}} + p_g and 2pg2p_g, 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.

LossDiscriminator outputGenerator signal for a fake the discriminator rejects strongly
Standard GANA probability from a sigmoidWeak: the derivative is the probability, which is near 0
Least squares GANA raw scoreStrong: 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 a=−1a = -1 for fake, b=1b = 1 for real, and c=0c = 0 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 aa, bb and cc in the least squares GAN, and what does each one set?Show answer
aa is the target score for fake samples, bb is the target score for real samples, and cc is the score the generator wants the discriminator to give its fakes. The paper's choice is a=−1a = -1, b=1b = 1 and c=0c = 0.
QuestionA discriminator scores a real sample 0.8 and a fake sample −0.5-0.5, with a=−1a = -1 and b=1b = 1. What is the discriminator's least squares loss on these two samples, using the one-half form?Show answer
The real error squared is (0.8−1)2=0.04(0.8 - 1)^2 = 0.04, and the fake error squared is (−0.5+1)2=0.25(-0.5 + 1)^2 = 0.25. The loss is 12(0.04)+12(0.25)=0.02+0.125=0.145\tfrac{1}{2}(0.04) + \tfrac{1}{2}(0.25) = 0.02 + 0.125 = 0.145.
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
Its derivative with respect to the score is −σ(s)-\sigma(s), the probability itself. When the discriminator is confident the fake is fake, ss is very negative, so σ(s)\sigma(s) is close to 0 and the gradient almost vanishes.
QuestionA team's GAN has stopped improving, and its discriminator gives fakes very negative scores. They switch to least squares with c=0c = 0. What changes for the generator on those fakes, and what should they still check?Show answer
The generator's gradient becomes the score minus cc, so fakes far below 0 get a strong push toward the boundary instead of a weak one. The team should still check the images against held-out data, because a better loss does not by itself guarantee that the generator covers the whole data distribution or avoids mode collapse.

Sources