![](/img/trans.png)
[英]Dimensions must be equal, but are 1 and 64 for 'Conv2D_1' (op: 'Conv2D') with input shapes: [?,24,24,1], [5,5,64,64]
[英]Dimensions must be equal, but are 64 and 32 for 'Conv2D_2' (op: 'Conv2D') with input shapes: [?,7,7,64], [5,5,32,64]
這是一個5個卷積層代碼:
# Template program with one convolution layer and one fully connected hidden layer,
# dropout and Adam Optimizer
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
# xi is an image of size n. yi is the N labels of the image
# X is mxn. Row xi of X is an image
# Y is mxN. Row yi of Y is the labels of xi
X = tf.placeholder(tf.float32, shape=[None, 784])
Y = tf.placeholder(tf.float32, shape=[None, 10])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(X, W):
return tf.nn.conv2d(X, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(X):
return tf.nn.max_pool(X, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
# First Max Pool layer -- to resize the image to half of the image size
orig_image = tf.reshape(X, [-1,28,28,1])
h_pool0 = max_pool_2x2(orig_image)
### End of first max pool layer ###
# First Convolutional Layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(h_pool0, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
# Second Convolutional Layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
# Third Convolutional Layer
W_conv3 = weight_variable([5, 5, 64, 128])
b_conv3 = bias_variable([128])
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)
# Fourth Convolutional Layer
W_conv3 = weight_variable([5, 5, 128, 256])
b_conv3 = bias_variable([256])
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)
# Fifth Convolutional Layer
W_conv3 = weight_variable([5, 5, 256, 512])
b_conv3 = bias_variable([512])
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)
# Densely Connected Layer
W_fc1 = weight_variable([7 * 7 * 512, 4096])
b_fc1 = bias_variable([4096])
h_pool2_flat = tf.reshape(h_conv3, [-1, 7*7*512])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# Dropout
keep_rate = 0.8
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_rate)
# Readout Layer
W_fc2 = weight_variable([4096, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=y_conv))
#train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy)
train_step = tf.train.AdamOptimizer().minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch = mnist.train.next_batch(100)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
X:batch[0], Y: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={X: batch[0], Y: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
X: mnist.test.images, Y: mnist.test.labels, keep_prob: 1.0}))
而且我不確定為什么,但是每次運行它時,我都會收到此錯誤消息:
Traceback (most recent call last):
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _call_cpp_shape_fn_impl
input_tensors_as_shapes, status)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__
next(self.gen)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 64 and 32 for 'Conv2D_2' (op: 'Conv2D') with input shapes: [?,7,7,64], [5,5,32,64].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Inshal/Desktop/Inshal Haq/UTD/Spring 2017/Computer Vision/Project2/mnist3.py", line 57, in <module>
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)
File "C:/Users/Inshal/Desktop/Inshal Haq/UTD/Spring 2017/Computer Vision/Project2/mnist3.py", line 25, in conv2d
return tf.nn.conv2d(X, W, strides=[1, 1, 1, 1], padding='SAME')
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 403, in conv2d
data_format=data_format, name=name)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
op_def=op_def)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2338, in create_op
set_shapes_for_outputs(ret)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1719, in set_shapes_for_outputs
shapes = shape_func(op)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1669, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "C:\Users\Inshal\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 676, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 64 and 32 for 'Conv2D_2' (op: 'Conv2D') with input shapes: [?,7,7,64], [5,5,32,64].
有誰知道我的代碼有什么問題,如何解決?
感謝您的輸入!
您在每層中的過濾器大小都有問題,應該打印每層的形狀以進行跟蹤和調試,在每個操作之后使用get_shape()
,例如:
h_conv1 = tf.nn.relu(conv2d(h_pool0, W_conv1) + b_conv1)
print h_conv1.get_shape()
h_pool1 = max_pool_2x2(h_conv1)
print h_pool1.get_shape()
# Second Convolutional Layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
print h_conv2.get_shape()
然后,您可以了解出了什么問題。
阿里的答案是調試問題的好方法。 您的特定問題是您的代碼中有錯字---您在第三卷積層中使用了錯誤的權重。
# Third Convolutional Layer
W_conv3 = weight_variable([5, 5, 64, 128])
b_conv3 = bias_variable([128])
# You wrote
# h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv2) + b_conv2)
# when you probably meant:
h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3) + b_conv3)
希望有幫助!
您使用相同的層名稱錯誤地在第4層和第5層中執行“ conv2d”操作。 在此處輸入圖片說明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.