2017-07-27 127 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)」的循環 –