Reputation: 109
I'm a newbie for TensorFlow. I have been trying to feed MNIST in CSV form. Each row contains (label,pixel i-j,...) which i = row, j = column. Then, I am trying to train model based on the tutorials in TensorFlow website.
However, I got the error
ValueError: Cannot feed value of shape (28, 28) for Tensor u'Placeholder_12:0', which has shape '(?, 784)'
Could you please suggest me what is wrong with my code?
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}))
Thank you in advance.
Upvotes: 0
Views: 798
Reputation: 101
test_input = (np.asfarray(pixels[1:])).reshape(28,28)
Drop the reshape. Your x is of shape:
x = tf.placeholder(tf.float32, [None, 784])
Which does not compute:
sess.run(accuracy, feed_dict={x: test_input, y_: test_targets})
Upvotes: 0