不允许Keras MaxPooling3D

问题描述

我正在尝试构建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,1)]    0                
 __________________________________________________________________________________________________ 
conv2d_1 (Conv2D)               (None,32)     192         input_1[0][0]                    
 __________________________________________________________________________________________________ 
conv2d (Conv2D)                 (None,32)     192         input_1[0][0]                    
 __________________________________________________________________________________________________ 
reshape (Reshape)               (None,32)     0           conv2d_1[0][0]                   
 __________________________________________________________________________________________________ 
max_pooling2d (MaxPooling2D)    (None,32)     0           conv2d[0][0]                     
 __________________________________________________________________________________________________ 
max_pooling2d_1 (MaxPooling2D)  (None,32)     0           reshape[0][0]                    
 __________________________________________________________________________________________________ 
concatenate (Concatenate)       (None,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,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()

解决方法

您的目标是在特征维上进行“池化” ...这不是池化层的范围...它们仅在空间维上进行池化。您需要更简单的东西

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,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,1)]    0                                            
__________________________________________________________________________________________________
conv2d_35 (Conv2D)              (None,1,32)     192         input_4[0][0]                    
__________________________________________________________________________________________________
conv2d_34 (Conv2D)              (None,32)     192         input_4[0][0]                    
__________________________________________________________________________________________________
reshape_2 (Reshape)             (None,32)     0           conv2d_35[0][0]                  
__________________________________________________________________________________________________
lambda_6 (Lambda)               (None,1)      0           conv2d_34[0][0]                  
__________________________________________________________________________________________________
lambda_7 (Lambda)               (None,1)      0           reshape_2[0][0]                  
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None,2)      0           lambda_6[0][0]                   
                                                                 lambda_7[0][0]                   
==================================================================================================