我正致力于在 TensorFlow 中创建一个简单的玩具示例,但我遇到了一个奇怪的错误。我有两个占位符定义如下:
x = tf.placeholder(tf.float32, shape=[None,2]) [two-parameter input]
y_ = tf.placeholder(tf.float32, shape=[None,2]) [one-hot labels]
我稍后尝试使用定义为的 feed_dict 来提供这些占位符:
feed_dict={x: batch[0].astype(np.float32), y_: batch[1].astype(np.float32)}
其中 batch[0]
和 batch[1]
都是大小为 (100,2) 的 numpy ndarrays [通过打印出它们各自的大小来验证]
当我尝试运行模型时,出现错误:
"InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float"
由我上面定义的占位符“x”引起
谁能告诉我做错了什么?我已经在线查看了几个示例,看起来这应该可行...是否有另一种方法可以使用 numpy 数组中的值来填充占位符?
如果有帮助,我正在使用 Ubuntu、SCL 和 Python 2.7,并且我安装了所有标准的 numpy 和 tensorflow 包。
最佳答案
没有您的完整代码,很难准确回答。 我试图重现您在玩具示例中描述的内容,并且成功了。
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=[None, 2])
y_ = tf.placeholder(tf.float32, shape=[None, 2])
loss = tf.reduce_sum(tf.abs(tf.sub(x, y_)))#Function chosen arbitrarily
input_x=np.random.randn(100, 2)#Random generation of variable x
input_y=np.random.randn(100, 2)#Random generation of variable y
with tf.Session() as sess:
print(sess.run(loss, feed_dict={x: input_x, y_: input_y}))
关于python - 如何为 Tensorflow 占位符提供 numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40470065/