Answers.Body: <p>It turns out there is actually a practical reason for this.</p>
  <p>Practically speaking, in GANs the generator tends to converge on few 'good' outputs that fool the discriminator if you don't do it. In the optimal case, the generator will actually emit a single fixed output regardless of the input vector that fools the discriminator.</p>
  <p>Which is to say, the generators loss function is intended not simply as &quot;fool the discriminator&quot;, it is actually:</p>
  <ul>
  <li>Fool the discriminator.</li>
  <li>Generate novel output.</li>
  </ul>
  <p>You can write your generator's loss function to explicitly attempt to say the output in any training batch should be distinct, but by passing the outputs to the discriminator you create a <strong>history of previous predictions</strong> from the generator, effectively applying a loss metric for when the generator tends to produce the same outputs over and over again.</p>
  <p>...but it is not magic, and is <em>not</em> about the discriminator learning &quot;good features&quot; features; it is about the loss applied to the <strong>generator</strong>.</p>
  <p>This is referred to as &quot;Mode Collapse&quot;, to quote the Google ML guide on GAN troubleshooting:</p>
  <blockquote>
  <p>If the generator starts producing the same output (or a small set of outputs) over and over again, the discriminator's best strategy is to learn to always reject that output. But if the next generation of discriminator gets stuck in a local minimum and doesn't find the best strategy, then it's too easy for the next generator iteration to find the most plausible output for the current discriminator.</p>
  <p>Each iteration of generator over-optimizes for a particular discriminator, and the discriminator never manages to learn its way out of the trap. As a result the generators rotate through a small set of output types. This form of GAN failure is called mode collapse.</p>
  </blockquote>
  <p>See also, for additional reading &quot;Unrolled GANs&quot; and &quot;Wasserstein loss&quot;.</p>
  <p>see: <a href="https://developers.google.com/machine-learning/gan/problems" rel="nofollow noreferrer">https://developers.google.com/machine-learning/gan/problems</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-27T09:46:41.667Z
Answers.LastActivityDate: 2020-01-27T09:46:41.667Z
Answers.LastEditDate: 2020-06-17T09:57:20.710Z
Answers.UserDisplayName: Doug
Answers.Body: <p>The main reason that the discriminator is trained concurrently with the generator is to provide (at least in theory) a smooth and gradual learning signal for the generator.</p>
  
  <p>If we trained the discriminator on only the input data, then, assuming our training algorithm converges well, it should quickly converge to a fixed model. The generator can then learn to fool this fixed model, but it will likely still be easy to spot the generator's fakes for a human. For example, the discriminator may have learned that, coincidentally, in the sample you provided, all images of trucks have a fully white pixel in the top left corner. If it learns that pattern, the generator can fool it by generating noise with a white pixel. Once the generator has learned this pattern, all learning stops.</p>
  
  <p>In contrast, suppose that the discriminator is repeatedly re-trained on a mixture of real and generated examples. The discriminator will be forced to learn more complex patterns than "white pixel in upper left", which improves its quality beyond the raw patterns in the sample data.</p>
  
  <p>The converse relationship is also true. If the generator is trained only on the training data, it will also likely pick out only the most obvious patterns. These patterns are likely to create many local minima in the weight space for the network. However, if the error signal from the discriminator is fed to the generator, then the generator must adapt: in effect, we are telling it "making the top left pixel white is not good enough to fool observers. Find more complex patterns". </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-19T04:17:57.737Z
Answers.Text: The discriminator is not going to be fooled by noise with a white pixel; it's been trained on pictures of trucks, it will recognise pictures of trucks. That's what NN's do. You're describing once again, augmenting your sample with additional fake samples, but all that proves if either a) your discriminator architecture is bad, or b) your sample is poor, surely?
Answers.UserDisplayName: Doug
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-19T04:23:51.413Z
Answers.Text: I believe the use of the generator output is to do with removing *artifacting* in the output, where say, 'truck like' images are recognized by the discriminator, but you want the discriminator to reject images which are 'truck like but have visual artifacts'. What I don't understand is *how/why* feeding the generator output in achieves this, other than, 'well, it seems to work pretty well in practice'.
Answers.UserDisplayName: Doug
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-19T18:48:37.630Z
Answers.Text: @Doug The white pixel example _is_ an example of artifacting. I picked a deliberatively extreme example to illustrate it. From the discriminative NN's perspective, if all pictures of trucks in the sample have a while pixel, that's a strong signal for being a truck. However, since it is easy for the generator to mimic, the discriminator is forced to learn more complex signals from the dataset. This iterative process repeats until (hopefully), the generator outputs things that look like the trucks in the input data, and the discriminator can't tell them apart anymore.
Answers.UserDisplayName: John Doucette
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-18T23:01:22.040Z
Answers.LastActivityDate: 2019-05-18T23:01:22.040Z
Answers.UserDisplayName: John Doucette
Answers.Body: <p>The discriminator's job is to tell between real images and generated images. It would be impossible to do that if it never actually sees generated images, just as if you wanted a network to differentiate between cats and dogs it wouldn't work if you only showed it pictures of dogs.<br>
  If you were to train a generator on only real images, all labels it would see would be a 1. What you end up with is a network that learns how to produce 1 regardless of its inputs, which is very easy to learn without finding any underlying patterns in the data. Once you add in the generated images and 0 labels it is forced to learn something interesting.  </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T08:07:53.583Z
