GANs — A Brief Introduction to Generative Adversarial Networks

Shweta Goyal
Analytics Vidhya
Published in
4 min readJun 2, 2019

Have you ever thought of converting your images of zebras to horses or vice-versa? Have you ever thought of predicting the next frame of a video? What if you can generate an image through a text? Do you want to generate emojis through pictures? Do you want to create an image in different poses or different angles? What if you can create an image of the day to night or vice-versa?

Yes, you can do all these things. These are some of the coolest applications that fascinated me and inspired me to write this article.

Generative Adversarial Networks (GANs) is one of the most popular topics in Deep Learning. Generative Adversarial Networks or GANs are types of Neural Networks used for Unsupervised learning. There has been a tremendous increase in applications of GANs. Let’s understand what is generative adversarial networks and then we will see how does it work.

Introduction

To understand Generative Adversarial Networks better, let’s break it down into individual words. So, the first word is Generative which means that there is a network that is constantly generating new data and the second word is Adversarial which means that it involves two networks opposing each other and Network simply means an order of data that is going ahead and generate new data.

How does it work?

GANs consists of two networks, a Generator G(x), and a Discriminator D(x). They both play an adversarial game where the generator tries to fool the discriminator by generating data similar to those in the training set. The Discriminator tries not to be fooled by identifying fake data from real data. They both work simultaneously to learn and train complex data like audio, video or image files.

Generative Adversarial Network

The generator model generates images from random noise(z) and then learns how to generate realistic images. Random noise which is input is sampled using uniform or normal distribution and then it is fed into the generator which generates an image. The generator output which are fake images and the real images from the training set is fed into the discriminator that learns how to differentiate fake images from real images. The output D(x) is the probability that the input is real. If the input is real, D(x) would be 1 and if it is generated, D(x) should be 0.

The math behind the GANs

Let’s dig a little deeper and understand how it works mathematically. The Discriminator and Generator play a two-player minimax game with the value function V(G, D). So, Minimax Objective function is:

Objective Function
Architecture of GANs

D() gives us the probability that the given sample is from training data X. For the Generator, we want to minimize log(1-D(G(z)) i.e. when the value of D(G(z)) is high then D will assume that G(z) is nothing but X and this makes 1-D(G(z)) very low and we want to minimize it which this even lower. For the Discriminator, we want to maximize D(X) and (1-D(G(z))). So the optimal state of D will be P(x)=0.5. However, we want to train the generator G such that it will produce the results for the discriminator D so that D won’t be able to distinguish between z and X.

Now the question is why this is a minimax function. Here, the Discriminator tries to maximize the objective which is V while the Generator tries to minimize it, due to this minimizing/maximizing we get the minimax term. They both learn together by alternating gradient descent.

How to make this work?

We will perform the iteration of gradient descent on D using real and generated images by fixing G. Then we fix D and train G for another single iteration to fool a fixed D. We want to optimize our minimax function by iterating both G and D in alternating steps until we get good quality images from the generator and discriminator won’t be able to differentiate between real and fake images.

Below is the pseudo-code which shows how GANs are trained.

Conclusion

GANs are very popular and widely used across various industries in a wide variety of problems. It seems easy to train them but actually, it’s not as it requires two networks to train which makes them unstable.

This is a simple article which gives you a basic idea of GANs. If you want to know more about it, below are the references which you can read.

I hope you find this article useful. I have implemented GANs using PyTorch. Do check out this repo.

https://github.com/Shwetago/Generative-model-using-PyTorch

Thank you for reading till here. Stay tuned for more articles.

--

--