TensorFlow张量不能正确地重塑

时间:2017-07-27 09:09:30

标签: python tensorflow conv-neural-network

我创建了一个脚本,该脚本反映了TensorFlow的 Deep MNIST for Experts 教程{@ 3}}中描述的脚本。

但是,当我尝试将x张量从维度[-1,28,28,1]重新整形为维度[-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}))

2 个答案:

答案 0 :(得分: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})

以上看起来好像来自其他地方:) 最后,我添加了一些改编自上述教程的打印语句,总是很好地了解您的培训实时执行情况

答案 1 :(得分:0)

问题在于,当您运行图表时,您没有将输入值(mnist.test.images)提供给输入占位符,因为您已将不同操作的结果分配给x(在我的代码中,我可以看到x = tf.reshape(x,[-1,28,28,1]),但可能在其他地方还有其他一些赋值,因为错误消息建议使用形状为(?, 10)的加法运算。 TensorFlow允许您为图中的任何张量(而不仅仅是占位符)提供值,因此它认为您正在尝试用输入替换某些中间结果。只需在输入张量和x_input调用中将包含输入张量的Python变量重命名为run,并注意不要在以后覆盖它。