Generative Adversarial Network¶
This is a use case of DEATF where a Generative Adeversarial Network (GAN) is used.
The GAN is created by using two MLP. This is an unsupervised problem where two networks are used: one responsible of generating data (called generator) that is similar but not equal to the input data and the other one resposible to detect if the received data is real or created by the other network (called discriminator). The key in GANs is the interaction between those two networks.
-
gan.generator_loss(fake_out)¶ Loss function for the generator network. This function might seem complex, in order to understand it the function of crossentropy has to be explained:
cross_entropy = -( p(x) * log(q(x)) + (1 - p(x)) * log(1 - q(x)) )
In it p(x) is the probability of the target and q(x) is the probability of the prediction. If the probability of the target p(x) is 1, the second part of the equation would desapear. Otherwise, if p(x) is 0, the first part of the equation would be the one that desapears. The loss function of the generator consist in reducing the mean of -log(q(x)).
- Parameters
fake_out – Output of the generator model.
- Returns
The mean of -log(fake_out).
-
gan.discriminator_loss(fake_out, real_out)¶ Loss function for the discriminator network. With the explanation of the cross entropy from the genereator_loss function is easier to understand the discriminator loss function. At the end, it is:
d_loss = -log(real_out) - log(1 - fake_out)
- Parameters
fake_out – Output of the generator model.
real_out – Real data, features given to the GAN.
- Returns
(log(real_out) + log(1 - fake_out))
-
gan.eval_gan(nets, train_inputs, _, batch_size, iters, __, ___, ____)¶ This case is more complex than other examples, in it two models are used the generator and the discriminator. They are two separete models, but they are related by their loss function. The generator is meant to create outputs that are similar but not equal to the real input and the discriminator has to diferentiate between the reals and the generated by the generator. That is why their losses are linked and the train step is more complex and tf.function ahs to be used to determine how the training has to go on.
- Parameters
nets – Dictionary with the networks that will be used to build the final network and that represent the individuals to be evaluated in the genetic algorithm.
train_inputs – Input data for training, this data will only be used to give it to the created networks and train them.
train_outputs – Output data for training, it will be used to compare the returned values by the networks and see their performance.
batch_size – Number of samples per batch are used during training process.
iters – Number of iterations that each network will be trained.
test_inputs – Input data for testing, this data will only be used to give it to the created networks and test them. It can not be used during training in order to get a real feedback.
test_outputs – Output data for testing, it will be used to compare the returned values by the networks and see their real performance.
hypers – Hyperparameters that are being evolved and used in the process.
- Returns
Generator’s loss function that evaluates the true performance of the network.