Python Conv2d在多类文本分类中的形状

Python Conv2d在多类文本分类中的形状,python,keras,deep-learning,cnn,Python,Keras,Deep Learning,Cnn,我正在使用卷积神经网络进行多类文本分类,我在手套嵌入权重上应用了以下代码,我得到了很好的结果,但我对CONV2D形状有一个问题: 为什么在conv2d1中我们得到conv_1(None,407,1,64),在conv_2中分别得到None,406,1,64)和conv_2:405 Layer (type) Output Shape Param # Connected to

我正在使用卷积神经网络进行多类文本分类,我在手套嵌入权重上应用了以下代码,我得到了很好的结果,但我对CONV2D形状有一个问题: 为什么在conv2d1中我们得到conv_1(None,407,1,64),在conv_2中分别得到None,406,1,64)和conv_2:405

               


Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_18 (InputLayer)           (None, 409)          0                                            
__________________________________________________________________________________________________
embedding_17 (Embedding)        (None, 409, 100)     1766600     input_18[0][0]                   
__________________________________________________________________________________________________
reshape_10 (Reshape)            (None, 409, 100, 1)  0           embedding_17[0][0]               
__________________________________________________________________________________________________
conv_1 (Conv2D)                 (None, 407, 1, 64)   19264       reshape_10[0][0]                 
__________________________________________________________________________________________________
conv_2 (Conv2D)                 (None, 406, 1, 64)   25664       reshape_10[0][0]                 
__________________________________________________________________________________________________
conv_3 (Conv2D)                 (None, 405, 1, 64)   32064       reshape_10[0][0]                 
__________________________________________________________________________________________________
max_pooling2d_16 (MaxPooling2D) (None, 1, 1, 64)     0           conv_1[0][0]                     
__________________________________________________________________________________________________
max_pooling2d_17 (MaxPooling2D) (None, 1, 1, 64)     0           conv_2[0][0]                     
__________________________________________________________________________________________________
max_pooling2d_18 (MaxPooling2D) (None, 1, 1, 64)     0           conv_3[0][0]                     
__________________________________________________________________________________________________
concatenate_6 (Concatenate)     (None, 3, 1, 64)     0           max_pooling2d_16[0][0]           
                                                                 max_pooling2d_17[0][0]           
                                                                 max_pooling2d_18[0][0]           
__________________________________________________________________________________________________
flatten_5 (Flatten)             (None, 192)          0           concatenate_6[0][0]              
__________________________________________________________________________________________________
dropout_11 (Dropout)            (None, 192)          0           flatten_5[0][0]                  
__________________________________________________________________________________________________
dense_11 (Dense)                (None, 3)            579         dropout_11[0][0]                 
==================================================================================================
Total params: 1,844,171
Trainable params: 1,844,171
Non-trainable params: 0
__________________________________________________________________________________________________
None

那没什么好担心的。这是任何核卷积运算的基本问题。在图像上卷积的内核大小会导致遗漏几个像素。下面是一个更详细地描述这个问题的例子


假设输入的形状是
感谢我添加padding=“same”,错误是:新数组的总大小必须保持不变。不管是文本还是图像,卷积都是自然现象。另外,我看到您正在使用conv2d,但仍然在单个维度上进行卷积。为什么不直接使用conv1d呢?这是文本分类中通常使用的方法。此外,您面临的问题更多地与此处使用的内核形状有点奇怪有关。\n与其重塑和添加新维度,不如使用conv1d。这将与您目前正在做的工作类似。非常感谢您能帮助我如何实现它吗?我是python和cnn的新手。。
from keras.layers import Dense, Input, GlobalMaxPooling1D
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.models import Model
from keras.layers import Input, Dense, Embedding, Conv2D, MaxPooling2D, Dropout,concatenate
from keras.layers.core import Reshape, Flatten
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from keras.models import Model
from keras import regularizers

sequence_length = 409
filter_sizes = [3,4,5]
num_filters = 64
drop = 0.5
EMBEDDING_DIM=100


embedding_layer = Embedding(len(tokenizer.word_index) + 1,
                            EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            trainable=True)


inputs = Input(shape=(sequence_length,))
embedding = embedding_layer(inputs)
reshape = Reshape((sequence_length,EMBEDDING_DIM,1))(embedding)

conv_0 = Conv2D(num_filters, (filter_sizes[0], EMBEDDING_DIM),activation='relu',kernel_regularizer=regularizers.l2(0.01),name='conv_1')(reshape)
conv_1 = Conv2D(num_filters, (filter_sizes[1], EMBEDDING_DIM),activation='relu',kernel_regularizer=regularizers.l2(0.01),name='conv_2')(reshape)
conv_2 = Conv2D(num_filters, (filter_sizes[2], EMBEDDING_DIM),activation='relu',kernel_regularizer=regularizers.l2(0.01),name='conv_3')(reshape)

maxpool_0 = MaxPooling2D((sequence_length - filter_sizes[0] + 1, 1), strides=(1,1))(conv_0)
maxpool_1 = MaxPooling2D((sequence_length - filter_sizes[1] + 1, 1), strides=(1,1))(conv_1)
maxpool_2 = MaxPooling2D((sequence_length - filter_sizes[2] + 1, 1), strides=(1,1))(conv_2)

merged_tensor = concatenate([maxpool_0, maxpool_1, maxpool_2], axis=1)
flatten = Flatten()(merged_tensor)
reshape = Reshape((3*num_filters,))(flatten)
dropout = Dropout(drop)(flatten)
output = Dense(units=3, activation='softmax',kernel_regularizer=regularizers.l2(0.01))(dropout)

# this creates a model that includes
model = Model(inputs, output)
adam = Adam(lr=1e-3)

model.compile(loss='categorical_crossentropy',
              optimizer=adam,
              metrics=['acc'])
print(model.summary())