Answers.Text: This may superficially appear to be correct according to "common sense", but that's not the question I was asking. You don't need a discriminator to be trained on generated images; you just need a discriminator to be able to be 'fuzzy' in it's matching. Early on, it should be fooled by random noise, later on it should be fooled only be high quality real images, so it provides a gradient to the generator. However, as far as I can tell there's actually no specific need to feed it the output of the generator.
Answers.UserDisplayName: Doug
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T08:09:30.253Z
Answers.Text: That's simply a convenient way to generate the right 'sort' of characteristics from a discriminator. Indeed, as you note, a pre-trained 'perfect discriminator' would make learning impossible (no gradient)... but for example, you could mix in random noise samples into the discriminator training set, and incrementally remove the noise sample to reduce the 'fuzziness' as you progress with training.
Answers.UserDisplayName: Doug
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-02T08:14:49.610Z
Answers.Text: There's a lot of hand waving and 'common sense' about building GANs, but I contend it's a lot more 'this example works in practice and we're just tweaking it' than 'we actually understand why the dynamic equilibrium produces the right sort of discriminator network'.
Answers.UserDisplayName: Doug
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-29T01:01:37.133Z
Answers.LastActivityDate: 2019-09-29T01:01:37.133Z
Answers.UserDisplayName: Frobot
Body: <p>I've been doing some reading about GANs, and although I've seen several excellent examples of <em>implementations</em>, the descriptions of why those patterns were chosen isn't clear to me in many cases.</p>
  
  <p>At a very high level, the purpose of the <strong>discriminator</strong> in a GAN is establish a <strong>loss function</strong> that can be used to train the <strong>generator</strong>.</p>
  
  <p>ie. Given the random input to the generator, the discriminator should be able to return a probability of the result being a 'real' image.</p>
  
  <p>If the discriminator is perfect the probability will always be zero, and the loss function will have no gradient.</p>
  
  <p>Therefore you iterate:</p>
  
  <ul>
  <li>generate random samples</li>
  <li>generate output from the generator</li>
  <li>evaluate the output using the discriminator</li>
  <li>train the generator</li>
  <li>update the discriminator to be more accurate by training it on samples from the real distribution and <strong>output from the generator</strong>.</li>
  </ul>
  
  <p>The problem, and what I don't understand, is point 5 in the above.</p>
  
  <p>Why do you use the output of the generator?</p>
  
  <p>I absolutely understand that you need to iterate on the accuracy of the discriminator. </p>
  
  <p>To start with it needs to respond with a non-zero value for the effectively random output from the generator, and slowly it needs to converge towards correctly classifying images at 'real' or 'fake'.</p>
  
  <p>In order to achieve this we iterate, training the generator with images from the real distribution, pushing it towards accepting 'real' images.</p>
  
  <p>...and with the images from the generator; but I don't understand why.</p>
  
  <p>Effectively, you have a set of real images (eg. 5000 pictures of faces), that represent a sample from the latent space you want the GAN to converge on (eg. all pictures of faces).</p>
  
  <p>So the argument goes: </p>
  
  <p>As the generator is trained iteratively closer and closer to <strong>generating images from the latent space</strong>, the discriminator is iteratively trained to <strong>recognise from the latent space</strong>, as though it had a <em>much larger sample size</em> than the 5000 (or whatever) sample images you started with.</p>
  
  <p>...ok, but that's daft.</p>
  
  <p>The <em>whole point</em> of DNN's is that given a sample you can train it to recognise images from the latent space the samples represent.</p>
  
  <p>I've never seen a DNN where the first step was 'augment your samples with extra procedurally generated fake samples'; the only reason to do this would be if you can only recognise samples in the input set, ie. your network is over-fitted.</p>
  
  <p>So, as a specific example, why can't you incrementally train the discriminator on samples of ('real' * epoch/iterations + 'noise' * 1 - epoch/iterations), where 'noise' is just a random input vector.</p>
  
  <p>Your discriminator will then <em>necessarily</em> converge towards recognising real images, as well as offering a meaningful gradient to the generator.</p>
  
  <p>What benefit does feeding the output of the generator in offer over this?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-18T22:55:32.690Z
