Numpy 将数据类型np.float32馈送到TensorFlow占位符

Numpy 将数据类型np.float32馈送到TensorFlow占位符,numpy,tensorflow,Numpy,Tensorflow,我试图将类型为float32的numpy ndarray提供给TensorFlow占位符,但它给了我以下错误: You must feed a value for placeholder tensor 'Placeholder' with dtype float TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings,

我试图将类型为float32的numpy ndarray提供给TensorFlow占位符,但它给了我以下错误:

You must feed a value for placeholder tensor 'Placeholder' with dtype float
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
我的席位持有人定义为:

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})
其中,我的批处理x和批处理y是数据类型(“float32”)的numpy ndarray。以下是我使用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)})
给出以下错误:

You must feed a value for placeholder tensor 'Placeholder' with dtype float
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
我应该如何输入占位符?我应该使用什么类型的?
任何帮助/建议都将不胜感激

对于第一个问题,您确定
batch_y
也是
float32
?您只提供
batch_x
类型的跟踪,
batch_y
更可能是整数,因为它似乎是类的一个热门编码

对于第二个问题,您的错误是在常规numpy数组上使用
tf.to_float
,这是一种张量运算。您应该使用numpy cast intstead:

sess.run(optimizer, feed_dict={x: batch_x.astype(np.float32), y: batch_y.astype(np.float32)})

你所说的似乎是对的。你介意试试小一点的程序吗?例如,以下工作是否有效
z=tf.reduce_sum(x)+tf.reduce_sum(y);sess.run(z,feed_dict={x:np.zeros([1,10,13]),y:np.zeros([11201]))