【问题标题】:TensorFlow: Cannot feed value of shape when using CSV file as inputTensorFlow:使用 CSV 文件作为输入时无法提供形状值
【发布时间】: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


    【解决方案1】:
    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})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2017-12-11
      相关资源
      最近更新 更多