【发布时间】:2016-10-25 09:07:10
【问题描述】:
我是 TensorFlow 的新手。我一直在尝试以 CSV 格式提供 MNIST。每行包含 (label,pixel i-j,...) 其中 i = 行,j = 列。然后,我尝试根据 TensorFlow 网站上的教程训练模型。
但是,我得到了错误
ValueError: 无法为形状为 '(?, 784)' 的张量 u'Placeholder_12:0' 提供形状 (28, 28) 的值
您能否建议我的代码有什么问题?
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}))
提前谢谢你。
【问题讨论】:
标签: python tensorflow