An Introduction to Generative Adversarial Networks

Hiran Hasanka
6 min readMay 22, 2021

--

Photo by Pawel Czerwinski on Unsplash

Generative Adversarial Network(GAN) is a different kind of deep learning method that is used for generating new data that looks like data from the dataset it was trained on.

A GAN is a combination of two distinct neural networks in a contest with each other. One of those is the generator, trained to generate new examples and the other is the discriminator, which is trained to classify whether examples are real or generated(fake).

GANs were first introduced in the paper “Generative Adversarial Nets” written by Ian J. Goodfellow et al. back in 2014. Since then, GANs have come a long way. In 2017, first faces were generated using a GAN and first CANs(Creative Adversarial Networks) that can generate original abstract paintings were created. An year after that, a GAN was able to create the painting “Edmon de Belamy” which was sold for $432,500. In 2019, a group of researchers at Samsung created a system to produce videos of a person talking given only a single photo of that person. Last year, a group of researchers at Nvidia have taught a GAN-based system to recreate the game “Pac-Man” simply by watching the game being played.

Edmond de Belamy (source)

Now that we’ve put the brief history lesson out of the way, let me explain two main parts of a GAN, one by one.

The Generator

The generator model is trained to create fake data with the help of feedback from the discriminator. Goal of this model is to make the discriminator to classify outputs of the generator model as real data. Generator takes in random data (most probably taken from a uniform or a Gaussian distribution) as inputs and tries to create meaningful data from it.

The Discriminator

The discriminator is a simple classifier model. The goal of this model is to distinguish real data from the fake data created by generator model. Takes in real data as positive examples and fake data created by the generator as negative examples in the training period.

Architecture

The two models mentioned above are always in a never ending tug-of-war between each other. The generator tries to fool the discriminator by using its feedback and the discriminator is trying to call generator’s bluff by using outputs of the generator to train itself. The following graph describes how these two models are placed inside a GAN.

Structure of GAN (source)

Training a GAN

Since a GAN consists of two models, it should go through two training processes. Not only does it complicate things in training a GAN, but also makes it harder to identify convergence. In order to overcome these complications, GANs are trained in alternating periods. First, the discriminator model is trained for one or more epochs, then the generator model is trained for one or more epochs. Then this process is repeated to continue training the generator and discriminator models. This continuous training will result in a very advanced GAN model which will output results that looks like real data to even humans.

For an example, if you visit https://thispersondoesnotexist.com/ you’ll see pictures of people. The shocker is that every image you’ll see will be fake. They are images of people that does not exist, just people ‘imagined’ by a GAN. This example alone will convince you how advanced these GANs can be, to create data out of thin air that are capable of fooling even humans.

An image of a person, imagined by a GAN. (source)

Now let’s take a look at GAN training process from the perspective of the discriminator and the generator.

Training the Discriminator

Training the discriminator mostly resembles to training a normal classification model. The model is trained to classify whether its inputs are real or fake. Real images are used as positive cases and images from generator are used as negative cases. The generator is kept constant during the training process of the discriminator. Of course, the generator model will be creating data during this process, but any of the weights in the generator will not be changed while training the discriminator.

The discriminator is connected to two loss functions; discriminator loss and generator loss. In the training process of the discriminator, only the discriminator loss is used and the generator loss is ignored. During the training, discriminator updates its weights using back-propagation.

Training the discriminator (source)

Training the Generator

Generator is not directly connected to the loss that should be affected, so the generator is using the discriminator model to train itself. The discriminator is kept constant while the generator model is training. The discriminator is only used to classification and none of its weight will be updated during the training of generator. The Generator model is penalized by the generator loss for creating data that the discriminator identify as fake. The generator feeds data into the discriminator, and the discriminator produces the result(whether its real or fake) that should be affected by the training. Weights are updated using back-propagation. Since weights on generative models depend on weights of discriminator model, back-propagation process is flown back to the generator model through the discriminator model.

Training the generator (source)

Loss Function

There are a handful of loss functions that can be used in a GAN. Since this is a brief introduction, I’ll only mention Minimax, the loss function that was proposed in “Generative Adversarial Nets”, the paper that introduced GANs to the world.

Minimax Loss Function (source)

If you look closely, you’ll notice a resemblance in this function to the loss function of a classification model. However, the generator is trying to minimize this loss function while the discriminator is trying to maximize it during training.

With that, my brief introduction to Generative Adversarial Networks comes to an end. What you should notice is that this is an area of deep learning that has many active researches ongoing and there are many interesting attempts to use GANs in very different tasks. If you’re interested in learning more about GANs, I suggest you to use following resources listed below.

Further Reading

References

--

--

Hiran Hasanka
Hiran Hasanka

No responses yet