Generative Adversarial Network
Generative Adversarial Network
Source
Introduction
• A Generative Adversarial Network or GAN is defined as the technique of generative
modeling used to generate new data sets based on training data sets. The newly generated
data set appears similar to the training data sets.
• GANs mainly contain two neural networks capable of capturing, copying, and analyzing the
variations in a dataset. These two neural networks are known as generators and
discriminators, and both of them compete with each other.
• The term Generative Adversarial Network consists of three words, and each of them has its
separate meaning, which is as follows:
• Generative: It is used to learn a generative model that visually explains how data is
generated.
• Adversarial: The word adversarial refers to setting one thing up against another. This
means that, in the context of GANs, the generative result is compared with the actual
images in the data set. A mechanism known as a discriminator is used to apply a model that
attempts to distinguish between real and fake images.
• Networks: It uses deep neural networks to train models, hence called networks.
Types of GANs
• Vanilla GAN
• This is the simplest type of GAN. Here, the generator and the discriminator are simple, basic multi-
layer perceptrons that try to optimize the mathematical equation using stochastic gradient descent.
• CGAN can be described as a deep learning method in which some conditional parameters are put
into place.
• DCGAN is one of the most popular and also the most successful implementations of GAN. It is
composed of ConvNets in place of multi-layer perceptrons.
• SRGAN as the name suggests is a way of designing a GAN in which a deep neural network is used
along with an adversarial network in order to produce higher-resolution images.
Components of GAN
Components of GAN
• GANs comprise two neural networks, a discriminator and a generator. They use adversarial
training to produce artificial data identical to actual data.
• Generator: The generator is an unsupervised machine learning method that generates fake
samples based on actual training data sets. It is also a neural network with hidden layers,
activation, and loss function.
As the name suggests, the generator generates a fake output of unseen data based on training
data sets, making the discriminator fool to understand this fake data as accurate.
Further, the discriminator acted as a cop and used to discriminate the training data and
generated data, identify the abnormalities in the samples created by the generator and classify
them as fake or genuine.
Components of GAN
• However, this entire process continues till the generator wins and ultimately makes a fool of
the discriminator on fake data and once this is achieved generalized GAN model is created.
• Realistic, high-quality samples are produced as a result of this competitive interaction, which
drives both networks toward advancement.
• GANs are proving to be highly versatile artificial intelligence tools, as evidenced by their
extensive use in image synthesis, style transfer, and text-to-image synthesis.
How does a GAN work?
Source
How does a GAN work?
• Initialization: Two neural networks are created: a Generator (G) and a Discriminator (D).
• G is tasked with creating new data, like images or text, that closely resembles real data.
• D acts as a critic, trying to distinguish between real data (from a training dataset) and the data
generated by G.
• Generator’s First Move: G takes a random noise vector as input. This noise vector contains
random values and acts as the starting point for G’s creation process. Using its internal layers
and learned patterns, G transforms the noise vector into a new data sample, like a generated
image.
How does a GAN work?
• Discriminator’s Turn: D receives two kinds of inputs:
• The data samples generated by G in the previous step. D’s job is to analyze each input and
determine whether it’s real data or something G cooked up. It outputs a probability score between 0
and 1. A score of 1 indicates the data is likely real, and 0 suggests it’s fake.
• If D correctly identifies real data as real (score close to 1) and generated data as fake (score close to
0), both G and D are rewarded to a small degree. This is because they’re both doing their jobs well.
• When D mistakenly labels G’s creation as real (score close to 1), it’s a sign that G is on the
right track. In this case, G receives a significant positive update, while D receives a penalty
for being fooled.
• This feedback helps G improve its generation process to create more realistic data.
• Discriminator’s Adaptation:
• Conversely, if D correctly identifies G’s fake data (score close to 0), but G receives no
reward, D is further strengthened in its discrimination abilities.
• This ongoing duel between G and D refines both networks over time.
GAN Algorithm
Loss Functions
Loss Functions
Generator Loss
• The objective of the generator in a GAN is to produce synthetic samples that are realistic enough to fool
the discriminator. The generator achieves this by minimizing its loss function JG
• The loss is minimized when the log probability is maximized, i.e., when the discriminator is highly likely
to classify the generated samples as real. The following equation is given below:
m
1
m∑
JG = − logD(G(zi)),
1
• where,
• logD(G(zi)) represents log probability of the discriminator being correct for generated samples.
• The generator aims to minimize this loss, encouraging the production of samples that the
discriminator classifies as real (logD(G(zi))), closes to 1.
Discriminator Loss
• The discriminator reduces the negative log-likelihood of correctly classifying both produced
and real samples. This loss incentivizes the discriminator to accurately categorize generated
samples as fake and real samples with the following equation:
m m
1 1
m∑ ∑
JD = JG = − logD(xi) − log(1 − D(G(zi)))
1
m 1
• JD assesses the discriminator’s ability to discern between produced and actual samples.
• The log likelihood that the discriminator will accurately categorize real data is represented by
logD(xi).
• The log chance that the discriminator would correctly categorize generated samples as fake is
represented by log(1 − D(G(zi)))
The discriminator aims to reduce this loss by accurately identifying artificial and real samples.
Example
• In a GAN model, the generator G(x) and the discriminator D(x) are playing a minimax game where
the generator tries to minimize the loss and the discriminator tries to maximize it. Assume the
following values during a training step:
The discriminator is trained to maximize the probability of correctly classifying both real and
generated data.
• For real data x, the discriminator outputs a score of 0.8 (ie D(x)=0.8)
• For generated data G(z), the discriminator outputs a score of 0.3 (ie D(G(z)) = 0.3)
Calculate: