简体   繁体   中英

feed placeholder traceback in tensorflow: 'tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor

Why does the following piece of code throw an exception? I feed the placeholder via feed_dict= in the for loop, but when I print the values of x or y_tensor , I get the following error message:

'tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input' with dtype float and shape [?,5]'.

Surprisingly, I don't get the error for a certain tensor when I print another one. For instance, when I only print y_tensor , I don't get the error message for the line where I declared x , and the opposite. I use the range of 10 only for testing the issue. The main problem is that the optimization can't work under this condition. How do I fix this?

Here The Code:

x = tf.placeholder(tf.float32, [None, 5], name='input')

W = tf.Variable(tf.zeros([5,1]))
b = tf.Variable(tf.zeros([1]))

y = tf.nn.softmax(tf.matmul(x, W) + b) 


y_tensor = tf.placeholder(tf.float32, [None, 1], name='output')
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_tensor * tf.log(y), reduction_indices=[1]))

optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)


session = tf.Session()
init = tf.global_variables_initializer()
session.run(init)

for i in range(10):
  batch_xs = [dataA[i], dataB[i], dataC[i], dataD[i],
              dataE[i]]]
  batch_ys = [[dataG[i]]]
  session.run(optimizer ,feed_dict={x: batch_xs, y_tensor: batch_ys})

print(session.run(y))

Because y depends on x . Therefore you need to change the last line to:

print(session.run(y, ,feed_dict={x: xs_test}))

where xs_test is your test data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM