<p>In the generator step training, you can think that the network involves the discriminator too. But to do the backpropagation, you will only consider the generator weights. A good explanation for it is found <a href="http://www.rricard.me/machine/learning/generative/adversarial/networks/2017/04/05/gans-part1.html" rel="noreferrer">here</a>.</p>

<p>As mentioned in <a href="https://arxiv.org/abs/1406.2661" rel="noreferrer">original paper</a>, the Discriminator cost is:</p>

<p><a href="https://i.stack.imgur.com/hsGm2.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/hsGm2.gif" alt="enter image description here"></a></p>

<p>And the generator cost is:</p>

<p><a href="https://i.stack.imgur.com/G7jMP.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/G7jMP.gif" alt="enter image description here"></a></p>

<p>Of course, you don't need to calculate it by hand. Tensorflow already handles it. To do all the process, you can implement the following:</p>

<pre><code>G_sample = generator(z)
D_real = discriminator(X)
D_fake = discriminator(G_sample)

D_loss = tf.reduce_mean(-tf.log(D_real)-tf.log(1-D_fake))
G_loss = tf.reduce_mean(-tf.log(D_fake))
</code></pre>

<p>where D_real, D_fake and D_sample are the last layers of your network.
Then you can implement the training process by the standard way:</p>

<pre><code>D_solver = (tf.train.AdamOptimizer(learning_rate=0.0001,beta1=0.5)
            .minimize(D_loss, var_list=theta_D))
G_solver = (tf.train.AdamOptimizer(learning_rate=0.0001,beta1=0.5)
            .minimize(G_loss, var_list=theta_G))
</code></pre>

<p>And just run the solvers in a session.</p>
TensorFlow MNIST DCGAN: how to set up the loss function?
<p>I would like to build a <a href="https://arxiv.org/abs/1511.06434" rel="nofollow noreferrer">DCGAN</a> for MNIST by myself in TensorFlow. However, I'm struggling to find out how I should set up the loss function for the generator. In a <a href="https://github.com/jacobgil/keras-dcgan" rel="nofollow noreferrer">Keras DCGAN implementation</a> the author used a little "workaround" for this problem: he simply built 3 models. The generator (G), the discriminator (D) and third one, where he just combined G with D, while setting the train-ability of D to false there.</p>

<p>This way, he can feed D with real images + generated images to train D and train the G+D-combined model, because the loss of D is propagated to G, since D is not trainable in the G+D-combined model.</p>

<p>In TensorFlow, I've built G and D already. Training D is relatively simple, since I just need to combine a batch of real MNIST training images with generated ones and call the training op:</p>

<pre><code>session.run(D_train_op,
            feed_dict={x: batch_x, y: batch_y})
</code></pre>

<p>The training op in this example is a binary <a href="https://www.tensorflow.org/api_docs/python/tf/losses/softmax_cross_entropy" rel="nofollow noreferrer">cross entropy</a>:</p>

<pre><code>tf.losses.softmax_cross_entropy(y, D_out)
</code></pre>

<p>...but how would I set up the loss function for G, when I do not have a "stacked" model, combining "G and D" to single, third model?</p>

<p>I know that I have to generate a batch of images with G, feed them into D and then I can obtain the loss of D...however, the output of G is of shape <code>(batch_size, 28, 28, 1)</code>. How would I set up a loss function for G by hand?</p>

<p>Without the "G and D"-combined model "workaround" for this, I have to propagate the loss of D, which has an output shape of <code>(batch_size, 1)</code> to the output layer of G.</p>

<p>If G would do some classification for example, this wouldn't be that hard to figure out...but G outputs images. Thus, I can not directly map the loss of D to the output layer of G.</p>

<p>Do I have to set up a third model combining G+D? Or is there a way to calculate the loss for G by hand?</p>

<p>Any help is highly appreciated :)</p>