Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Pulling fakes all the way in
In a standard GAN the discriminator outputs a probability. Once it thinks a fake looks real, it stops complaining about it, even if the fake is still far from any real sample. LSGAN changes the discriminator into a scorer trained with squared error. Real samples should score 1 and fakes 0, and the generator wants its fakes to score 1. A fake that scores 3 is just as wrong as one that scores −1, so it still gets pushed. The demo plots how strongly each loss pushes a fake, depending on the score the discriminator gives it.
Least-squares adversarial losses are common in image translation and enhancement pipelines (CycleGAN, pix2pixHD), for example converting radar images to optical-looking images or enhancing low-light camera frames.
- 1. LSGAN trains the discriminator with…
- 2. A fake already scored as real gets, under least squares,…
- 3. LSGAN's discriminator output is…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Losses. D minimises ½ E over p_data of (D(x) − b)² + ½ E over p_z of (D(G(z)) − a)². G minimises ½ E over p_z of (D(G(z)) − c)². D has a linear output.
- Targets. (a, b, c) = (−1, 1, 0) satisfies b − c = 1 and b − a = 2 (the χ² case). (0, 1, 1) is the popular choice: G wants fakes to look exactly like reals to D.
- Optimal discriminator. For fixed G, pointwise minimisation of p(D − b)² + q(D − a)² gives D*(x) = (b p(x) + a q(x))/(p(x) + q(x)). With (0, 1) it is the same as the GAN’s p/(p + q).
- Divergence. With b − c = 1 and b − a = 2, 2C(G) = ∫ (p_data(x) − p_g(x))²/(p_data(x) + p_g(x)) dx, which the paper writes as χ²_Pearson(p_data + p_g ‖ 2p_g).
- Gradient shape. ∂/∂D of ½(D − c)² is D − c: linear in the distance from the target, never saturating. Compare the non-saturating loss, whose gradient −(1 − σ(l)) vanishes for fakes scored confidently as real.
- Adoption. CycleGAN (Zhu et al. 2017) and pix2pixHD (Wang et al. 2018) use the least-squares loss; many audio and video GANs followed.
- Evidence. Lucic et al. (2018), “Are GANs created equal?”, compared losses under equal tuning budgets and found no loss consistently better; most differences came from hyperparameters and random seeds.
- 1. The LSGAN optimal discriminator is…
- 2. The χ² case needs…
- 3. Lucic et al. (2018) found…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Losses and the χ² identity
Exercise 1.
- LSGAN discriminator and generator losses with general targets.
- The optimal least-squares discriminator (bp + aq)/(p + q).
- The generator objective at the optimum on a grid, checked against ∫ (p − q)²/(p + q).
Practice.
- Default targets: (a, b, c) = (0, 1, 1) with a linear D output; CycleGAN’s implementation uses MSELoss against tensors of ones and zeros.
- Combine with other stabilisers: spectral normalisation, a replay buffer of past fakes (CycleGAN) or a gradient penalty.
- Watch the scale: squared losses are unbounded, so large discriminator outputs give large gradients. Keep learning rates moderate and monitor D’s output range.
- Do not expect a free lunch: tune the learning rate and the number of D steps for each loss; compare losses over several seeds.
import torch
mse = torch.nn.MSELoss()
# discriminator step (targets a = 0, b = 1)
d_loss = 0.5 * mse(D(real), torch.ones_like(D(real))) + 0.5 * mse(D(fake.detach()), torch.zeros_like(D(real)))
# generator step (target c = 1)
g_loss = 0.5 * mse(D(fake), torch.ones_like(D(real)))- 1. CycleGAN's adversarial loss is…
- 2. Squared losses are…
- 3. To compare losses fairly…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Optimal discriminator. For fixed G, V_LS(D) = ½ ∫ [p (D − b)² + q (D − a)²] dx. The integrand is a convex quadratic in D(x) with derivative p (D − b) + q (D − a) = 0 at D* = (b p + a q)/(p + q).
Generator objective at the optimum. Add the term ½ E over p_data of (D(x) − c)², which does not depend on G, to the generator loss. Then 2C(G) = ∫ (p + q)(D* − c)² dx. Since D* − c = [(b − c) p + (a − c) q]/(p + q) = [(b − c)(p + q) − (b − a) q]/(p + q), we get 2C(G) = ∫ [(b − c)(p + q) − (b − a) q]²/(p + q) dx. With b − c = 1 and b − a = 2 this is ∫ (p − q)²/(p + q) dx, which is zero iff q = p. Mao et al. write it as χ²_Pearson(p + q ‖ 2q), with the mixture p + q in the denominator.
Why it might help. Near a fake that the discriminator scores as real, the sigmoid loss is flat: −log σ(l) has gradient −(1 − σ(l)) ≈ 0 for large l. The least-squares loss has gradient D − c, which only vanishes on the target. The penalty therefore pulls fakes toward the decision boundary and the data, and D’s gradients stay informative.
Why it might not. The argument concerns the shape of the loss in D’s output, not the geometry of the data. With disjoint supports the χ² divergence, like JSD, does not measure distance in sample space. In practice the discriminator’s capacity, the learning rates and the optimizer dominate, and unbounded squared losses can produce large, noisy gradients.
- 1. D* for least squares solves…
- 2. With b − c = 1 and b − a = 2, 2C(G) equals…
- 3. The least-squares generator gradient vanishes only…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does the least-squares loss make a small GAN cover more modes of a ring of Gaussians?
History.
- X. Mao, Q. Li, H. Xie, R. Lau, Z. Wang and S. Smolley, “Least squares generative adversarial networks” (ICCV 2017); extended in TPAMI (2019).
- J.-Y. Zhu, T. Park, P. Isola and A. Efros, “Unpaired image-to-image translation using cycle-consistent adversarial networks” (ICCV 2017): CycleGAN, with a least-squares loss.
- M. Lucic, K. Kurach, M. Michalski, S. Gelly and O. Bousquet, “Are GANs created equal? A large-scale study” (NeurIPS 2018).
Exercise 2. A ring of 8 Gaussians (radius 2, std 0.1), the toy problem Mao et al. used to argue for stability.
- Models: G 2 → 32 → 32 → 2 and D 2 → 32 → 32 → 1 with LeakyReLU(0.2). Three non-saturating GANs (sigmoid cross-entropy) and three LSGANs (targets a = 0, b = c = 1, linear output) are trained side by side, each with its own initialisation.
- Training: Adam (lr 0.002, β1 = 0.5), batch 64, 1000 steps, identical for all six.
- Measures on 2000 samples: modes holding at least a quarter of their fair share of samples within 0.3 of a centre, and the share of samples on a mode.
| seed | non-saturating: modes | non-saturating: on a mode | LSGAN: modes | LSGAN: on a mode |
|---|---|---|---|---|
| 0 | 1, 5, 7 | 0.14, 0.28, 0.46 | 3, 3, 1 | 0.18, 0.2, 0.16 |
| 1 | 8, 5, 8 | 0.53, 0.26, 0.59 | 1, 3, 3 | 0.16, 0.17, 0.26 |
| 2 | 7, 6, 8 | 0.44, 0.43, 0.58 | 6, 3, 4 | 0.55, 0.26, 0.18 |
- Null result: over 9 runs each, LSGAN covered 3.0 modes on average against 6.1 for the non-saturating GAN; 3 non-saturating runs reached all 8 modes, no LSGAN run did.
- Large run-to-run spread for both losses (1 to 8 modes for the non-saturating GAN): one run per loss would have been enough to ‘show’ either loss winning.
- Caveat: one architecture, one learning rate and 1000 steps. The LSGAN paper used different networks and longer training; the honest conclusion is that the loss alone did not fix mode dropping here, in line with Lucic et al. (2018).
LSGAN in AI, robotics and aerospace
- Image translation: CycleGAN-style SAR-to-optical, day-to-night or simulation-to-real translation for robot perception usually uses the least-squares adversarial loss.
- Enhancement and super-resolution: pix2pixHD-style generators for map, terrain and thermal imagery.
- Speech and audio: several neural vocoders (for example HiFi-GAN, Kong et al. 2020) use least-squares adversarial losses with feature matching.
References
- X. Mao, Q. Li, H. Xie, R. Y. K. Lau, Z. Wang and S. P. Smolley (2017). “Least squares generative adversarial networks.” ICCV.
- X. Mao et al. (2019). “On the effectiveness of least squares generative adversarial networks.” IEEE TPAMI.
- J.-Y. Zhu, T. Park, P. Isola and A. A. Efros (2017). “Unpaired image-to-image translation using cycle-consistent adversarial networks.” ICCV.
- M. Lucic, K. Kurach, M. Michalski, S. Gelly and O. Bousquet (2018). “Are GANs created equal? A large-scale study.” NeurIPS.
- J. Kong, J. Kim and J. Bae (2020). “HiFi-GAN: generative adversarial networks for efficient and high fidelity speech synthesis.” NeurIPS.