numpy - 将 dtype np.float32 提供给 TensorFlow 占位符

标签 numpy tensorflow

我试图将类型为 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.

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

最佳答案

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

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

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

关于numpy - 将 dtype np.float32 提供给 TensorFlow 占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39148628/

相关文章:

python - 快速内插网格数据

python - 类型错误 : src data type = 15 is not supported

tensorflow - 我已经安装了 CUDA 工具包,为什么 conda 又要安装 CUDA?

python - 在没有自定义对象的情况下保存完整的 tf.keras 模型?

python - TFRecordReader 在 session 关闭后保持文件锁定

tensorflow - 如何在 tensorflow 中的不同图中共享/重用层/变量?

python - 滚动窗口重新访问 - 添加窗口滚动数量作为参数 - 前瞻分析

python - 为什么比较两个分别包含一个 NumPy 对象的元组会报错?

python - 如何连接 matplotlib、scipy 等中的不连续曲线

Tensorflow 高误报率和非最大抑制问题