Answers.Body: <blockquote>
  <p>Why I have to set to real these fake images and what fake images are these?</p>
  </blockquote>
  <p>You set them to &quot;real&quot; 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.</p>
  <p>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.</p>
  <blockquote>
  <p>The one generated in the first round from the generator itself?</p>
  </blockquote>
  <p>No. New images generated just for training the generator.</p>
  <blockquote>
  <p>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).</p>
  </blockquote>
  <p>No. New images generated just for training the generator.</p>
  <p>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.</p>
  <blockquote>
  <p>Finally what the generator does to these faked images?</p>
  </blockquote>
  <p>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 <em>involved</em> in backpropagation calculations for that layer (with no difference to any other hidden layer in a CNN), but are not changed directly.</p>
  <p>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.</p>
  <p>In terms of training the generator/discriminator combined network to update the generator:</p>
  <ul>
  <li><p>The input to the combined network is some new random input vectors (typically a vector with independent truncated normal distribution for each element).</p>
  </li>
  <li><p>The &quot;ground truth&quot; label is 1 for every item.</p>
  </li>
  <li><p>The discriminator parameters must be &quot;frozen&quot;, somehow excluded from being updated.</p>
  </li>
  <li><p>Run the minibatch forward to get loss and backpropagate to get gradients for the whole network including the generator.</p>
  </li>
  <li><p>Apply a gradient step (usually via some optimiser, such as Adam).</p>
  </li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-16T09:36:38.083Z
Answers.Text: 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?
Answers.UserDisplayName: AntonYellow
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-16T10:38:02.800Z
Answers.Text: @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
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-16T12:14:13.153Z
Answers.Text: [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).]
Answers.UserDisplayName: AntonYellow
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-16T12:38:32.287Z
Answers.Text: 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
Answers.UserDisplayName: AntonYellow
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-16T12:59:59.333Z
Answers.Text: @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.
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-16T08:53:18.837Z
Answers.LastActivityDate: 2020-10-16T10:32:22.470Z
Answers.LastEditDate: 2020-10-16T10:32:22.470Z
Answers.UserDisplayName: Neil Slater
Answers.Body: <p>I'd like to add some details to the Neil Slater's answer.</p>
  <p>In order to generate data, we want to find some unknown distribution. Since we do not know anything about a real distribution, we can approximate it using GAN. It was shown that optimizing the loss function of the original GAN is equivalent to
  minimizing Jensen-Shannon divergence between the real distribution and the estimated (generator) distribution.</p>
  <p><a href="https://i.sstatic.net/Ktbwcm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ktbwcm.png" alt="enter image description here" /></a></p>
  <p>where <code>q</code> is our initial distribution and <code>p</code> is the target distribution.</p>
  <p>While training the Discriminator as a normal classification model (which learns to distinguish between generated and real samples), we want to push the Generator towards the real data distribution. To make the generated samples look realistic, we pass true labels to calculate the gradient in that direction. Here is the intuition behind generator training:</p>
  <p><a href="https://i.sstatic.net/Bfdcj.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Bfdcj.png" alt="enter image description here" /></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-19T18:19:28.577Z
Answers.LastActivityDate: 2020-10-19T18:24:43.660Z
Answers.LastEditDate: 2020-10-19T18:24:43.660Z
Answers.UserDisplayName: Aray Karjauv
Body: <p>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.</p>
  <p>I am using the MNIST dataset.</p>
  <ol>
  <li><p>I generate a batch of random images (the faked ones) with the generator.</p>
  </li>
  <li><p>I train the discriminator with the set composed of faked images and real MNIST images.</p>
  </li>
  <li><p>After the training phase, the discriminator modifies the weights in the direction of recognizing fake (probability 0) from real (probability 1) ones.</p>
  </li>
  <li><p>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).</p>
  </li>
  </ol>
  <p>My questions are:</p>
  <p>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?</p>
  
Tags: <training><generative-adversarial-networks><generative-model><mnist>
Title: What is the right way to train a generator in a GAN?
UserDisplayName: AntonYellow
ThreadText: # What is the right way to train a generator in a GAN?
  
  (Asked by: username_0 on 2020-10-16)
  
  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?
  
  **Tags:** training, generative-adversarial-networks, generative-model, mnist
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-10-16. Score: 1
  
  > 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).
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_1**: @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  
  > **username_0**: [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).]  
  > **username_0**: 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  
  > **username_1**: @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.  
  
  ---
  
  ### Answer by username_2 on 2020-10-19. Score: 1
  
  I'd like to add some details to the Neil Slater's answer.
  
  In order to generate data, we want to find some unknown distribution. Since we do not know anything about a real distribution, we can approximate it using GAN. It was shown that optimizing the loss function of the original GAN is equivalent to
  minimizing Jensen-Shannon divergence between the real distribution and the estimated (generator) distribution.
  
  [![enter image description here](https://i.sstatic.net/Ktbwcm.png)](https://i.sstatic.net/Ktbwcm.png)
  
  where `q` is our initial distribution and `p` is the target distribution.
  
  While training the Discriminator as a normal classification model (which learns to distinguish between generated and real samples), we want to push the Generator towards the real data distribution. To make the generated samples look realistic, we pass true labels to calculate the gradient in that direction. Here is the intuition behind generator training:
  
  [![enter image description here](https://i.sstatic.net/Bfdcj.png)](https://i.sstatic.net/Bfdcj.png)
  
  
  ---
  
  