Understanding Generative Adversarial Networks

Image for post
Image for post

Background

GAN (Generative Adversarial Network) is a framework proposed by Ian Goodfellow, Yoshua Bengio and others in 2014.

Image for post
Image for post
MNIST hand-written digit images

The Generator

The input to the generator is a series of randomly generated numbers called latent sample. Once trained, the generator can produce digit images from latent samples.

Image for post
Image for post
generator = Sequential([
Dense(128, input_shape=(100,)),
LeakyReLU(alpha=0.01),
Dense(784),
Activation('tanh')
], name='generator')
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 128) 12928
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU) (None, 128) 0
_________________________________________________________________
dense_2 (Dense) (None, 784) 101136
_________________________________________________________________
activation_1 (Activation) (None, 784) 0
=================================================================
Total params: 114,064
Trainable params: 114,064
Non-trainable params: 0
_________________________________________________________________

How to Train the Generator?

Without training, the generator produces garbage images only.

Image for post
Image for post

The Discriminator

The discriminator is a classifier trained using the supervised learning. It classifies whether an image is real (1) or not (0).

Image for post
Image for post
Image for post
Image for post
discriminator = Sequential([
Dense(128, input_shape=(784,)),
LeakyReLU(alpha=0.01),
Dense(1),
Activation('sigmoid')
], name='discriminator')
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_3 (Dense) (None, 128) 100480
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU) (None, 128) 0
_________________________________________________________________
dense_4 (Dense) (None, 1) 129
_________________________________________________________________
activation_2 (Activation) (None, 1) 0
=================================================================
Total params: 100,609
Trainable params: 100,609
Non-trainable params: 0
_________________________________________________________________

The GAN

We connect the generator and the discriminator to produce a GAN.

Image for post
Image for post
gan = Sequential([
generator,
discriminator
])
> gan.summary()Layer (type)                 Output Shape              Param #   
=================================================================
generator (Sequential) (None, 784) 114064
_________________________________________________________________
discriminator (Sequential) (None, 1) 100609
=================================================================
Total params: 214,673
Trainable params: 214,673
Non-trainable params: 0
_________________________________________________________________

Training the GAN means Training the Generator

When we feed a latent sample to the GAN, the generator internally produces a digit image which is then passed to the discriminator for classification. If the generator does a good job, the discriminator returns a value close to 1 (high probability of the image being real).

Image for post
Image for post

The Train Loop

Let’s not forget that we also need to train the discriminator as well so that it can do a good job as a classifier of real and fake images.

Image for post
Image for post
The Discriminator Training
Image for post
Image for post
The Generator Training (via GAN)

But does it actually work?

The result of the simple GAN is not outstanding. Some of them look pretty good but others are not.

Image for post
Image for post
Image for post
Image for post

References

[1] Generative Adversarial Networks

Ian J. Goodfellow, Jean Pouget-Abadie, Mehdi Mirza, Bing Xu, David Warde-Farley, Sherjil Ozair, Aaron Courville, Yoshua Bengio

[2] GAN MNIST Example in TensorFlow

Udacity

[3] MNIST dataset

Yann LeCun

[4] How to Train a GAN? Tips and tricks to make GANs work

Facebook AI Research: Soumith Chintala, Emily Denton, Martin Arjovsky, Michael Mathieu

[5] Generative Models

Agustinus Kristiadi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store