Generative models

Lecture 8 · Machine Learning for ICE · University of Klagenfurt, 2023

Nunzio Alexandro Letizia

This lecture

Lecture 8 of 8

  1. What generative models do
  2. Variational autoencoders
  3. Generative adversarial networks, and how they can learn a channel

From Machine Learning for ICE at the University of Klagenfurt, first taught in 2022: this is the 2023 edition, so some of it may be a little dated. In particular, diffusion models, today the leading generative models for images, are not covered here. All lectures.

Generative models

01

What generative models do

A generative model estimates

  • in the unsupervised setting, the data distribution p(x), from the collected samples x_i, i=1,\dots,N
  • in the supervised setting, the joint distribution of data and targets p(x,y), from the pairs (x_i,y_i), i=1,\dots,N

The estimate can be explicit, with a parametric expression for the density, or implicit, with no expression at all, only a way to sample from it.

In practice, generative models are used to generate new data from the estimated distribution.

A histogram of collected samples with two humps, the estimated density over it, and a few new samples drawn from the estimate.

Variational autoencoders

02

Variational autoencoders

Variational autoencoders (Kingma and Welling, 2013) are based on variational Bayesian methods. An autoencoder architecture, read in probabilistic terms:

Input data x\sim p(x) Encoder Latent variable z\sim q_x(z)\approx p(z\mid x) Decoder Generated data \hat x\sim p(x\mid z)

  • The encoder maps the input x to a compressed latent variable z: it estimates the posterior p(z\mid x), the distribution of the code given the observation.
  • The decoder maps z back to the data space: it estimates the likelihood p(x\mid z), the distribution of the data given the code.

To generate new data: sample a latent variable from the prior p(z), and pass it through the decoder to get \hat x.

The VAE objective

Approximate the posterior with a Gaussian, p(z\mid x)\approx q_x(z)=\mathcal{N}\big(g(x),h(x)\big), with g and h to be found, and the likelihood with another Gaussian, p(x\mid z)=\mathcal{N}\big(f(z),c\mathbf{I}\big)\propto e^{-\frac{1}{2c}\lVert x-f(z)\rVert^2}. The prior p(z) is a standard normal.

The best approximation minimizes the KL divergence to the true posterior. With Bayes, p(z\mid x)=p(x\mid z)\,p(z)/p(x), and \log p(x) does not depend on g,h,f:

\begin{aligned} (g^*,h^*,f^*)&=\arg\min_{g,h,f}D_{\mathrm{KL}}\big(q_x(z)\,\|\,p(z\mid x)\big)=\arg\min_{g,h,f}\mathbb{E}_{z\sim q_x(z)}\Big[\log\frac{q_x(z)}{p(z\mid x)}\Big]\\ &=\arg\min_{g,h,f}\mathbb{E}_{z\sim q_x(z)}\Big[\log\frac{q_x(z)}{p(z)}\Big]-\mathbb{E}_{z\sim q_x(z)}\big[\log p(x\mid z)\big]\\ &=\arg\min_{g,h,f}\underbrace{\frac{1}{2c}\,\mathbb{E}_{z\sim q_x(z)}\big[\lVert x-f(z)\rVert^2\big]}_{\text{reconstruction (decoder) loss}}+\underbrace{D_{\mathrm{KL}}\big(q_x(z)\,\|\,p(z)\big)}_{\text{regularization}} \end{aligned}

A trade-off: reconstruct the input well, while keeping the codes close to the prior, so that sampling from the prior produces sensible data. The constant c sets the balance.

Generative adversarial networks

03

Generative adversarial networks

Generative adversarial networks (Goodfellow et al., 2014) are two networks in competition:

  • a generator, which produces new data \hat x meant to follow the distribution p(x) of the real data
  • a discriminator, which tells whether a sample is real data x or generated data \hat x

The generator wins when the discriminator can no longer tell its output from the real data.

Noise z\sim p_z(z) Generator G G(z) Discriminator D real data x also comes in real or fake? D(\cdot)\in(0,1)

The min-max game

