Keras:为什么 Conv2D 层的输出大小与特征图的预期形状不匹配?

Keras: Why the output size of a Conv2D layer doesn't match the expected shape of feature map?

我正在尝试 运行 MNIST 图像(28x28 灰度图像)上的卷积神经网络作为输入。为此,我正在使用 Keras(代码如下所示)。我无法理解的是为什么卷积层的形状与我期望的形状不匹配。

特别是第一个卷积层,我尝试使用的是 32 个特征图,将它们全部与 3x3 内核进行卷积。我使用的批量大小是 200。

鉴于此,我预计第一个 Conv2D 层的形状是 (200, 26, 26, 32) 即处理了 200 个样本(由于批量大小), 32 个特征图,其大小为 26x26(由于卷积应用于具有 3x3 内核的 28x28 输入图像)。

但是,我使用 model.summary() 得到的大小是 (200, 9, 9, 32) 如下所示:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (200, 9, 9, 32)           320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (200, 4, 4, 32)           0         
_________________________________________________________________
dropout_1 (Dropout)          (200, 4, 4, 32)           0         
_________________________________________________________________
flatten_1 (Flatten)          (200, 512)                0         
_________________________________________________________________
dense_2 (Dense)              (200, 256)                131328    
_________________________________________________________________
dense_3 (Dense)              (200, 10)                 2570      
=================================================================
Total params: 134,218
Trainable params: 134,218
Non-trainable params: 0
_________________________________________________________________

我使用的代码是:

%tensorflow_version 2.x
# MNIST
from tensorflow.keras.datasets import mnist
from keras.utils import np_utils
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, Dropout, Flatten, Dense

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(X_train.shape[0],28,28,1).astype('float32')
X_test  = X_test.reshape(X_test.shape[0],28,28,1).astype('float32')

X_train = X_train/255
X_test  = X_test/255

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

model = Sequential()
model.add(Convolution2D(32,3,3,activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256,activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

history = model.fit(X_train, y_train, validation_data = (X_test, y_test), epochs = 10, batch_size = 200, verbose = 2)

model.summary()

提前致谢!

这是由于Convolution2D(32, 3, 3)是一个卷积层,输出有32个特征图,内核大小为3x3,步长为3

你的意思可能是 Convolution2D(32, (3, 3)) 或者只是 Convolution2D(32, 3).