What are Generative Adversarial Networks?
What is a Generator?
What is a Discriminator?
How Do GANs Work?Steps for Training GAN
Deep Learning has found numerous applications in the
industries. Neural networks are being widely used in almost
every business sector, such as healthcare, finance, and e-
commerce. These neural networks help solve business problems.
What are Generative Adversarial Networks?
Generative Adversarial Networks (GANs) were introduced in
2014 by Ian J. Goodfellow and co-authors. GANs perform
unsupervised learning tasks in machine learning. It consists of 2
models that automatically discover and learn the patterns in
input data.
The two models are known as Generator and Discriminator.
They compete with each other to scrutinize, capture, and
replicate the variations within a dataset. GANs can be used to
generate new examples that plausibly could have been drawn
from the original dataset.
Shown below is an example of a GAN. There is a database that
has real 100 rupee notes. The generator neural network
generates fake 100 rupee notes. The discriminator network will
help identify the real and fake notes.
What is a Generator?
A Generator in GANs is a neural network that creates
fake data to be trained on the discriminator. It learns to
generate plausible data. The generated
examples/instances become negative training examples
for the discriminator. It takes a fixed-length random
vector carrying noise as input and generates a sample.
The main aim of the Generator is to make the
discriminator classify its output as real. The part of the
GAN that trains the Generator includes:
● noisy input vector
● generator network, which transforms the random
input into a data instance
● discriminator network, which classifies the
generated data
● generator loss, which penalizes the Generator for
failing to dolt the discriminator
The backpropagation method is used to adjust each
weight in the right direction by calculating the weight's
impact on the output. It is also used to obtain gradients
and these gradients can help change the generator
weights.
What is a Discriminator?
The Discriminator is a neural network that identifies real data
from the fake data created by the Generator. The discriminator's
training data comes from different tgwo sources:
● The real data instances, such as real pictures of birds,
humans, currency notes, etc., are used by the
Discriminator as positive samples during training.
● The fake data instances created by the Generator are
used as negative examples during the training process.
While training the discriminator, it connects to two loss
functions. During discriminator training, the discriminator
ignores the generator loss and just uses the discriminator loss.
In the process of training the discriminator, the discriminator
classifies both real data and fake data from the generator. The
discriminator loss penalizes the discriminator for misclassifying a
real data instance as fake or a fake data instance as real.
The discriminator updates its weights through backpropagation
from the discriminator loss through the discriminator network.
How Do GANs Work?
GANs consist of two neural networks. There is a Generator G(x)
and a Discriminator D(x). Both of them play an adversarial game.
The generator's aim is to fool the discriminator by producing
data that are similar to those in the training set. The
discriminator will try not to be fooled by identifying fake data
from real data. Both of them work simultaneously to learn and
train complex data like audio, video, or image files.
The Generator network takes a sample and generates a fake
sample of data. The Generator is trained to increase the
Discriminator network's probability of making mistakes.
Below is an example of a GAN trying to identify if the 100 rupee
notes are real or fake. So, first, a noise vector or the input vector
is fed to the Generator network. The generator creates fake 100
rupee notes. The real images of 100 rupee notes stored in a
database are passed to the discriminator along with the fake
notes. The Discriminator then identifies the notes as classifying
them as real or fake.
We train the model, calculate the loss function at the end of the
discriminator network, and backpropagate the loss into both
discriminator and generator models.
Mathematical Equation
The mathematical equation for training a GAN can be
represented as:
Here,
G = Generator
D = Discriminator
Pdata(x) = distribution of real data
p(z) = distribution of generator
x = sample from Pdata(x)
z = sample from P(z)
D(x) = Discriminator network
G(z) = Generator network
Steps for Training GAN
● Define the problem
● Choose the architecture of GAN
● Train discriminator on real data
● Generate fake inputs for the generator
● Train discriminator on fake data
● Train generator with the output of the discriminator
Training
For each training step, we start with the discriminator loop. We want to
repeat this step before moving to the generator loop.
Discriminator loop:
1. Set a loop k where k>1. We do this because we want to make sure
that the discriminator converges to a good estimator of original
data pd.
2. Sample m noise data from a normal distribution {z1,z2,z3,…zn}
and transform them through the generator.
3. Sample m real data from a normal distribution {x1,x2,x3,…xn}.
4. It’s important to remember that fake samples are labeled zero and
real samples are labeled one.
5. We then use the loss function to calculate loss using labels.
6. We take the gradient of the loss function with respect to the
discriminator parameters, and update the weights in the
discriminator. To update the gradient in the discriminator, we use
gradient ascend because we want to maximize it.
This completes the discriminator loop.
Generator loop:
The generator loop is similar. We start by:
1. Sample m noise data from a normal distribution {z1,z2,z3,…zn}
and transform them through the generator to get our fake samples.
2. Since we’re only interested in updating the generator’s loop, we
take in the gradient of the loss function with respect to the
generator, eventually turning the derivatives to zero.
3. Also, in the generator loop we won’t be working with the real
sample, so the cost function reduces to:
Using the same equation, we can now update the weights of the
generator using gradient descent.
It is quite intriguing that the generator evolves by keeping the
discriminator as a constant. The discriminator acts as a guide to help the
generator learn and evolve!
Loss functions
Two loss function dominate in gans:
1. Min-max loss
2. Wasserstein loss
Minimax loss
Minimax is all about maximizing error and minimizing the error. The min-
max loss was first described in a 2014 paper by Ian Goodfellow et al.,
titled “Generative Adversarial Networks”.
Minimax loss comes from game theory. It basically revolves around
players competing against each other. The idea is that to win, a player
must maximize their own probability of winning, and minimize it for the
opponents by finding the best moves the opponent can make.
Here, we need to understand that the discriminator is the player who
wants to maximize the probability of winning by correctly classifying fake
images the generator generates. It makes itself familiar with real images
first, i.e. D(x) = 1, and then fake images, i.e. D(G(x)) = 0.
The difference between (1 – D(G(x))) should increase. Larger difference
indicates that the discriminator is performing well; it’s able to classify real
and fake images.
On the other hand, when it comes to the generator, it will try to minimize
the winning probability of the discriminator by minimizing (1 – D(G(x))).
All the generator wants to do is to produce the sample that, when passed
through the discriminator (D(G(x)), is closer to 1. Then, when loss is
calculated (1 – D(G(x))), the output will be closer to zero.
This process keeps on going until one player helps the other to evolve, or
training iteration is terminated.
READ ALSO
Understanding GAN Loss Functions
Wasserstein loss
Wasserstein loss function was developed for a new type of GAN called
the WGAN, where the discriminator does not classify the output as fake
or real, but for each generated sample it outputs a number between not
between 0 and 1. Although the idea remains the same, where the real
samples are represented by a larger number and fake samples are
represented by a smaller number.
So, it can’t really discriminate between real and fake samples. The
WGAN discriminator is actually called a “critic”.
Critic Loss: C(x) – C(G(z))
The discriminator tries to maximize this function, it’s the same as the
minimax function we saw previously. It tries to maximize the difference
between the real instances and the fake instances.
Generator Loss: C(G(z))
The generator tries to maximize this function. In other words, it tries to
maximize the discriminator’s output for its fake instances.
Let us now look at the different types of GANs.
Vanilla GANs: Vanilla GANs have a min-max optimization
formulation where the Discriminator is a binary classifier and
uses sigmoid cross-entropy loss during optimization. The
Generator and the Discriminator in Vanilla GANs are multi-layer
perceptrons. The algorithm tries to optimize the mathematical
equation using stochastic gradient descent.
Deep Convolutional GANs (DCGANs): DCGANs support
convolution neural networks instead of vanilla neural networks at
both Discriminator and Generator. They are more stable and
generate better quality images. The Generator is a set of
convolution layers with fractional-strided convolutions or
transpose convolutions, so it up-samples the input image at every
convolutional layer. The discriminator is a set of convolution
layers with strided convolutions, so it down-samples the input
image at every convolution layer.
Conditional GANs: Vanilla GANs can be extended into
Conditional models by using extra-label information to generate
better results. In CGAN, an additional parameter ‘y’ is added to
the Generator for generating the corresponding data. Labels are
fed as input to the Discriminator to help distinguish the real data
from the fake generated data.
Super Resolution GANs: SRGANs use deep neural networks
along with an adversarial network to produce higher resolution
images. SRGANs generate a photorealistic high-resolution image
when given a low-resolution image.
Now that we have understood what are GANs, let’s look at some
of the important applications of GANs.
Application of GANs
● With the help of DCGANs, you can train images of cartoon
characters for generating faces of anime characters as
well as Pokemon characters.
● GANs can be trained on the images of humans to generate
realistic faces. The faces that you see below have been
generated using GANs and do not exist in reality.
● GANs can build realistic images from textual descriptions
of objects like birds, humans, and other animals. We input
a sentence and generate multiple images fitting the
description.
Conclusion
We have learned the basics of GANs and how it works. You
understood about generators and discriminators. You saw how
GANs can help identify real and fake images. You looked at the
training process of the Generative Adversarial Network and the
applications of GANs.
https://www.simplilearn.com/tutorials/deep-learning-tutorial/
generative-adversarial-networks-gans