# GAN

Give a GAN some training samples and it will gradually learn how to generate new data samples with the same characteristics as of the training samples. As an example such network is able to generate photographs of people who actually have never existed.

Or it can generate new anime characters:

Or even new designs for your bedroom:

The "magic" of GANs lies in its two main network components: a **Generator** and a **Discriminator**. Let's say we want to produce some new cat pictures showing cats who never existed. The Generator receives some input in form of real cat pictures with actually real cats and gives some output. In the very first steps those cat pictures will probably look nothing like a cat but with time our Generator will get better and produce more and more realistic cat pictures. How will it do this? Well, a Generator can improve itself by constantly receiving feedback from its most severe critic: the Discriminator. The Discriminator's task is to detect which data it receives, is real and which is fake. As everything that comes from the Generator, no matter how good or bad, is not real, the Discriminator will try to identify everything coming from the Generator as fake.

So, the Generator tries to fool the Discriminator and the Discriminator tries to not get fooled.

This is a very general idea of the GAN structure. But now let's dive deeper ...

##### Generative in GANs

The are two general classes in statistical modeling: *1. generative models* and

*2.*. A major difference between those is that a generative model is able to generate new photos of people, cats, cars, etc ... that look like they were real people, cats, cars, etc... On the other hand, a discriminative model is only able to distinguish one object from another, for example separate a cat from a dog. Along with Naive Bayes and HHM, GANs also belong to the family of generative models, as GANs

**discriminative models****generate**completely new outputs.

* generative probability -> joint probability *

*discriminative probability -> conditional probability*

A generative model is normally more complex to get to as it has not only to distinguish between objects but also find correlations like: if there is a car, then there must be a road. At the same time, a discriminative model only has to tell weather it is a car or a dog, hence a discriminative model draws a boundary line between two classes and a generative model directly locates those elements in the data space.

*generative model*

* discriminative model*

* Generative models are harder to work with as they try to model how data is placed in space specifically. *

##### Structure of GANs

A GAN consists of two crucial parts: a **Generator** and a **Discriminator**. Both of them are neural networks. The Generator network learns to generate realistic outputs. The Discriminator network learns to detect fake data which comes from the Generator. The Generator relies on the feedback from the Discriminator as the Generator needs to know weather what it produced was a good fake (=the Discriminator could not detect it as a fake) or what it produced was a bad fake (=the Discriminator could detect that it was a fake).

##### Discriminator

The Discriminator is a classifier neural network (e.g. CNN for image data). It receives two types of inputs: one is real data and the other one is the fake data produced by the Generator.

As we can see in the picture above, the Discriminator has two Loss functions. If the Discriminator is in training mode, it ignores the Generator Loss and only uses the Discriminator Loss. When the Discriminator trains, it:

- classifies data into real and fake
- passes the results to its Discriminator Loss
- the Discriminator Loss gives a penalty for wrong classifications
- the Discriminator adjusts its weights with backpropagation based on the Discriminator Loss results
- repeats from 1

##### Generator

A Generator is a neural network that produces new data instances. What data input should be, so that our Generator can output absolutely new data instances? We give **random noise** as input for the Generator. Why random noise? Well, the Generator has more freedom to combine features learned from random noise to create brand new samples as when we give already predefined data with existing features in it. With noise data, the Generator samples from different places in the target distribution and by doing so is able to output a broad variety of new data.

As well as the Discriminator, the Generator has its own training phase. In its training the Generator:

- preforms random noise sampling
- creates some output from sampled noise
- gives this output to the Discriminator
- receives the Discriminator feedback: "real" or "fake"
- computes Discriminator Loss from Discriminator classification
- backpropagates through the Discriminator and the Generator to acquire gradients
- changes the Generator weights by gradients

##### The Generator-Discriminator-Tandem

How does the training of both networks in a GAN system occur? A GAN training of both networks happens one after another. First of all the Discriminator must complete its training in an epoch. After that the Generator comes in and begins its own training for one (or even more) epochs. Then we continue to train the Generator and the Discriminator one after another.

