alanjiach@gmail.com

← all writing

GANs and perceptual objectives

study notesGANs

Part of the generative visual computing notes.

The adversarial objective

A generator maps noise zz to an image Gθ(z)G_\theta(z). A discriminator predicts whether an image came from the data or the generator. The original game is

min⁡Gmax⁡D  Ex∼p[log⁡D(x)]+Ex∼q[log⁡(1−D(x))].\min_G\max_D\;\mathbb E_{x\sim p}[\log D(x)]+\mathbb E_{x\sim q}[\log(1-D(x))].

Fix the generator. At one location, maximizing plog⁡d+qlog⁡(1−d)p\log d+q\log(1-d) gives

pd−q1−d=0,D∗(x)=p(x)p(x)+q(x).\frac{p}{d}-\frac{q}{1-d}=0, \qquad D^*(x)=\frac{p(x)}{p(x)+q(x)}.

This is a ratio involving both densities. It is not the data density alone. Substitution into the objective gives −log⁡4+2JS⁡(P∥Q)-\log 4+2\operatorname{JS}(P\|Q) under the population assumptions.

The identity characterizes an optimized discriminator. Alternating finite neural-network updates need not reach that optimum. A discriminator output near one half can also result from a weak discriminator, so it does not by itself prove that the distributions match.

Gradients and sample quality

When the discriminator confidently rejects generated images, the original generator objective can provide a weak gradient through the discriminator’s sigmoid. The commonly used non-saturating generator loss is

LG=−Ezlog⁡D(Gθ(z)).L_G=-\mathbb E_z\log D(G_\theta(z)).

During this update, discriminator parameters remain fixed, but input gradients must still pass through it. Detaching its output would remove the generator’s learning signal.

Mode collapse is a separate concern. A generator may produce convincing examples from only a small part of the data distribution. Quality and coverage therefore require separate checks.

Architecture and training choices also matter. Convolutional generators use image structure. Progressive training changes resolution over the course of optimization. Data augmentation can reduce discriminator overfitting, but the transformations must be compatible with the intended data distribution and with the gradients used to train the generator.

Perceptual losses

Squared pixel error favors a conditional mean. To see this, differentiate E[∥Y−a∥2∣X=x]\mathbb E[\|Y-a\|^2\mid X=x] with respect to aa. The minimizer is a=E[Y∣X=x]a=\mathbb E[Y\mid X=x]. Several plausible sharp images can have a blurry average.

A perceptual loss compares features instead:

Lfeat=∑lλl∥ϕl(x^)−ϕl(x)∥2.L_{\mathrm{feat}}=\sum_l\lambda_l\|\phi_l(\hat x)-\phi_l(x)\|^2.

The feature network may be frozen while remaining on the backward path. Matching spatial feature activations constrains content. Matching feature correlations, often represented by Gram matrices, constrains aspects of style. These are different requirements.

A reference-based metric such as LPIPS measures similarity in its learned representation. It does not establish distribution coverage or prove that reconstructed details are faithful.

Wasserstein training

Wasserstein distance assigns a cost to moving probability mass. Its dual uses a 1-Lipschitz critic:

W1(P,Q)=sup⁡∥f∥L≤1EPf(x)−EQf(x).W_1(P,Q)=\sup_{\|f\|_L\leq1}\mathbb E_P f(x)-\mathbb E_Q f(x).

The critic is real-valued, not a probability classifier. A generator minimizes −Ezf(G(z))-\mathbb E_z f(G(z)).

WGAN-GP adds a penalty on the critic’s input-gradient norm at sampled interpolations between real and generated examples. That penalty belongs in the critic update. It is a soft constraint on sampled points, not a proof that the critic is globally 1-Lipschitz.

In training logs, keep the critic’s real-minus-generated score gap separate from its regularization loss. A single combined number can conceal whether a change came from the distribution comparison or from the constraint penalty.

Worked example: an optimal discriminator

Suppose there are only two outcomes, with data probabilities [0.6,0.4][0.6,0.4] and generator probabilities [0.2,0.8][0.2,0.8]. The optimal discriminator assigns the first outcome 0.6/(0.6+0.2)=0.750.6/(0.6+0.2)=0.75 and the second 0.4/(0.4+0.8)=1/30.4/(0.4+0.8)=1/3. It is more confident that the first outcome came from the data because the generator produces it too rarely. At a perfect match, both outputs become 1/21/2.

The Wasserstein distance has a useful geometric example. Put all real probability mass at zero and all generated mass at aa. The distance is ∣a∣|a|, since that is how far the mass must move. It changes continuously as the generated point approaches zero, even when the distributions have disjoint support.

Further reading

Generative Adversarial Networks derives the optimal discriminator. Wasserstein GAN explains the motivation for an objective based on transport distance.