1

我正在尝试构建一个 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()
4

1 回答 1

2

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

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]                   
==================================================================================================
于 2020-09-08T09:53:24.880 回答