我试图将类型为:float32的numpy ndarray提供给TensorFlow占位符,但它给了我以下错误:
You must feed a value for placeholder tensor 'Placeholder' with dtype float
我的占位符定义为:
n_steps = 10
n_input = 13
n_classes = 1201
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
它给我上述错误的一行是:
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
其中我的batch_x和batch_y是dtype('float32')的numpy ndarrays . 以下是我使用pdb打印的类型:
(Pdb)batch_x.dtype
dtype('float32')
(Pdb)x.dtype
tf.float32
我也尝试过类型转换batch_x和batch_y到tf.float32,因为看起来x是dtype tf.float32但运行带有类型转换的代码:
sess.run(optimizer, feed_dict={x: tf.to_float(batch_x), y: tf.to_float(batch_y)})
给出以下错误:
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
我该如何喂占位符?我应该使用什么类型的?任何帮助/建议将不胜感激!
1 回答
对于你的第一个问题,你确定
batch_y
也是float32
吗?您只提供batch_x
类型的跟踪,而batch_y
更可能是整数,因为它似乎是您的类的一个热门编码 .对于第二个问题,你做错了是你在常规numpy数组上使用
tf.to_float
,这是一个张量操作 . 你应该使用numpy cast intstead: