Search code examples
pythontensorflowgenerative-adversarial-network

Confused on tensorflow feed_dict problem with value 'z' not feed


Recently, I write Tensorflow code by myself, however, when I use feed_dict to get the real value with Tensor object and I meet such problem.

I first define placeholder such as self.z and self.G as the following. The discriminator is a neural network.

    self.z = tf.placeholder(
        tf.float32, [None, self.z_dim], name='z')
    self.z_sum = histogram_summary("z", self.z)

    self.G = tf.placeholder(tf.float32, [self.batch_size] + image_dims, name='Generated_picture')
    self.real = self.discriminator(inputs)
    self.fake = self.discriminator(self.G, reuse=True)
    self.d_loss = tf.reduce_mean(tf.log(1 + tf.exp(-self.real)) + tf.log(1 + tf.exp(self.fake)))
    self.real_sum = histogram_summary("real", self.real)
    self.fake_sum = histogram_summary("fake", self.fake)
    self.d_loss_sum = histogram_summary("d_loss", self.d_loss)
    self.d_sum = merge_summary([self.z_sum, self.d_loss_sum, self.real_sum, self.fake_sum])

I try to update my discriminator as the following.

generated_images = self.generator(self.z)
index = np.random.choice(self.batch_size*10, size=config.batch_size)
generated_images_real = self.sess.run(generated_images, feed_dict={self.z: self.sz[index]})
_, summary_str = self.sess.run([d_optim, self.d_sum],feed_dict={
                                                       self.inputs: batch_images,
                                                       self.G: generated_images_real,
                                                       self.z: self.sz[index],

                                                   })

In this situation, I am not sure why I have to feed value for self.z. I believe that self.G only depends on generated_images_real which is a real value vector. I am so confused about that. Thank you every one.


Solution

  • The object self.z is atf.placeholder. If you execute an operation in your session that depends on this placeholder, than tensorflow needs a value for this placeholder to execute the the real calculations.

    Lets look into the operations you run: generated_images_real = self.sess.run(generated_images... and self.sess.run([d_optim, self.d_sum] ,...

    From the definition of self.d_sum we see that it depends on self.z_sum which in turn depends on self.z - our placeholder. Therefore we have to provide a value for this placeholder, if the operation self.d_sum is executed. The operation d_optim might also depend on self.z but its definition is not given here. This explains why we need a value for self.z in the second statement.

    In the first statement, generated_images depends directly on self.z, as this placeholder is passed to the self.generator function.