tensorflow In python 3.x - 您必须使用 dtype float 为占位符张量 'Placeholder' 提供一个值

tensorflow In python 3.x - You must feed a value for placeholder tensor 'Placeholder' with dtype float

我在 python 3.6 中使用 tensorflow 代码。当我尝试这样做时,出现异常:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholderdtype=DT_FLOAT, shape=, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

import tensorflow as tf

x_data = [1.0, 2.0, 3.0]
y_data = [1.0, 2.0, 3.0]

W = tf.Variable(tf.random_uniform([1]), name='weight')
b = tf.Variable(tf.random_uniform([1]), name='bias')

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

hypothesis = W * X + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))

a = tf.Variable(0.1)

optimizer = tf.train.GradientDescentOptimizer(a)
train = optimizer.minimize(cost)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

for index in range(2001):
    sess.run(train, feed_dict={X: x_data, Y: y_data})
    if index % 20 == 0:
         print(index, sess.run(cost), sess.run(W), sess.run(b))

我认为问题出在最后一行 运行 session 的成本。 尝试用

替换最后一行
print(index, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W), sess.run(b))

这是因为 cost 需要获取 X 和 Y 数据才能找到假设,正如您在代码中指定的那样,可以使用 feed_dict 给出。我已经粘贴了您评估成本的行。

hypothesis = W * X + b 
cost = tf.reduce_mean(tf.square(hypothesis - Y))