简体   繁体   中英

Dimensions must be equal, but are 64 and 1 for 'Conv2D_13' (op: 'Conv2D') with input shapes: [?,28,28,64], [3,3,1,64]

I am doing data Digital recognition in kaggle by using cnn. I am getting this error:

Dimensions must be equal, but are 64 and 1 for 'Conv2D_13' (op: 'Conv2D') with input shapes: [?,28,28,64], [3,3,1,64].

I don't know why and the output shape different from my # symbol. For example, I think it should be (26,26,24) but the result actually is (?, 28, 28, 64). if you know. please tell me. thanks!

My code is:

X=tf.placeholder(tf.float32,(None,28,28,1),"X")
y=tf.placeholder(tf.int32,(None,10),"y")

#conv1
filter1=tf.Variable(tf.truncated_normal((3,3,1,64)))
x=tf.nn.relu(tf.nn.conv2d(X,filter1,strides[1,1,1,1],padding="SAME"),name='relu_1') 
#(26,26,24)
print(x.shape) #(?, 28, 28, 64) 
filter2=tf.Variable(tf.truncated_normal((3,3,1,64)))
x=tf.nn.relu(tf.nn.conv2d(x,filter2,strides[1,1,1,1],padding="SAME"),name='relu_2') 
#(24,24,64)
x = tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1],padding="VALID") 
#(12,12,64)
filter3=tf.Variable(tf.truncated_normal((3,3,1,128)))
x=tf.nn.relu(tf.nn.conv2d(x,filter3,strides=[1,1,1,1],padding="SAME"),name='relu_3') 
#(10,10,128)
filter4=tf.Variable(tf.truncated_normal((3,3,1,128)))
x=tf.nn.relu(tf.nn.conv2d(x,filter4,strides=[1,1,1,1],padding="SAME"),name='relu_4')
#(8,8,128)
x = tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1],padding="VALID")                 
#(4,4,128)
fc_weight=tf.Variable(tf.truncated_normal((16*128,10)))
logits= tf.add(tf.matmul(x, fc_weight), name = 'logits')
y_proba=tf.nn.softmax(logits)

#cost
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=logits)
loss = tf.reduce_mean(xentropy, name="loss")

#optimizer
lr = tf.placeholder(dtype=tf.float32, name="lr")
optimizer = tf.train.AdamOptimizer(lr)
training_op = optimizer.minimize(loss)

#evaluation
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

def shuffle_batch(X, y, batch_size):
    rnd_idx = np.random.permutation(len(X))
    n_batches = len(X) // batch_size
    for batch_idx in np.array_split(rnd_idx, n_batches):
        X_batch, y_batch = X[batch_idx], y[batch_idx]
        yield X_batch, y_batch

n_epochs = 5
batch_size = 68

init = tf.global_variables_initializer()
with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
        accuracy_val = accuracy.eval(feed_dict={X: X_valid, y: y_valid})
        print(epoch, "Validation accuracy:", accuracy_val)

I know the reason. because padding="SAME" so the shape unchange. the shape of filter I set is missed. for example: filter1 s shape is (3,3,1,64). filter2 s shape is (3,3,1,64). filter2 s shape is (3,3,64,64)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM