Skip to content
BIASED

Autoencoding GANs: AAE and α-GAN

How to give a GAN an encoder and a VAE sharp samples: a discriminator on codes matches their cloud to the prior, a discriminator on images removes the blur, and Wasserstein critics make training steadier.

What each family is missing

The topic Wasserstein GANs and Lipschitz constraints gave the generator a loss whose gradient survives when the samples are far from the data. Two things are still missing, and the VAE has both. A GAN has no encoder: to find the code of a real photograph, StyleGAN included, one has to solve an optimization problem for every image. And nothing ties a GAN to each training image, so it can quietly drop modes. The VAE of The reparameterization trick has an encoder, and every training image must be rebuilt from its code. But its samples are blurry.

Can one model keep the encoder and the coverage of a VAE and the sharpness of a GAN? Start from the ELBO and ask which of its parts we actually need:

L(x)=Eqφ(z∣x)[ln⁡pθ(x∣z)]−DKL(qφ(z∣x) ∥ p(z)).\mathcal L(x) = \mathbb E_{q_\varphi(z \mid x)}\big[\ln p_\theta(x \mid z)\big] - D_{\mathrm{KL}}\big(q_\varphi(z \mid x)\,\|\,p(z)\big).

With a Gaussian decoder the first term is a squared error. It gives coverage, since no image may be ignored. Its optimum, the average of the images that fit one code, gives the blur.

The KL term is worth splitting. Let qφ(z)=Ex∼P qφ(z∣x)q_\varphi(z) = \mathbb E_{x \sim P}\, q_\varphi(z \mid x) be the aggregate posterior, the distribution of codes over the whole dataset. Write the ratio inside the logarithm as qφ(z)p(z)⋅qφ(z∣x)qφ(z)\frac{q_\varphi(z)}{p(z)} \cdot \frac{q_\varphi(z \mid x)}{q_\varphi(z)} and average each factor's logarithm over images and codes. The result:

Ex∼P DKL(qφ(z∣x) ∥ p(z))=I(X;Z)+DKL(qφ(z) ∥ p(z)).\mathbb E_{x \sim P}\, D_{\mathrm{KL}}\big(q_\varphi(z \mid x)\,\|\,p(z)\big) = I(X; Z) + D_{\mathrm{KL}}\big(q_\varphi(z)\,\|\,p(z)\big).

The first part, the mutual information between images and codes, charges every image for an informative code, although reconstruction needs exactly that information. Only the second part matters for sampling: a prior sample decodes well only if it lands where the codes of real images lie.

The models of this topic keep the encoder and the reconstruction term. The marginal mismatch, and then the blur, they hand to discriminators.

Adversarial autoencoders: a discriminator on codes

The adversarial autoencoder (AAE) of Makhzani et al. (2015) is an ordinary autoencoder with a third network attached to its code. The encoder EφE_\varphi maps an image to a code z^=Eφ(x)\hat z = E_\varphi(x). The generator GθG_\theta, which is the decoder, maps it back to x^=Gθ(z^)\hat x = G_\theta(\hat z). The code discriminator CωC_\omega tells codes from samples of the prior p(z)=N(0,I)p(z) = \mathcal N(0, I). For a deterministic encoder, qφ(z)q_\varphi(z) is simply the distribution of Eφ(x)E_\varphi(x) with x∼Px \sim P.

One change of notation holds for the whole topic. Both discriminators here output a real-valued score, a logit, and σ\sigma of the score is the probability "real". GANs as a two-player game already computed its losses from logits in code. Later the same networks will become Wasserstein critics.

Each minibatch has two phases. In the reconstruction phase, EφE_\varphi and GθG_\theta minimize the squared error Ex∥x−Gθ(Eφ(x))∥2\mathbb E_x \lVert x - G_\theta(E_\varphi(x)) \rVert^2. In the regularization phase, CωC_\omega learns to call prior samples real and codes fake:

LC=−Ez∼p(z)ln⁡σ(Cω(z))−Ex∼Pln⁡(1−σ(Cω(z^))).\mathcal L_C = -\mathbb E_{z \sim p(z)} \ln \sigma\big(C_\omega(z)\big) - \mathbb E_{x \sim P} \ln\Big(1 - \sigma\big(C_\omega(\hat z)\big)\Big).