Comments.Text: In your title, you ask about using the output of the discriminator to train the generator, but in your question body, you ask about using the output of the generator to train the discriminator. I'm going to assume you mean what it says in the body, but you should edit the question to be self-consistent.
Comments.UserDisplayName: John Doucette
Tags: <generative-adversarial-networks>
Title: Why use the output of the generator to train the discriminator in a GAN?
UserDisplayName: Doug
ThreadText: # Why use the output of the generator to train the discriminator in a GAN?
  
  (Asked by: username_0 on 2019-05-18)
  
  I've been doing some reading about GANs, and although I've seen several excellent examples of *implementations*, the descriptions of why those patterns were chosen isn't clear to me in many cases.
  
  At a very high level, the purpose of the **discriminator** in a GAN is establish a **loss function** that can be used to train the **generator**.
  
  ie. Given the random input to the generator, the discriminator should be able to return a probability of the result being a 'real' image.
  
  If the discriminator is perfect the probability will always be zero, and the loss function will have no gradient.
  
  Therefore you iterate:
  
  * generate random samples
  * generate output from the generator
  * evaluate the output using the discriminator
  * train the generator
  * update the discriminator to be more accurate by training it on samples from the real distribution and **output from the generator**.
  
  The problem, and what I don't understand, is point 5 in the above.
  
  Why do you use the output of the generator?
  
  I absolutely understand that you need to iterate on the accuracy of the discriminator.
  
  To start with it needs to respond with a non-zero value for the effectively random output from the generator, and slowly it needs to converge towards correctly classifying images at 'real' or 'fake'.
  
  In order to achieve this we iterate, training the generator with images from the real distribution, pushing it towards accepting 'real' images.
  
  ...and with the images from the generator; but I don't understand why.
  
  Effectively, you have a set of real images (eg. 5000 pictures of faces), that represent a sample from the latent space you want the GAN to converge on (eg. all pictures of faces).
  
  So the argument goes:
  
  As the generator is trained iteratively closer and closer to **generating images from the latent space**, the discriminator is iteratively trained to **recognise from the latent space**, as though it had a *much larger sample size* than the 5000 (or whatever) sample images you started with.
  
  ...ok, but that's daft.
  
  The *whole point* of DNN's is that given a sample you can train it to recognise images from the latent space the samples represent.
  
  I've never seen a DNN where the first step was 'augment your samples with extra procedurally generated fake samples'; the only reason to do this would be if you can only recognise samples in the input set, ie. your network is over-fitted.
  
  So, as a specific example, why can't you incrementally train the discriminator on samples of ('real' \* epoch/iterations + 'noise' \* 1 - epoch/iterations), where 'noise' is just a random input vector.
  
  Your discriminator will then *necessarily* converge towards recognising real images, as well as offering a meaningful gradient to the generator.
  
  What benefit does feeding the output of the generator in offer over this?
  
  **Tags:** generative-adversarial-networks
  > **Comments:**
  > **username_1**: In your title, you ask about using the output of the discriminator to train the generator, but in your question body, you ask about using the output of the generator to train the discriminator. I'm going to assume you mean what it says in the body, but you should edit the question to be self-consistent.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2020-01-27. Score: 1 | Accepted answer
  
  It turns out there is actually a practical reason for this.
  
  Practically speaking, in GANs the generator tends to converge on few 'good' outputs that fool the discriminator if you don't do it. In the optimal case, the generator will actually emit a single fixed output regardless of the input vector that fools the discriminator.
  
  Which is to say, the generators loss function is intended not simply as "fool the discriminator", it is actually:
  
  * Fool the discriminator.
  * Generate novel output.
  
  You can write your generator's loss function to explicitly attempt to say the output in any training batch should be distinct, but by passing the outputs to the discriminator you create a **history of previous predictions** from the generator, effectively applying a loss metric for when the generator tends to produce the same outputs over and over again.
  
  ...but it is not magic, and is *not* about the discriminator learning "good features" features; it is about the loss applied to the **generator**.
  
  This is referred to as "Mode Collapse", to quote the Google ML guide on GAN troubleshooting:
  
  > If the generator starts producing the same output (or a small set of outputs) over and over again, the discriminator's best strategy is to learn to always reject that output. But if the next generation of discriminator gets stuck in a local minimum and doesn't find the best strategy, then it's too easy for the next generator iteration to find the most plausible output for the current discriminator.
  >
  > Each iteration of generator over-optimizes for a particular discriminator, and the discriminator never manages to learn its way out of the trap. As a result the generators rotate through a small set of output types. This form of GAN failure is called mode collapse.
  
  See also, for additional reading "Unrolled GANs" and "Wasserstein loss".
  
  see: <https://developers.google.com/machine-learning/gan/problems>
  
  
  ---
  
  ### Answer by username_1 on 2019-05-18. Score: 3
  
  The main reason that the discriminator is trained concurrently with the generator is to provide (at least in theory) a smooth and gradual learning signal for the generator.
  
  If we trained the discriminator on only the input data, then, assuming our training algorithm converges well, it should quickly converge to a fixed model. The generator can then learn to fool this fixed model, but it will likely still be easy to spot the generator's fakes for a human. For example, the discriminator may have learned that, coincidentally, in the sample you provided, all images of trucks have a fully white pixel in the top left corner. If it learns that pattern, the generator can fool it by generating noise with a white pixel. Once the generator has learned this pattern, all learning stops.
  
  In contrast, suppose that the discriminator is repeatedly re-trained on a mixture of real and generated examples. The discriminator will be forced to learn more complex patterns than "white pixel in upper left", which improves its quality beyond the raw patterns in the sample data.
  
  The converse relationship is also true. If the generator is trained only on the training data, it will also likely pick out only the most obvious patterns. These patterns are likely to create many local minima in the weight space for the network. However, if the error signal from the discriminator is fed to the generator, then the generator must adapt: in effect, we are telling it "making the top left pixel white is not good enough to fool observers. Find more complex patterns".
  
  > **Comments:**
  > **username_0**: The discriminator is not going to be fooled by noise with a white pixel; it's been trained on pictures of trucks, it will recognise pictures of trucks. That's what NN's do. You're describing once again, augmenting your sample with additional fake samples, but all that proves if either a) your discriminator architecture is bad, or b) your sample is poor, surely?  
  > **username_0**: I believe the use of the generator output is to do with removing *artifacting* in the output, where say, 'truck like' images are recognized by the discriminator, but you want the discriminator to reject images which are 'truck like but have visual artifacts'. What I don't understand is *how/why* feeding the generator output in achieves this, other than, 'well, it seems to work pretty well in practice'.  
  > **username_1**: @Doug The white pixel example _is_ an example of artifacting. I picked a deliberatively extreme example to illustrate it. From the discriminative NN's perspective, if all pictures of trucks in the sample have a while pixel, that's a strong signal for being a truck. However, since it is easy for the generator to mimic, the discriminator is forced to learn more complex signals from the dataset. This iterative process repeats until (hopefully), the generator outputs things that look like the trucks in the input data, and the discriminator can't tell them apart anymore.  
  
  ---
  
  ### Answer by username_2 on 2019-09-29. Score: 1
  
  The discriminator's job is to tell between real images and generated images. It would be impossible to do that if it never actually sees generated images, just as if you wanted a network to differentiate between cats and dogs it wouldn't work if you only showed it pictures of dogs.  
  If you were to train a generator on only real images, all labels it would see would be a 1. What you end up with is a network that learns how to produce 1 regardless of its inputs, which is very easy to learn without finding any underlying patterns in the data. Once you add in the generated images and 0 labels it is forced to learn something interesting.
  
  > **Comments:**
  > **username_0**: This may superficially appear to be correct according to "common sense", but that's not the question I was asking. You don't need a discriminator to be trained on generated images; you just need a discriminator to be able to be 'fuzzy' in it's matching. Early on, it should be fooled by random noise, later on it should be fooled only be high quality real images, so it provides a gradient to the generator. However, as far as I can tell there's actually no specific need to feed it the output of the generator.  
  > **username_0**: That's simply a convenient way to generate the right 'sort' of characteristics from a discriminator. Indeed, as you note, a pre-trained 'perfect discriminator' would make learning impossible (no gradient)... but for example, you could mix in random noise samples into the discriminator training set, and incrementally remove the noise sample to reduce the 'fuzziness' as you progress with training.  
  > **username_0**: There's a lot of hand waving and 'common sense' about building GANs, but I contend it's a lot more 'this example works in practice and we're just tweaking it' than 'we actually understand why the dynamic equilibrium produces the right sort of discriminator network'.  
  
  ---
  
  