Path: blob/master/Generative AI for Intelligent Data Handling/Day 6 GANS_lab1.ipynb
3074 views
Understanding Generative Adversarial Networks GANs
Objectives
Understand the original formulation of GANs, and their two separately trained networks: Generator and Discriminator
Implement GANs on simulated and real datasets
Installing Required Libraries
The following required libraries are pre-installed in the Skills Network Labs environment. However, if you run these notebook commands in a different Jupyter environment (like Watson Studio or Ananconda), you will need to install these libraries by removing the #
sign before !pip3 install --upgrade tensorflow
in the cell below.
Importing Required Libraries
Helper function
Background
Generative Adversarial Networks (GANs) are generative models that convert random samples of one distribution into another distribution. They have several applications, like the following:
Generate Examples for Image Datasets
Generate Photographs of Human Faces
Generate Realistic Photographs
Generate Cartoon Characters
Image-to-Image Translation
Text-to-Image Translation
Face Frontal View Generation
Generate New Human Poses
Face Aging
Photo Blending
Super Resolution
Photo Inpainting
Clothing Translation
Video Prediction
In this lab, we will use a toy example to help understand the basic theoretical principles behind GANs. The original form of GANs consisted of a discriminator and a generator; let's use the analogy of a currency forger and the police.
The Generator is the currency forger, and the output is the counterfeit, for example, a 100-dollar bill. The discriminator is analogous to the police taking the counterfeit and trying to determine if it's real by comparing it to a real $100 bill. In real life, if the counterfeit is easy to detect, the forger will adapt; conversely, the police will also improve; GANs emulate this game of cat and mouse.
What makes GANs interesting is that the discriminator and generator continuously improve each other by a well-formulated cost function that backpropagates the errors. GANs are a family of algorithms that use learning by comparison. In the lab, we will review the original formulation and use a simulated dataset. We will also point you to some more advanced methods and issues you will encounter with the real datasets for the next lab.
Toy Data
Consider the following data, , that is normally distributed with a mean of 10 and a standard deviation of 1. Now we would like to randomly sample data from this distribution.
We also have the data sample, z, which is also normally distributed , with mean of 0 and a standard deviation of 2:
Let's compare the two distributions:
Let's create our first generative model by adding 10 to every sample of . We will call the result as it's an approximation of . It is not too difficult to show that .
We see that the mean and standard deviation are almost identical
Similarly for the histograms
In the case above, since we just add 10 to the latent variable , we transform using a deterministic function. We can call this an implicit generative model.
The Generator
There are two networks involved in a GAN, the Generator and the Discriminator. Let's understand the Generator network first.
The Generator is a neural network denoted by ; the idea is that a neural network can approximate any function (by the Universal Approximation Theorem), so you should be able to generate data samples from any type of distribution.
Our goal is to convert the samples, , to one that approximates , i.e . Let's build a simple Generator using Keras.
The following is a function that outputs a generator using Kera's Sequential model object.
We can use the Generator to convert and make a prediction , and display the histogram of the distributions of and . As the model is not trained, the trained distributions are quite different:
We will discuss the use of the parameter training=False
later on.
The Discriminator
The discriminator is a neural network that learns to distinguish between actual and generated samples. The simplest Discriminator is a simple logistic regression function. Let's create a discriminator in Keras with one Dense layer; we leave the logistic function out as it will be incorporated in the cost function, which is the convention in Keras.
The discriminator and generator are randomly initialized, but we can plot the output of each and compare it to the true data distribution, with the generated data in red and the real data in green, and the logistic function as a function of the x axis. We also include the threshold. If the output of the logistic function is less than 0.5, the sample is classified as generated data; conversely, if the output is greater than 0.5, the sample will be classified as data that came from the real distribution.
Applying the sigmoid function to the discriminator output, we get the probabilites that the samples belong to the real distribution. We can count the number of true samples that the discriminator correctly classifies.
For the real data, the discriminator successfully assigns a probability greater than 0.5 for all 5000 samples:
For the generated data, only 1425 out of the 5000 samples are classified as having more than 50% chance of coming from the real distribution.
We can also use the follwoing to find the average value of the sigmoid function for all the samples.
In many cases, we can instead study the difference in the distribution; in this case, the discriminator is called a Critic, a real-valued function.
The Loss Fuction GANs (optional)
GANs convert an unsupervised learning problem to a supervised one. Instead of formulating the problem like a two-player minimax game with a value function like in <a href=https://arxiv.org/pdf/1406.2661.pdf?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkML311Coursera35714171-2022-01-01 >[1], we can treat the problem of maximizing the familiar log-likelihood of the logistic function analogous to minimizing the cross-entropy loss, then incorporate the generator and discriminator.
Discriminator
In order to train the GANS, we start off with standard maximization of the likelihood for the discriminator for the standard dataset :
Where for samples from the true distribution and for samples from the generator. The goal is to maximize this term with respect to :
To also incorporate the generated samples, we augment the right side of the equation with the generated th sample . As they are not part of the dataset , we have to include a second summation where . Finally, combining the cases of and , we get:
Generator
For the generator we simply replace with the .
As this is a density estimation problem, it is common to replace the summation with the expected value like in <a href=https://arxiv.org/pdf/1406.2661.pdf?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkML311Coursera35714171-2022-01-01 >[1]. We replace the summations with an expectation where is the true distribution and is the distribution of .
As we are trying to trick the discriminator, we would like to find a that minimize the above expression, such as:
Training GANS
Training Generator
GANs are quite difficult to train, even for a simple example. Let's start off with training the generator in practice.
is difficult to work with as is near one or zero for the first few iterations. This is because the generator is not yet properly trained, and the discriminator can easily distinguish between the generated and actual samples. Therefore we maximize .
Although the output of the generator passes through the discriminator, we do not update the generator in the optimization step, hence we set the parameter training=False
in the actual training steps.
Instead of maximizing the term, we can take the negative and minimize it. The resultant expression can be calculated in Keras using the cross-entropy loss where all the target values are set to one:
Training Discriminator
We can also use the cross-entropy to train the discriminator; we simply multiply by a negative number, set for the generated values and for the real values. We do not update the generator parameters.
The first term is the real loss and the second is the fake loss in Keras.
We create the optimizer for the discriminator and generator:
We now train the model; as the dataset is small, we will use batch gradient descent.
For each iteration we will generate real examples , these are from the generating distribution . This would be our actual dataset if we used real data.
We will then generate a sample batch of noise samples from noise prior and convert the result to a generated image using the generator .
We determine the output of the discriminator for both the real and generated samples. We calculate the loss and then update the discriminator and generator through their respective stochastic gradients.
The convergence of GAN training is a subject in itself. But let's explore a method that works for this simple dataset. Intuitively, we know that if our generated data is identical to our actual data, the probability of correctly classifying is random. Therefore if the generated and actual data are of equal proportion, and .
We only display iterations where the average discriminator output gets closer to 50% for both the generated data and actual data.
For more on training GANs check out the following blog. We can display the best performing model
We usually use this version of TensorFlow for the rest of the course, so don't forget to downgrade
This code defines a simple GAN architecture to generate responses to mathematical expressions. The generator takes a random noise vector as input and produces a mathematical expression, while the discriminator takes a mathematical expression as input and outputs a probability that the expression is real or generated.
Please note:
This code is a simplified example and may not produce accurate results.
GANs require significant training data and fine-tuning to generate high-quality outputs.
This code uses a simple architecture and may not be suitable for complex mathematical expressions.
To improve the accuracy of the GAN, you can:
Increase the training data size and diversity.
Use a more complex architecture, such as a convolutional neural network (CNN) or recurrent neural network (RNN).
Fine-tune the hyperparameters, such as the learning rate and batch size.
Use a different loss function or training strategy.