简体   繁体   中英

InvalidArgumentError in tensorflow (softmax mnist)

When I was trying to accomplish softmax regression with tensorflow, some problems occurred as below:

tensorflow.python.framework.errors_impl.InvalidArgumentError:
You must feed a value for placeholder tensor 'Placeholder_1' with dtype float [[Node: Placeholder_1 = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

From above description, I understand that the problem is an argument type error. But in my code, the type of my data is same as the placeholder.

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

m = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, [None, 784])
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, w)+b)
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
tf.global_variables_initializer()

for i in range(1000):
    batch_xs, batch_ys = m.train.next_batch(100)
    train_step.run({x: batch_xs, y: batch_ys})

correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: m.test.images, y: m.test.labels}))

I think the problem is caused by the type of batch_xs(float32) and batch_ys(float32).

Any suggestions on how to solve this?

The problem is caused by the fact that you're passing y instead of y_ into the feed_dict of the accuracy.eval call.

In this way, you're overwriting the value of y and your placeholder y_ is not used.

Just change the line to

print(accuracy.eval({x: m.test.images, y_: m.test.labels}))

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