2016-11-07 115 views
6

我正在TensorFlow中創建一個簡單的玩具示例,並且遇到了一個奇怪的錯誤。我已經定義兩個佔位符如下:如何使用numpy數組提供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]是大小的兩個numpy的ndarrays(100,2) [打印出各自的尺寸覈實]

當我嘗試運行模式,我得到的錯誤:

"InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float"

由上面定義的佔位符「x」引起的

任何人都可以告訴我做錯了什麼嗎?我已經瀏覽了幾個在線的例子,它似乎應該工作...有沒有另一種方式來提供numpy數組值的佔位符?

如果有幫助,我正在使用Ubuntu,SCL和Python 2.7,並安裝了所有標準numpy和tensorflow軟件包。

+0

爲了幫助調試,您可以爲所有張量和操作命名,例如,通過執行'x = tf.placeholder(...,name ='x')'。那麼錯誤信息就不那麼神祕了。 – sunside

+0

你的代碼應該已經工作了,我在我的機器上做了同樣的事情,即用'numpy.ndarray'給相同類型('float32')提供'tf.placeholder'。如果你給出了整個錯誤信息,這將有所幫助。 –

回答

4

沒有你的整個代碼,它很難準確回答。 我試圖重現你在一個玩具的例子中描述的,它的工作。

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}))