我正在使用cnn在kaggle中进行数据数字识别。 我收到此错误:
尺寸必须相等,但输入形状为[?,28,28,64],[3,3,1,64]的'Conv2D_13'(op:'Conv2D')的尺寸必须为64和1。>
我不知道为什么,输出形状与#符号不同。例如,我认为应该是(26,26,24),但结果实际上是(?,28,28,64)。 如果你知道的话。请告诉我。谢谢!
我的代码是:
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)
答案 0 :(得分:0)
我知道原因。
因为padding =“ SAME”所以形状不变。我设置的过滤器形状丢失了。
例如:
filter1 s shape is (3,3,1,64). filter2
的形状为(3,3,64,64)