Answers.Body: <p>I had this same problem some time ago, here's what you should do:</p>
  <p>notice how we first input i1 to the generator, that outputs o1, than we input o1 into the discriminator, that outputs y1, so the output from the first (o1) is the input to the second, but that's like if we combine the two networks into one from a moment, right? under the hood, a layer from a neural network connects with the next layer, just like the output layer from the generator connected with the input layer from the generator, so you could interpret them as a single big neural network. Then it's easy to see how we would backpropagate through this whole combined network, all the way into the first layer of the generator, successfully updating all it's parameters, thus training it.</p>
  <p>That's not all when training a GAN, as we need to also train the discriminator separately with real images. For that we simply input the image directly into the discriminator, and when backpropagating, we stop in the discriminator, as anything from the generator has nothing to do with the real images.</p>
  <p>Really, all you need to do is treat the generator and discriminator as one big neural network when you want to input noise to the generator, but separate them when you want to input a real image into the discriminator.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-05T19:11:59.433Z
Answers.LastActivityDate: 2024-06-05T19:11:59.433Z
Answers.UserDisplayName: Osmar
Answers.Body: <p>Let's take a look at the images that fooled the D-network. When this happens the binary cross entropy loss of the D-network is important to look at. The network said a fake image was real. As usual, you back propagate to the very beginning of the D-network. What is the back prop doing? It is telling the weights in the D-network to change such that you lessen the loss (remember you are minimizing the loss function by going down hill --> using gradient descent). So you changed the weights of the D-network, but you really don't have to stop there. </p>
  
  <p>The complex nonlinear function the D-network is calculating is really f(Image, weights of D-network). So the output of the D-network is really dependent on the Image fed in and the weights of the D-network. The cost is a function of the output f. So we have c = g(f(Image, weights of D-network)) where g is really the binary cross entropy. Looking at this, we see that the cost is also a function of the image!</p>
  
  <p>We can take the partials with respect to the image just like we take the partials with respect to the weights to update the weights. And so after doing back prop to the image, we don't stop -> we take the partial of the cost function with respect to the image that is fed into the D-network and we update the image. And essentially we <em>don't have the original image</em> when we continue to back prop to the beginning of the G-network. Let me know if you'd like more clarification. Again this is my understanding and I haven't coded a GAN yet, so someone please confirm. Also, I am open to others' interpretations. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-18T05:13:04.210Z
Answers.LastActivityDate: 2019-01-18T05:13:04.210Z
Answers.UserDisplayName: Ali_Ayub
Body: <p>For the purposes of this question I am asking about training the generator, assume that training the discriminator is another topic.</p>
  
  <p>My understanding of generative adversarial networks is that you feed random input data to the generator and it generates images. Out of those images, the ones which the discriminator thinks are real are used to train the generator.</p>
  
  <p>For example, I have the random inputs <span class="math-container">$i_1$</span>, <span class="math-container">$i_2$</span>, <span class="math-container">$i_3$</span>, <span class="math-container">$i_4$</span>... from which the generator produces <span class="math-container">$o_1$</span>, <span class="math-container">$o_2$</span>, <span class="math-container">$o_3$</span>, <span class="math-container">$o_4$</span>. Say for example, the discriminator thinks that <span class="math-container">$o_1$</span> and <span class="math-container">$o_2$</span> are real but <span class="math-container">$o_3$</span> and <span class="math-container">$o_4$</span> are fake, I then throw away input output pairs 3 and 4, but keep 1 and 2, and run back propagation on the generator to tell it that <span class="math-container">$i_1$</span> should produce <span class="math-container">$o_1$</span>, and <span class="math-container">$i_2$</span> should produce <span class="math-container">$o_2$</span> since these were "correct" according to the discriminator.</p>
  
  <p>The contradiction seems to come from the fact that the generator <em>already</em> generates those outputs from those inputs, so nothing will be gained by running backprop on those input output pairs.</p>
  
  <p>Where is the flaw in my logic here? I seem to have something wrong in my reasoning, or a misunderstanding of how the generator is trained.</p>
  
Tags: <backpropagation><generative-adversarial-networks>
Title: Training the generator in a GAN pair with back propagation
UserDisplayName: F Chopin
ThreadText: # Training the generator in a GAN pair with back propagation
  
  (Asked by: username_0 on 2019-01-18)
  
  For the purposes of this question I am asking about training the generator, assume that training the discriminator is another topic.
  
  My understanding of generative adversarial networks is that you feed random input data to the generator and it generates images. Out of those images, the ones which the discriminator thinks are real are used to train the generator.
  
  For example, I have the random inputs $i\_1$, $i\_2$, $i\_3$, $i\_4$... from which the generator produces $o\_1$, $o\_2$, $o\_3$, $o\_4$. Say for example, the discriminator thinks that $o\_1$ and $o\_2$ are real but $o\_3$ and $o\_4$ are fake, I then throw away input output pairs 3 and 4, but keep 1 and 2, and run back propagation on the generator to tell it that $i\_1$ should produce $o\_1$, and $i\_2$ should produce $o\_2$ since these were "correct" according to the discriminator.
  
  The contradiction seems to come from the fact that the generator *already* generates those outputs from those inputs, so nothing will be gained by running backprop on those input output pairs.
  
  Where is the flaw in my logic here? I seem to have something wrong in my reasoning, or a misunderstanding of how the generator is trained.
  
  **Tags:** backpropagation, generative-adversarial-networks
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2024-06-05. Score: 1
  
  I had this same problem some time ago, here's what you should do:
  
  notice how we first input i1 to the generator, that outputs o1, than we input o1 into the discriminator, that outputs y1, so the output from the first (o1) is the input to the second, but that's like if we combine the two networks into one from a moment, right? under the hood, a layer from a neural network connects with the next layer, just like the output layer from the generator connected with the input layer from the generator, so you could interpret them as a single big neural network. Then it's easy to see how we would backpropagate through this whole combined network, all the way into the first layer of the generator, successfully updating all it's parameters, thus training it.
  
  That's not all when training a GAN, as we need to also train the discriminator separately with real images. For that we simply input the image directly into the discriminator, and when backpropagating, we stop in the discriminator, as anything from the generator has nothing to do with the real images.
  
  Really, all you need to do is treat the generator and discriminator as one big neural network when you want to input noise to the generator, but separate them when you want to input a real image into the discriminator.
  
  
  ---
  
  ### Answer by username_2 on 2019-01-18. Score: 0
  
  Let's take a look at the images that fooled the D-network. When this happens the binary cross entropy loss of the D-network is important to look at. The network said a fake image was real. As usual, you back propagate to the very beginning of the D-network. What is the back prop doing? It is telling the weights in the D-network to change such that you lessen the loss (remember you are minimizing the loss function by going down hill --> using gradient descent). So you changed the weights of the D-network, but you really don't have to stop there.
  
  The complex nonlinear function the D-network is calculating is really f(Image, weights of D-network). So the output of the D-network is really dependent on the Image fed in and the weights of the D-network. The cost is a function of the output f. So we have c = g(f(Image, weights of D-network)) where g is really the binary cross entropy. Looking at this, we see that the cost is also a function of the image!
  
  We can take the partials with respect to the image just like we take the partials with respect to the weights to update the weights. And so after doing back prop to the image, we don't stop -> we take the partial of the cost function with respect to the image that is fed into the D-network and we update the image. And essentially we *don't have the original image* when we continue to back prop to the beginning of the G-network. Let me know if you'd like more clarification. Again this is my understanding and I haven't coded a GAN yet, so someone please confirm. Also, I am open to others' interpretations.
  
  
  ---
  
  