Why were GANs designed in the first place?
It has been observed that most major neural networks can be easily tricked into misclassifying, adding only a small amount of noise to the raw data. Surprisingly, the model, after adding noise, has a higher confidence in the wrong prediction than when it was predicted correctly. The reason for this adversary is that most machine learning models learn from a limited amount of data, which is a huge disadvantage as it tends to overfit. In addition, the mapping between input and output is almost linear. While it may seem that the boundaries between the different classes are linear, in reality they are made up of linearities, and even a small change in a point in the feature space can lead to misclassification of the data.
How does GAN work?
Generative adversarial networks (GANs) can be broken down into three parts:
The GAN has a generator and a discriminator . The generator generates fake data samples (be it image, audio, etc.) and tries to trick the Discriminator. A discriminator, on the other hand, tries to distinguish between real and fake samples. The Generator and Discriminator are Neural Networks, and they both compete with each other during the training phase. The steps are repeated several times, and in this the Generator and Discriminator become better and better at their work after each repetition. The work can be visualized using the diagram below:
Here the generative model captures the distribution of the data and is trained like this in a way that tries to maximize the likelihood that the discriminator will make a mistake. The discriminator, on the other hand, is based on a model that estimates the likelihood that the resulting sample will come from the training data rather than from a generator.
GANs are formulated as a minimax game in which the Discriminator tries to minimize its reward V (D, G), and the Generator tries to minimize the Discriminator`s reward, or, in other words, maximize its loss. This can be mathematically described by the formula below:
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
So basically, GAN training consists of two parts: p>
The above method is repeated over several epochs. and then manually checks the fake data if it appears to be genuine. If this seems acceptable, then the learning is stopped, otherwise it is allowed to continue for several more eras.
Different types of GANs:
Currently, GANs are a very active research topic. and there are many different types of GAN implementations. Some of the important ones that are actively used today are described below:
Sample Python code that implements an adversarial network generator: b >
GANs are very computationally expensive. They require powerful GPUs and a lot of time (many epochs) to get good results. In our example, we`ll use the well-known MNIST dataset and use it to create a clone of a random digit.
on epoch0 on epoch1 ... ... ... on epoch498 on epoch499
Result after epoch 0:
Resulr after the 499th era:
So, from the above example, we can see that in the first image after the 0th epoch, pixels are scattered all over the place, and we couldn`t do anything from find out this.
But from the second image, we could see that the pixels are organized more systematically, and we could figure out that this is the number "7" that the code picked at random and the network was trying to clone it. In our example, we took 500 for the number of epochs. But you can increase this number to improve your score.