我很难用张量填充占位符。我已搜索但只能找到将单个值提供到计算图中的值。
这是我的代码:
import tensorflow as tf
import numpy as np
features = [ [0,0], [0,1], [1,0], [1,1] ]
labels = [ 0, 1, 1, 0]
x = tf.placeholder(tf.float32, [2, 1], name="inputs")
W = tf.Variable(np.random.rand(1, 4), dtype=tf.float32, name="hidden1")
b = tf.Variable(np.ones((2,4)), name="b", dtype=tf.float32)
with tf.Session() as sess:
input_data = np.random.rand(2, 1)
feed_dict = {
x:input_data
}
sess.run(tf.global_variables_initializer())
sess.run(feed_dict)
print(x.eval(session=sess))
给了我以下错误:
has invalid type <class 'numpy.ndarray'>, must be a string or Tensor.
我也尝试将占位符定义为tf.Constant:
input_data = tf.constant([[0],[1]], dtype=tf.float32)
这给了我一个不同的错误:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'inputs_54' with dtype float and shape [2,1]
[[Node: inputs_54 = Placeholder[dtype=DT_FLOAT, shape=[2,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
我只想提供像[[0],[1]这样的2 x 1矩阵,但事实证明这很难。
非常感谢任何帮助。
答案 0 :(得分:1)
为了评估张量x
,它必须是sess.run
的一部分,
你需要打电话,
print(sess.run(x,{x:np.array([[0],[1]])}))
#x is the tensor you want to execute,
#feed_dict: feeds the input to the placeholder