Generator and discriminator play the min-max game

\min_G\max_D V(D,G)=\mathbb{E}_{x\sim p_{\text{data}}(x)}\big[\log D(x)\big]+\mathbb{E}_{z\sim p_z(z)}\big[\log\big(1-D(G(z))\big)\big]

The discriminator wants D(x) close to 1 on real data and close to 0 on generated data. The generator wants to fool it.

Along the way, the generator implicitly builds a distribution p_{\text{gen}}(x), which should get closer and closer to the real p_{\text{data}}(x).

How training works

Start from the inner maximization. For a fixed generator G, which D maximizes V(D,G)? Taking the derivative with respect to D(x), point by point:

D^*(x)=\frac{p_{\text{data}}(x)}{p_{\text{data}}(x)+p_{\text{gen}}(x)}

Now the generator, against the optimal discriminator:

\min_GV(D^*,G)=\mathbb{E}_{x\sim p_{\text{data}}(x)}\big[\log D^*(x)\big]+\mathbb{E}_{z\sim p_z(z)}\big[\log\big(1-D^*(G(z))\big)\big]

The generator controls p_{\text{gen}}(x). One can show that the minimum is reached when p_{\text{gen}}(x)=p_{\text{data}}(x), and that the value of the game is then -\log4. So at convergence

D^*(x)=\frac12

the discriminator is guessing at random between real and generated data.

The game, in a picture

Three panels: the generated density starts far from the data density, moves towards it, and finally coincides with it, while the optimal discriminator flattens to one half.

At the start the discriminator separates the two distributions easily. Each generator step moves p_{\text{gen}} where D says “real”, until the two coincide and D^*(x)=\frac12 everywhere.

GANs for communications

In communications, GANs are an excellent tool to model the medium.

  • From a dataset of channel and noise measurements, a GAN can learn to synthesize new channel and noise samples.
  • The advantage: the channel gets a closed form, a neural network, which can be used in the end-to-end training of a communication system (lecture 7).
  • Depending on the channel (linear or not, time-invariant or not) and on the noise (stationary, cyclostationary, non-stationary), different architectures fit.

For example, a generator can learn the noise measured on power line cables, which is far from Gaussian and changes with the mains cycle.

Assignment 3: a channel GAN

Consider a 4-QAM scheme, with symbols x_k in the constellation. Simulate a transmission over an AWGN channel and generate the “real” received samples y_k=x_k+n_k.

  1. Design a conditional GAN that takes x_k as input and produces y_k.
  2. Motivate the choice of the generator and discriminator architectures.
  3. Plot the real and the generated samples. How do they differ?
  4. Comment on the training progress.

Assignment 3: the solution

x = Input(shape=(2,))             # the symbol, the condition
z = Input(shape=(2,))             # the randomness
y = self.generator(Concatenate()([x, z]))   # 100 relu, 100 sigmoid, 2
self.discriminator.trainable = False
self.combined = Model([x, z], self.discriminator(y))

for epoch in range(epochs):
    data_tx = 2*np.random.randint(2, size=(batch_size, 2)) - 1
    z_prior = np.random.uniform(-1, 1, size=(batch_size, 2))
    data_rx = data_tx + 0.1*np.random.normal(0, 1, (batch_size, 2))
    fake_rx = self.generator.predict(np.concatenate([data_tx, z_prior], axis=1))
    # D learns real from fake, then G learns to fool it
    self.discriminator.train_on_batch(data_rx, valid)
    self.discriminator.train_on_batch(fake_rx, fake)
    self.combined.train_on_batch([data_tx, z_prior], valid)

The four 4-QAM symbols at plus or minus one on each axis, each surrounded by a cloud of received samples.

the real samples the GAN must imitate

The condition x_k picks which cloud to draw, the noise z where in it. Once trained, the generator is a differentiable channel for the autoencoder of lecture 7.

After H. Ye, G. Y. Li, B.-H. F. Juang, K. Sivanesan, Channel agnostic end-to-end learning based communication systems with conditional GAN, 2018.

This was the last lecture. All lectures.