为占位符

时间:2017-08-24 16:42:26

标签: python tensorflow placeholder

我有这个占位符:

__y = tf.placeholder(tf.int32)

然后我在下面的代码中使用它:

self.session.run(tf.global_variables_initializer())
self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.__y, logits=self.super_source))
opt =  tf.train.GradientDescentOptimizer(0.1).minimize(self.cost)
not_important, c = self.session.run(opt, feed_dict={labels: label})

我收到了这个错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype int32
     [[Node: Placeholder = Placeholder[dtype=DT_INT32, shape=<unknown>, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

我不明白我得到的错误,所以我无法解决我的问题。有人能解释我至少发生了什么吗?

2 个答案:

答案 0 :(得分:0)

如果我将您的示例重写为:

import tensorflow as tf

__y = tf.placeholder(tf.int32)

with tf.Session() as session:
    session.run(__y)

我遇到了同样的错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype int32
     [[Node: Placeholder = Placeholder[dtype=DT_INT32, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

这里的问题是,因为你没有为'__y'提供一个明确的名称,所以得到了默认名称'Placeholder',所以它应该按如下方式提供:

with tf.Session() as session:
   print(session.run(__y, feed_dict={__y: 123}))

答案 1 :(得分:0)

问题是愚蠢的,我没有喂__y:

not_important, c = self.session.run([optimizer, self.cost], feed_dict={self.__labels: label, self.__y: 3.0})