2017-02-10 28 views
2

我想通過x_data作爲feed_dict但得到低於錯誤,我不確定在代碼中出現了什麼問題。Tensorflow:與一維數據的形狀不匹配問題

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x_12' with dtype int32 and shape [1000] 
    [[Node: x_12 = Placeholder[dtype=DT_INT32, shape=[1000], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

我的代碼:

import tensorflow as tf 
import numpy as np 
model = tf.global_variables_initializer() 
#define x and y 
x = tf.placeholder(shape=[1000],dtype=tf.int32,name="x") 
y = tf.Variable(5*x**2-3*x+15,name = "y") 
x_data = tf.pack(np.random.randint(0,100,size=1000)) 
print(x_data) 
print(x) 
with tf.Session() as sess: 
    sess.run(model) 
    print(sess.run(y,feed_dict={x:x_data})) 

我檢查了xx_data的形狀和它是一樣

Tensor("pack_8:0", shape=(1000,), dtype=int32) 
Tensor("x_14:0", shape=(1000,), dtype=int32) 

我與一個維數據的工作。 任何幫助表示讚賞,謝謝!

回答

1

爲了使它工作,我改變了兩件事,首先我將y更改爲Tensor。其次我沒有更改x_dataTensor,如評論here

可選feed_dict參數允許呼叫者覆蓋在圖形張量的值。 feed_dict中的每個鍵可以是以下類型之一:

如果鍵是一個張量,則該值可以是可以轉換爲與該張量相同的dtype的Python標量,字符串,列表或numpy ndarray。此外,如果鍵是佔位符,則將檢查值的形狀是否與佔位符兼容。

更改後的代碼工作對我來說:

import tensorflow as tf 
import numpy as np 
model = tf.global_variables_initializer() 
#define x and y 
x = tf.placeholder(shape=[1000],dtype=tf.int32,name="x") 
y = 5*x**2-3*x+15 # without tf.Variable, making it a tf.Tensor 
x_data = np.random.randint(0,100,size=1000) # without tf.pack 
print(x_data) 
print(x) 
with tf.Session() as sess: 
    sess.run(model) 
    print(sess.run(y,feed_dict={x:x_data}))