2017-07-27 85 views
0

我已经创建了反映了TensorFlow的MNIST深为专家所描述的一个教程中here的脚本。TensorFlow张量不正确重塑

但是,当它试图将尺寸为[-1,28,28,1]的x张量重塑为尺寸[-1,28,28,1]时,我的脚本很早就返回了一个错误。我很困惑的教程并与然而成功同样的事情,它抛出下面的错误对我来说:

ValueError: Cannot feed value of shape (100, 784) for Tensor 'Reshape:0', which has shape '(?, 28, 28, 1)' 

完全我的Python脚本是在这里如下:

from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 
import tensorflow as tf 


x = tf.placeholder(dtype = tf.float32, shape = [None,784]) 
y_ = tf.placeholder(dtype = tf.float32, shape = [None, 10]) 


W1 = tf.Variable(tf.random_normal([5,5,1,32])) 
b1 = tf.Variable(tf.random_normal([32])) 

这是我怀疑发生错误:

x = tf.reshape(x,[-1,28,28,1]) 

output1 = tf.add(tf.nn.conv2d(x,W1, strides =[1,1,1,1], padding = "SAME"), b1) 
output1 = tf.nn.relu(output1) 
output1 = tf.nn.max_pool(output1, ksize = [1,2,2,1], strides = [1,2,2,1], padding = "SAME") 


W2 = tf.Variable(tf.random_normal([5,5,32,64])) 
b2 = tf.Variable(tf.random_normal([64])) 


output2 = tf.add(tf.nn.conv2d(output1,W2, strides = [1,1,1,1], padding = "SAME"), b2) 
output2 = tf.nn.relu(output2) 
output2 = tf.nn.max_pool(output2, ksize = [1,2,2,1], strides = [1,2,2,1], padding = "SAME") 
output2 = tf.reshape(output2, [-1, 7*7*64]) 



W_fc = tf.Variable(tf.random_normal([7*7*64,1024])) 
b_fc = tf.Variable(tf.random_normal([1024])) 


output3 = tf.add(tf.matmul(output2,W_fc), b_fc) 
output3 = tf.nn.relu(output3) 
output3 = tf.nn.dropout(output3, keep_prob = 0.85) 


W_final = tf.Variable(tf.random_normal([1024,10])) 
b_final = tf.Variable(tf.random_normal([10])) 


predictions = tf.add(tf.matmul(output3,W_final), b_final) 


cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels = y_ ,logits = predictions)) 
optimiser = tf.train.AdamOptimizer(1e-4).minimize(cost) 


sess = tf.InteractiveSession() 
tf.global_variables_initializer().run() 



for i in range(7000): 
    batchx_s,batchy_s = mnist.train.next_batch(100) 
    sess.run(optimiser, feed_dict = {x:batchx_s, y_:batchy_s}) 


with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for i in range(20000): 
     batch = mnist.train.next_batch(50) 
     optimiser.run(feed_dict={x: batch[0], y_: batch[1]}) 


correct_prediction = tf.equal(tf.argmax(predictions, 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})) 
+0

你能告诉在那里你打电话给会议的'run'方法的代码,包括'feed_dict'参数? – jdehesa

回答

1

我修正了自己的代码,它应能正常工作。

from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 
import tensorflow as tf 


x = tf.placeholder(dtype = tf.float32, shape = [None,784]) 
y_ = tf.placeholder(dtype = tf.float32, shape = [None, 10]) 


W1 = tf.Variable(tf.random_normal([5,5,1,32])) 
b1 = tf.Variable(tf.random_normal([32])) 

x_image = tf.reshape(x,[-1,28,28,1]) 

output1 = tf.add(tf.nn.conv2d(x_image,W1, strides =[1,1,1,1], padding = "SAME"), b1) 
output1 = tf.nn.relu(output1) 
output1 = tf.nn.max_pool(output1, ksize = [1,2,2,1], strides = [1,2,2,1], padding = "SAME") 


W2 = tf.Variable(tf.random_normal([5,5,32,64])) 
b2 = tf.Variable(tf.random_normal([64])) 


output2 = tf.add(tf.nn.conv2d(output1,W2, strides = [1,1,1,1], padding = "SAME"), b2) 
output2 = tf.nn.relu(output2) 
output2 = tf.nn.max_pool(output2, ksize = [1,2,2,1], strides = [1,2,2,1], padding = "SAME") 
output2 = tf.reshape(output2, [-1, 7*7*64]) 



W_fc = tf.Variable(tf.random_normal([7*7*64,1024])) 
b_fc = tf.Variable(tf.random_normal([1024])) 

output3 = tf.add(tf.matmul(output2,W_fc), b_fc) 
output3 = tf.nn.relu(output3) 
output3 = tf.nn.dropout(output3, keep_prob = 0.85) 


W_final = tf.Variable(tf.random_normal([1024,10])) 
b_final = tf.Variable(tf.random_normal([10])) 


predictions = tf.add(tf.matmul(output3,W_final), b_final) 


cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels = y_ ,logits = predictions)) 
optimiser = tf.train.AdamOptimizer(1e-4).minimize(cost) 
correct_prediction = tf.equal(tf.argmax(predictions, 1), tf.argmax(y_, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 



with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for i in range(20000): 
     batch = mnist.train.next_batch(50) 
     if i % 100 == 0: 
      train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1]}) 
      print('step %d, training accuracy %g' % (i, train_accuracy)) 
     optimiser.run(feed_dict={x: batch[0], y_: batch[1]}) 
    print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels,})) 

你的主要问题是线 -

x = tf.reshape(x,[-1,28,28,1]) 

output1 = tf.add(tf.nn.conv2d(x,W1, strides =[1,1,1,1], padding = "SAME"), b1) 

一个占位符的目的只是为了数据馈送到目标张量;它不应该像普通的张量一样对待。 我也除去变量初始值和运行优化冗余呼叫 -

sess = tf.InteractiveSession() 
tf.global_variables_initializer().run() 


for i in range(7000): 
    batchx_s,batchy_s = mnist.train.next_batch(100) 
    sess.run(optimiser, feed_dict = {x:batchx_s, y_:batchy_s}) 

上面看起来好像它们来自其他一些地方:) 最后,我添加了改编自一些打印语句说教程,它总是好的,知道你的训练是如何执行实时

+0

谢谢纠正工作 –

+0

谢谢纠正工作 –

0

的问题是,当你正在运行的图形,你是不是给输入值(mnist.test.images)到输入占位符,因为你已经分配的结果不同的操作来x(在你的代码中,我可以看到x = tf.reshape(x,[-1,28,28,1]),但可能有一些其他的分配别的地方,因为错误信息提示与形状(?, 10))的加法运算。 TensorFlow让你喂值在图中(不只是占位符)的任何张量,所以它认为你试图取代你输入一些中间结果。只需重命名Python变量保持输入张量像在其定义和run通话双方x_input,并注意不要在以后覆盖它。

+0

嗨,我重新运行它,发现它所指的形状错误是'(?,28,28,1)'。 –

+0

@spicyburrito当你创建了palceholder和你调用run时,你必须把'x'改成'x_input'。您仍然试图为重塑操作提供值,而不是占位符。 – jdehesa

+0

我已经但是它会引发同样的错误在“为我的range(7000)”的循环 –