InvalidArgumentError while coding MNIST tutorial

these are my first tensorflow steps, and I would like if somebody else had the same issues as me and if there is a way around it.

I am coding the mnist tutorial and my current code-snippet is:

#placeholder for input
x = tf.placeholder(tf.float32,[None,784]) # None means a dimension can be of any length

#Weights for the model: 784 pixel maps to ten results
W = tf.Variable(tf.zeros([784,10]))

#bias
b = tf.Variable( tf.zeros([10])) 

#implementing the model
y = tf.matmul(x,W) + b

#implementing cross-entropy
y_ = tf.placeholder(tf.float32,[None,10])

#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
cross_entropy = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

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

sess=tf.InteractiveSession()
tf.global_variables_initializer().run()

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
for _ in range(1000):
    batch_xs, batch_xy64 = mnist.train.next_batch(100)
    batch_xy = batch_xy64.astype(np.float32)
    sess.run(train_step , feed_dict={x:batch_xs,y:batch_xy})

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

print (sess.run(accuracy,feed_dict={x:mnist.test.images, y_:mnist.test.labels}))

First I tried cross_entropy from the MNIST description and the the one in the provided source-code, which made no difference.

Note that I explicitly try to cast the batch_xy, as it is returned as a float 64.

This also seems to be the problem, as in the session.run float32 tensors and variables seem to be expected.

As far as I saw debugging the code, the labes in the mnist are returned as float64 - perhaps that explains my error:

...
      File "/home/braunalx/python-workspace/LearnTensorFlow/firstSteps/MNIST_Start.py", line 40, in mnist_run
    y_ = tf.placeholder(tf.float32,[None,10])


     File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1548, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
...
    InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Is there any issue with the provided mnist data?


The error says that you didn't feed a value for a placeholder that is needed. Replace y with y_ on this line sess.run(train_step , feed_dict={x:batch_xs,y:batch_xy}) .

链接地址: http://www.djcxy.com/p/32090.html

上一篇: Tensorflow Python类型错误让我明白什么是错的

下一篇: 编写MNIST教程时出现InvalidArgumentError