Then the encoder, the generator of this small GAN, is updated to fool it, for example with the non-saturating loss −Exln⁡σ(Cω(z^))-\mathbb E_x \ln \sigma\big(C_\omega(\hat z)\big).

CωC_\omega sees one code at a time and never learns which image it came from. So it compares the whole cloud of codes with the prior. The optimal discriminator of the GAN topic, applied to codes, gives σ(C∗(z))=p(z)/(p(z)+qφ(z))\sigma\big(C^*(z)\big) = p(z) / \big(p(z) + q_\varphi(z)\big). Inverting the sigmoid turns this into a score:

C∗(z)=ln⁡p(z)qφ(z).C^*(z) = \ln \frac{p(z)}{q_\varphi(z)} .

A classifier's logit estimates a log density ratio. This density-ratio trick drives every model below. Substitute C∗C^* into the minimax loss, as in the GAN topic, and the encoder minimizes 2 DJS(p ∥ qφ)−2ln⁡22\,D_{\mathrm{JS}}(p \,\|\, q_\varphi) - 2\ln 2. Compared with the ELBO, the AAE keeps the marginal mismatch, measured by JS instead of KL, and drops the penalty on informative codes.

The discriminator needs only samples of the prior, never its density, so any prior we can sample will do. With a 2-D Gaussian prior, the codes of held-out MNIST digits fill the prior without gaps, while a VAE with the same architecture leaves holes (Makhzani et al., 2015, Figure 2). The decoder, however, still learns from a pixel error alone, so its outputs are as blurry as a VAE's.

α-GAN: a discriminator on images as well

The α-GAN of Rosca et al. (2017) starts again from the ELBO and replaces each term with something a discriminator can estimate.

The KL term becomes a code discriminator, as in the AAE. By the density-ratio trick,

−DKL(qφ(z) ∥ p(z))=Eqφln⁡p(z)qφ(z)≈Eqφ Cω(z).-D_{\mathrm{KL}}\big(q_\varphi(z)\,\|\,p(z)\big) = \mathbb E_{q_\varphi} \ln \frac{p(z)}{q_\varphi(z)} \approx \mathbb E_{q_\varphi}\, C_\omega(z).

This is the marginal KL, the part that sampling needs.

The likelihood term gets two candidates that fail in opposite ways. The first is a Laplace likelihood over DD pixels:

pθ(x∣z)=(λ2)Dexp⁡(−λ∥x−Gθ(z)∥1).p_\theta(x \mid z) = \Big(\frac{\lambda}{2}\Big)^{D} \exp\big(-\lambda \lVert x - G_\theta(z) \rVert_1\big).

Its negative logarithm is the L1L_1 reconstruction loss plus a constant. It covers every training image, but it still blurs. Per pixel, the derivative of E∣x−a∣\mathbb E\lvert x - a \rvert in aa is P(x<a)−P(x>a)P(x < a) - P(x > a), so the optimum sits where half of the plausible values lie on each side. A squared error would pick their mean instead.

The second candidate is a synthetic likelihood: the score Dψ(x^)D_\psi(\hat x) of an image discriminator trained to tell data from decoded images. It gives sharp images but, like any GAN, allows mode collapse. α-GAN takes both. With a deterministic encoder, EφE_\varphi and GθG_\theta maximize

Ex∼P[−λ∥x−x^∥1+Dψ(x^)+Cω(z^)].\mathbb E_{x \sim P}\Big[-\lambda \lVert x - \hat x \rVert_1 + D_\psi(\hat x) + C_\omega(\hat z)\Big].

Why plain scores rather than logarithms of probabilities? On the generator side Rosca et al. use −ln⁡σ(s)+ln⁡(1−σ(s))-\ln\sigma(s) + \ln\big(1 - \sigma(s)\big), the sum of the non-saturating and the minimax losses. Since 1−σ(s)=e−sσ(s)1 - \sigma(s) = e^{-s}\sigma(s), the sum is exactly −s-s, minus the logit. Its gradient with respect to the logit is −1-1 for every ss, so it never saturates. The minimax part alone has gradient −σ(s)-\sigma(s), which vanishes when the fakes are confidently rejected.

Why all three terms are needed