It's important to keep the Generator unchanged while the Discriminator trains. The Discriminator has to have a chance to recognize current flaws in the Generator and improve itself by weight update.

The Discriminator has to be kept constant as well while the Generator trains. If not the Generator will be constantly behind the Discriminator and never have a chance to fool the Discriminator.

Thanks to the training in alternating steps, a GAN can solve intractable generative problems. We start with a simple classification task for the Discriminator. With increasing training steps, the Discriminator should have a bigger problem detecting fake generated data as such.

##### GAN convergence is hard to identify

When the Generator improves its output results with training, the Discriminator worsens its accuracy as it is no longer able to say weather the input image is real or fake. In case we have trained a "perfect generator" which produced "perfect fakes", the accuracy of the Discriminator will be somewhat around 50%. With this accuracy, the Discriminator basically gives random answers. As a result the Discriminator feedback makes no sense anymore. At this point the Discriminator outputs random classification results. With random classification results based on nothing meaningful, the Generator cannot learn relevant features and incorporates flaws into its system, as the Generator trains on useless feedback from the Discriminator.

Convergence in GANs is not stable. We have to find the "sweet spot" where the Discriminator is just about to output accuracy of 50% but still can give valuable feedback to the Generator.

##### The GAN Loss

The **Generator** and the **Discriminator** have different goals for the loss function presented above. The Generator tries to **minimize** it and the Discriminator tries to **maximize** it, hence the "minimax game" with the loss function. The main Discriminator's goal is to achieve **D(x)** to be **1** for real data. The Discriminator wants ideally to classify all the the samples it receives correctly in real and fake. The Discriminator also wants **D(G(z))** to be **0** if input data is fake, or in other words, it wants to output 0 probability that the input data **G(z)** is real.

The minimax log proceeds as following: **log(D(x)) = 0** if **D(x) = 1** as log(1) is always 0 (This is because any number raised to 0 equals 1). If D(x) is negative. So the Discriminator will prefer such values to maximize the loss.

As an analogy, **log(1- D(G(z)))** prefers values of D(G(z)) > 0.

This loss comes from the cross-entropy loss which measures the discrepancy between the real and "fake" distributions. The cross entropy loss is closely related to the Kullback–Leibler divergence between the target and predicted distributions. Cross-entropy is derived from Log Loss which deals with multi-class classification problems and Log Loss comes from Logistic regression.

##### Nothing is too easy

As GANs are relatively new, there are still things to improve. One of the main challenges in GANs includes the Vanishing Gradient Problem.

At the very beginning the Discriminator's job is fairly easy as the Generator produces a lot of nonsense at the start, so the Discriminator can effortlessly tell fake from real. If it happens to often, then the Generator receives no valuable feedback, hence it gets no meaningful weight update as the gradients (the feedback) are based on the perfect prediction of the Discriminator and the Generator does not know what features it should incorporate for further successful learning.

As a solution we may change the main Generator's goal from minimizing the complete MiniMax Loss function to maximizing the **D(G(x))** part of the MiniMax loss function.

##### Conclusion

The Discriminator and the Generator "compete" with each other in "a game" called minimax. The Discriminator tries to classify its input as accurately as possible weather the input it receive is real or fake. The Generator tries to output samples that look exactly like real samples. With more training steps the Discriminator will get better at detecting fake samples coming from the Generator and the Generator will become better at producing samples which are more difficult to detect as being fake. If the GAN system is balanced, then the Discriminator outputs 50% probability for fake or real, which means the Generator is doing a great job. After this the Generator can be used separately to create meaningful samples which are based on the original training data but are still brand new.

GANs are revolutionary idea of a new neural network structure. It is here to stay and to be evolved further on.

*Further recommended readings:*

The original GAN Paper

Progressive Growing of GANs for Improved Quality, Stability, and Variation

Towards the Automatic Anime Characters Creation with Generative Adversarial Networks