如何使用numpy数组提供Tensorflow占位符?

Pat*_*ger 11 python arrays numpy tensorflow

我正在TensorFlow中创建一个简单的玩具示例,我遇到了一个奇怪的错误.我有两个占位符定义如下:

x = tf.placeholder(tf.float32, shape=[None,2]) [two-parameter input]

y_ = tf.placeholder(tf.float32, shape=[None,2]) [one-hot labels]
Run Code Online (Sandbox Code Playgroud)

我稍后尝试使用定义为的feed_dict为这些占位符提供:

feed_dict={x: batch[0].astype(np.float32), y_: batch[1].astype(np.float32)}
Run Code Online (Sandbox Code Playgroud)

哪里batch[0]batch[1]都是大小的numpy ndarray(100,2)[通过打印各自的大小验证]

当我尝试运行模型时,我收到错误:

"InvalidArgumentError:您必须使用dtype float为占位符张量'占位符'提供值"

由我在上面定义的占位符"x"引起的

谁能说出我做错了什么?我在网上查看了几个例子,看起来这应该有用......有没有另一种方法来为占位符提供来自numpy数组的值?

如果它有帮助,我在Ubuntu,SCL和Python 2.7中工作,我安装了所有标准的numpy和tensorflow包.

Thi*_*eur 10

没有你的整个代码,很难准确回答.我试图重现你在玩具示例中描述的内容并且它有效.

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}))
Run Code Online (Sandbox Code Playgroud)