我正在尝试运行以下代码以对MNIST图像进行分类:
import random
import input_data
mnist = input_data.read_data_sets(raw_input(), raw_input(), raw_input())
import tensorflow as tf
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.05).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.initialize_all_variables().run()
for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
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}))
在我正在进行的练习中提供了前3行,我从Tensorflow教程here复制了其余的部分。
问题在于我正在
ValueError: Cannot feed value of shape (1000, 28, 28, 1) for Tensor u'Placeholder_1:0', which has shape (Dimension(None), Dimension(10))
在最后一行。
我认为问题出在mnist
对象上。它的类型为input_data.DataSets
,但我不知道它里面是什么,我不知道如何查看里面的内容。
如果我运行print(mnist.test.labels)
,我会收到[[[[0]
,这非常令人困惑。
那么任何想法如何修复此代码?