【问题标题】:Feeding dtype np.float32 to TensorFlow placeholder将 dtype np.float32 输入到 TensorFlow 占位符
【发布时间】:2017-01-02 02:00:11
【问题描述】:

我正在尝试将类型为 : 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 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)})

给出以下错误:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

我应该如何提供占位符?我应该使用什么类型的? 任何帮助/建议将不胜感激!

【问题讨论】:

  • 你所拥有的似乎是正确的。你介意尝试一个更小的程序吗?例如,以下是否有效? z = tf.reduce_sum(x) + tf.reduce_sum(y); sess.run(z, feed_dict={x:np.zeros([1, 10, 13]), y:np.zeros([1, 1201])})

标签: numpy tensorflow


【解决方案1】:

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

对于第二个问题,你做错的是你在一个常规的 numpy 数组上使用了tf.to_float,这是一个张量操作。您应该使用 numpy cast 代替:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多