我是TensorFlow的新手。我一直试图以CSV形式提供MNIST。每行包含(标签,像素i-j,...),其中i =行,j =列。然后,我试图根据TensorFlow网站上的教程训练模型。
然而,我收到了错误
ValueError:无法为Tensor u' Placeholder_12:0'提供形状值(28,28),其形状为'(?,784)'
您能告诉我我的代码有什么问题吗?
import tensorflow as tf
import numpy as np
x_file = open("mnist_train_100.csv",'r')
x_list = x_file.readlines()
x_file.close()
output_nodes = 10
for r in x_list:
pixs = r.split(',')
inp = (np.asfarray(pixs[1:])).reshape(28,28)
targets = np.zeros(output_nodes)
targets[int(pixs[0])] = 1
test_file = open("mnist_test_10.csv",'r')
test_list = test_file.readlines()
test_file.close()
for i in test_list:
pixels = i.split(',')
test_input = (np.asfarray(pixels[1:])).reshape(28,28)
test_targets = np.zeros(output_nodes)
test_targets[int(pixels[0])] = 1
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10])) #weight
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)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train_step, feed_dict={x: inp, y_: targets})
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: test_input, y_: test_targets}))
提前谢谢。
答案 0 :(得分:0)
test_input = (np.asfarray(pixels[1:])).reshape(28,28)
放下重塑。你的x是形状:
x = tf.placeholder(tf.float32, [None, 784])
哪个不计算:
sess.run(accuracy, feed_dict={x: test_input, y_: test_targets})