# What is the right way to train a generator in a GAN?

I am not fully understanding how to train a GAN's generator. I have a few questions below, but let me first describe what I am doing.

I am using the MNIST dataset.

1. I generate a batch of random images (the faked ones) with the generator.

2. I train the discriminator with the set composed of faked images and real MNIST images.

3. After the training phase, the discriminator modifies the weights in the direction of recognizing fake (probability 0) from real (probability 1) ones.

4. At this point, I have to consider the combined model of generator and discriminator (keep untrainable the discriminator) and put in the generator as input the faked images with the tag of 1s (as was real one).

My questions are:

Why do I have to set to real these fake images, and what fake images are these? The one generated in the first round from the generator itself? Or only the one classified as faked by the discriminator? (Then they could be both real images classified wrongly or fake images classified in the right way). Finally, what the generator does to these faked images?

Why I have to set to real these fake images and what fake images are these?

You set them to "real" label for the discriminator when training the generator, because that is the goal of the generator, to produce an output of 1 (probability of being a real image) when tested.

Usually you will generate a new batch of generated images for this step in training. You just used the last generated mini-batch to train the discriminator, so you expect them to score worse. Sending the exact same images again will cause correlation between the two minibatches that you want to avoid. It would not be a disaster, but training GANs can be quite difficult and sensitive to details like this, so it is better to keep generating new images and not re-use the previous ones.

The one generated in the first round from the generator itself?

No. New images generated just for training the generator.

Or only the one classified as faked by the discriminator? (then they could be both real images classified wrongly or fake images classified in the right way).

No. New images generated just for training the generator.

Out of interest though, if the discriminator classifies a fake image as 100% real (with a probability close to 1), then the generator will not learn anything from that. The gradients would all be zero.

Finally what the generator does to these faked images?

Nothing is done to the images themselves - unless perhaps you are keeping some copies to render and monitor training progress etc. The images occur within the combined generator/discriminator network, effectively as a hidden layer. The images are represented as artificial neuron output, so they are involved in backpropagation calculations for that layer (with no difference to any other hidden layer in a CNN), but are not changed directly.

The generator uses the gradients calculated from the combined discriminator/generator network to update its weights using gradient descent. Importantly in this phase of the updates, the discriminator weights are not changed.

In terms of training the generator/discriminator combined network to update the generator:

• The input to the combined network is some new random input vectors (typically a vector with independent truncated normal distribution for each element).

• The "ground truth" label is 1 for every item.

• The discriminator parameters must be "frozen", somehow excluded from being updated.

• Run the minibatch forward to get loss and backpropagate to get gradients for the whole network including the generator.

• Apply a gradient step (usually via some optimiser, such as Adam).

• Many thanks for the prompt answer! Said that, how the algorithm works after that? We have the generator trained with this batch and now it is the discriminator turn right? So I use a new generated batch from the generator + real data set, and then inject in the discriminator alone, that does the new update on weights and so on, (it follows then another round of combined gen and discr to train generator with fix discriminator.....) until the nash equilibrium if obtained. Am I right? Oct 16 '20 at 9:36
• @AntonYellow Yes then you go back to (1) in your description and repeat. I don't think it is really a Nash equilibrium - although there is a game-like feel to it, the networks are not trying to optimise decisions or actions, each only has one action and is trying to improve their chance of success. The equilibrium point depends critically on how well the training data of real images covers the population of all possible images of the thing you want the generator to learn, plus the limits of sophistication of the dsicriminator Oct 16 '20 at 10:38
• [Run the minibatch forward to get loss and backpropagate to get gradients for the whole network including the generator. Apply a gradient step (usually via some optimiser, such as Adam).] Oct 16 '20 at 12:14
• Ok good for the Nash eq. Get back on what we were, the last two bullet points in your answer mean that we run the minibatch and get the loss respect the overall cost function (gen+discriminator) and then backpropagate the signal towards the generator, but the discriminator is not touched by this update, right? If it is the case, why use a combined model and don't stick only with the separated generator. I am near the answer but I need a further push:-) to understand why this interconnection is needed Oct 16 '20 at 12:38
• @AntonYellow: You need the discriminator included in order to have a measurable loss value for the generator's output, plus the gradients associated with that loss. The generator cannot compare to the goal of creating a realistic fake by itself - it outputs an image, and has no way to get a score for that image. If this was not a GAN, and if you expected a specific/precise image then you could get a score, e.g. if you knew what exact image a generator should make given the input, then you could measure the difference and get a gradient. But with a GAN you cannot, the image is random. Oct 16 '20 at 12:59

where q is our initial distribution and p is the target distribution.