Each network minimizes its own loss while the others stay fixed. With z^=Eφ(x)\hat z = E_\varphi(x) and x^=Gθ(z^)\hat x = G_\theta(\hat z):

LE=Ex[λ∥x−x^∥1−Cω(z^)],LG=Ex[λ∥x−x^∥1−Dψ(x^)]−Ez Dψ(Gθ(z)),LD=−Ex[ln⁡σ(Dψ(x))+ln⁡(1−σ(Dψ(x^)))]−Ezln⁡(1−σ(Dψ(Gθ(z)))),LC=−Exln⁡(1−σ(Cω(z^)))−Ezln⁡σ(Cω(z)).\begin{aligned} \mathcal L_E &= \mathbb E_{x}\big[\lambda \lVert x - \hat x \rVert_1 - C_\omega(\hat z)\big],\\ \mathcal L_G &= \mathbb E_{x}\big[\lambda \lVert x - \hat x \rVert_1 - D_\psi(\hat x)\big] - \mathbb E_{z}\, D_\psi\big(G_\theta(z)\big),\\ \mathcal L_D &= -\mathbb E_{x}\Big[\ln \sigma\big(D_\psi(x)\big) + \ln\big(1 - \sigma(D_\psi(\hat x))\big)\Big] - \mathbb E_{z} \ln\big(1 - \sigma(D_\psi(G_\theta(z)))\big),\\ \mathcal L_C &= -\mathbb E_{x} \ln\big(1 - \sigma(C_\omega(\hat z))\big) - \mathbb E_{z} \ln \sigma\big(C_\omega(z)\big). \end{aligned}

The image discriminator receives samples Gθ(z)G_\theta(z), z∼p(z)z \sim p(z), as fakes next to the reconstructions. So samples are pushed toward the data, not only reconstructions.

Why are all three parts needed? Suppose two conditions hold exactly: the codes are distributed as the prior, qφ=pq_\varphi = p, and every image is rebuilt, Gθ(Eφ(x))=xG_\theta(E_\varphi(x)) = x. Take z∼p(z)z \sim p(z). Then zz and Eφ(x)E_\varphi(x) have the same distribution, and the same GθG_\theta is applied to both, so Gθ(z)G_\theta(z) is distributed as Gθ(Eφ(x))G_\theta(E_\varphi(x)). That is xx itself, distributed as PP. The samples are distributed as the data.

Each term guards one condition. The code discriminator enforces the first. Without it the codes could sit anywhere in Rd\mathbb R^d, and prior samples would land where GθG_\theta never learned anything, the failure of a plain autoencoder (see Autoencoders). The reconstruction term enforces the second, and it is what discourages mode collapse: a generator that dropped a mode could not rebuild that mode's images, and its L1L_1 loss would grow. Both conditions hold only approximately, so the image discriminator checks the samples directly. By judging reconstructions as well, it pushes GθG_\theta toward a sharp image near xx instead of the blurry per-pixel optimum.

On CelebA and CIFAR-10, α-GAN was competitive with DCGAN and WGAN-GP, but not uniformly better.

α-WGAN: two Wasserstein critics

The generator-side losses of α-GAN are already minus a score, which is the form of the WGAN generator loss. Only the training of the discriminators separates α-GAN from a Wasserstein version. Kwon, Han and Kim (2019) keep the encoder and generator losses and train both DψD_\psi and CωC_\omega as critics with the gradient penalty of Wasserstein GANs and Lipschitz constraints. With λGP\lambda_{\mathrm{GP}} the penalty weight, the three losses are

LD=ExDψ(x^)+EzDψ(Gθ(z))−2 ExDψ(x)+λGP GPD,LC=ExCω(z^)−EzCω(z)+λGP GPC,LEG=λ Ex∥x−x^∥1−ExDψ(x^)−EzDψ(Gθ(z))−ExCω(z^).\begin{aligned} \mathcal L_D &= \mathbb E_{x} D_\psi(\hat x) + \mathbb E_{z} D_\psi\big(G_\theta(z)\big) - 2\,\mathbb E_{x} D_\psi(x) + \lambda_{\mathrm{GP}}\,\mathrm{GP}_D,\\ \mathcal L_C &= \mathbb E_{x} C_\omega(\hat z) - \mathbb E_{z} C_\omega(z) + \lambda_{\mathrm{GP}}\,\mathrm{GP}_C,\\ \mathcal L_{EG} &= \lambda\,\mathbb E_{x} \lVert x - \hat x \rVert_1 - \mathbb E_{x} D_\psi(\hat x) - \mathbb E_{z} D_\psi\big(G_\theta(z)\big) - \mathbb E_{x} C_\omega(\hat z). \end{aligned}

