我正在尝试构建一个 CNN,但遇到了 MaxPooling3D 层无法正常工作的问题。两层的输入形状均为 (1, 5, 32),我想使用 poolsize (1, 1, 32) 在深度上进行最大池化,因此输出的形状为 (1, 5, 1)。但是,这会引发错误:
ValueError: Input 0 of layer max_pooling3d is incompatible with the
layer: expected ndim=5, found ndim=4. Full shape received: [None, 1,
5, 32]
我不明白为什么预期/需要 5 的维度。如果我改为使用具有 poolsize (1,1) 的 MaxPooling2D 层,一切都会正确编译,我得到下面的模型。
> Model: "functional_1"
> __________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
> ==================================================================================================
input_1 (InputLayer) [(None, 5, 5, 1)] 0
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 5, 1, 32) 192 input_1[0][0]
__________________________________________________________________________________________________
conv2d (Conv2D) (None, 1, 5, 32) 192 input_1[0][0]
__________________________________________________________________________________________________
reshape (Reshape) (None, 1, 5, 32) 0 conv2d_1[0][0]
__________________________________________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 1, 5, 32) 0 conv2d[0][0]
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 1, 5, 32) 0 reshape[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate) (None, 1, 5, 64) 0 max_pooling2d[0][0]
max_pooling2d_1[0][0]
==================================================================================================
Total params: 384 Trainable params: 384 Non-trainable params: 0
__________________________________________________________________________________________________
Process finished with exit code 0
我用来构建这个的代码:
n=5
inp_similarity = Input(shape=(n, n, 1))
conv11 = Conv2D(32, (n, 1))(inp_similarity)
conv12 = Conv2D(32, (1, n))(inp_similarity)
reshape1 = Reshape((1,5,32))(conv12)
maxpl11 = MaxPooling2D((1, 1))(conv11)
maxpl12 = MaxPooling2D((1, 1))(reshape1)
merge1 = Concatenate()([maxpl11, maxpl12])
model = Model(inp_similarity, merge1)
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()