【问题标题】:TensorFlow InvalidArgumentError when feeding a placeholder馈送占位符时的TensorFlow InvalidArgumentError
【发布时间】:2018-02-02 15:39:05
【问题描述】:

我有这个占位符:

__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"]()]]

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

【问题讨论】:

    标签: python tensorflow placeholder


    【解决方案1】:

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

    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”提供明确的名称,因此获得了默认名称“占位符”,因此应按如下方式提供:

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

    【讨论】:

      【解决方案2】:

      问题很愚蠢,我没有喂__y:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-12
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多