Each penalty is the WGAN-GP one, taken at random points between a real input and a fake one: between an image and a reconstruction or sample for DψD_\psi, between a prior sample and a code for CωC_\omega. Kwon et al. use λ=λGP=10\lambda = \lambda_{\mathrm{GP}} = 10.

The image critic's loss is two WGAN comparisons sharing one critic, reconstructions against data and samples against data. Hence the factor 2 on the real term. The encoder and the generator are trained as one network on LEG\mathcal L_{EG}, and each term sends gradients to every network it depends on.

Why a Wasserstein critic for the codes too? Early in training, the codes of a deterministic encoder can sit far from the prior. A classifier then separates them easily, and an optimal one is constant on the codes: the case of non-overlapping supports from GANs as a two-player game, where the gradient vanishes. Kwon et al. made the change because their model, trained on 3D brain MRI volumes, was unstable with the classifier losses.

Each player gets its own optimizer over its own parameters: one for EφE_\varphi and GθG_\theta together, one for DψD_\psi, one for CωC_\omega. In the critics' steps, reconstructions, samples and codes enter as constants.

A code critic in one dimension

A one-dimensional example shows what each kind of code critic tells the encoder. Take d=1d = 1 and the prior p=N(0,1)p = \mathcal N(0, 1), and suppose every code is shifted by μ>0\mu > 0, so qφ=N(μ,1)q_\varphi = \mathcal N(\mu, 1).

The classifier's optimal score is the log ratio of the two densities:

C∗(z)=ln⁡p(z)qφ(z)=−z22+(z−μ)22=−μz+μ22.C^*(z) = \ln \frac{p(z)}{q_\varphi(z)} = -\frac{z^2}{2} + \frac{(z - \mu)^2}{2} = -\mu z + \frac{\mu^2}{2}.

With the classifier held fixed, the encoder minimizes −EqφC∗(z)-\mathbb E_{q_\varphi} C^*(z), a function of the shift μ\mu.

For the Wasserstein critic, take f(z)=−zf(z) = -z. It is 1-Lipschitz, and Epf−Eqφf=0+μ=μ\mathbb E_p f - \mathbb E_{q_\varphi} f = 0 + \mu = \mu. By the Kantorovich–Rubinstein duality, W1W_1 is the largest such difference, so W1≥μW_1 \ge \mu. Moving every point by μ\mu is a transport plan of cost μ\mu, so W1≤μW_1 \le \mu. Hence W1=μW_1 = \mu, and ff is an optimal critic. Its gradient has norm 1 everywhere, so GPC=0\mathrm{GP}_C = 0.

With this critic held fixed, the encoder's loss is −Eqφf=μ-\mathbb E_{q_\varphi} f = \mu. Its derivative with respect to the shift is 1, whatever the shift.

Both critics point the codes toward the prior. The question is how hard each one pushes as the codes get close.

Interpolation: lerp or slerp

An encoder lets us walk between two real images. Encode them, z0=Eφ(x0)z_0 = E_\varphi(x_0) and z1=Eφ(x1)z_1 = E_\varphi(x_1), move along a path ztz_t and decode Gθ(zt)G_\theta(z_t). The simplest path is linear interpolation (lerp), zt=(1−t) z0+t z1z_t = (1 - t)\,z_0 + t\,z_1 for t∈[0,1]t \in [0, 1].

Spherical linear interpolation (slerp) follows a great circle instead (Shoemake, 1985; White, 2016). With Ω\Omega the angle between z0z_0 and z1z_1,

cos⁡Ω=⟨z0,z1⟩∥z0∥ ∥z1∥,zt=sin⁡((1−t) Ω)sin⁡Ω z0+sin⁡(t Ω)sin⁡Ω z1.\cos\Omega = \frac{\langle z_0, z_1 \rangle}{\lVert z_0 \rVert\,\lVert z_1 \rVert}, \qquad z_t = \frac{\sin\big((1 - t)\,\Omega\big)}{\sin\Omega}\, z_0 + \frac{\sin(t\,\Omega)}{\sin\Omega}\, z_1 .

