不允许 Keras MaxPooling3D

Keras MaxPooling3D not allowed

我正在尝试构建 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 维是 expected/required。如果我改用池大小为 (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()

您的目标是在特征维度上操作 'pooling'...这不是池化层的范围...它们仅在空间维度上操作池化。你需要更简单的东西

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 = Lambda(lambda x: tf.reduce_max(x, axis=-1, keepdims=True))(conv11)
maxpl12 = Lambda(lambda x: tf.reduce_max(x, axis=-1, keepdims=True))(reshape1)

merge1 = Concatenate()([maxpl11, maxpl12])

model = Model(inp_similarity, merge1)
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model.summary()

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_4 (InputLayer)            [(None, 5, 5, 1)]    0                                            
__________________________________________________________________________________________________
conv2d_35 (Conv2D)              (None, 5, 1, 32)     192         input_4[0][0]                    
__________________________________________________________________________________________________
conv2d_34 (Conv2D)              (None, 1, 5, 32)     192         input_4[0][0]                    
__________________________________________________________________________________________________
reshape_2 (Reshape)             (None, 1, 5, 32)     0           conv2d_35[0][0]                  
__________________________________________________________________________________________________
lambda_6 (Lambda)               (None, 1, 5, 1)      0           conv2d_34[0][0]                  
__________________________________________________________________________________________________
lambda_7 (Lambda)               (None, 1, 5, 1)      0           reshape_2[0][0]                  
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 1, 5, 2)      0           lambda_6[0][0]                   
                                                                 lambda_7[0][0]                   
==================================================================================================