For two vectors of equal length rr, the point ztz_t turns from z0z_0 toward z1z_1 by the angle tΩt\Omega and keeps the length rr.

Why does that matter? In high dimension, samples of N(0,Id)\mathcal N(0, I_d) lie in a thin shell. ∥z∥2\lVert z \rVert^2 is a sum of dd squared standard normals, with mean dd and variance 2d2d, so the length is close to d\sqrt d, with a spread of about 1/2≈0.711/\sqrt 2 \approx 0.71. Two independent samples are nearly orthogonal: their cosine has standard deviation 1/d1/\sqrt d. For d=128d = 128, a sample has length 11.3±0.711.3 \pm 0.7, while the lerp midpoint (z0+z1)/2(z_0 + z_1)/2 of two samples has length about 8.08.0. That is 4.7 spreads inside the shell, a region that training on z∼p(z)z \sim p(z) almost never visits.

High density is not the same as typical. The log-density of N(0,I)\mathcal N(0, I) is −∥z∥2/2-\lVert z \rVert^2/2 plus a constant, so the midpoint is 32 nats more probable per unit volume than a typical sample. Yet points of that length almost never occur, because there is little volume near the centre. A generator learns what the prior actually produces.

In two dimensions, take z0=(3,0)z_0 = (3, 0) and z1=(0,3)z_1 = (0, 3). The lerp midpoint (1.5,1.5)(1.5, 1.5) has length 2.122.12. Slerp has Ω=90°\Omega = 90°, both coefficients at t=12t = \tfrac12 equal sin⁡45°\sin 45°, and the midpoint (2.12,2.12)(2.12, 2.12) has length 3.

For an autoencoding GAN the endpoints are codes of real images. If the code critic has matched qφ(z)q_\varphi(z) to the prior, the same argument applies. But slerp keeps only the length typical. It does not steer the path around holes in qφ(z)q_\varphi(z), which White calls dead zones.

Where it breaks, and what comes next

Rosca et al. found every model they tested, α-GAN and the baselines alike, sensitive to the architectures: minor changes led to catastrophic mode collapse. An autoencoding GAN has four networks, three optimizers, the weights λ\lambda and λGP\lambda_{\mathrm{GP}} and the update ratios. A large λ\lambda brings back the blur of the pixel loss; a small one weakens the guard against mode collapse.

The code size shows the tension concretely. Suppose the images lie on a surface described by mm coordinates, and a deterministic encoder is Lipschitz in them. If d>md > m, its codes form a set of zero volume in Rd\mathbb R^d, and the prior gives that set probability zero. The supports of qφ(z)q_\varphi(z) and p(z)p(z) then do not overlap, and prior samples land off the surface of codes, where GθG_\theta never learned anything. The AAE paper ties the code size of a deterministic encoder to the intrinsic dimension of the data, 5 to 8 for MNIST. For large codes it recommends a stochastic encoder, whose noise fills the missing directions. The code cannot be too small either: if two images share a code, GθG_\theta can return only one of them, and the reconstruction term grows. Only held-out reconstructions and samples tell where the workable range lies.

WGAN-GP AAE α-GAN α-WGAN
Networks GG, critic EE, GG, CC EE, GG, DD, CC EE, GG, DD, CC
Codes matched to the prior by — classifier classifier WGAN-GP critic
Pixel term none squared error L1L_1 L1L_1
Image adversary sees samples — reconstructions and samples reconstructions and samples
Can encode real images no yes yes yes

α-GAN matches images to their reconstructions point by point, but codes only in distribution. Rosca et al. note that adding a pointwise code reconstruction ∥z−Eφ(Gθ(z))∥\lVert z - E_\varphi(G_\theta(z)) \rVert would make it resemble CycleGAN. The next extension, CycleGAN: unpaired image-to-image translation, replaces the code space with a second image domain: two generators translate between domains without paired examples, and a cycle of reconstructions ties every output to its input.

The main line goes the other way. Here the encoder only approximately inverts the generator, and there is still no likelihood. The change of variables makes the encoder the exact inverse of the generator, with the same dimension in and out, and gets the likelihood exactly: normalizing flows.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.