简体   繁体   English

将卷积层添加到 CNN 以进行 NLP 分析

[英]Adding Convolutional Layer to CNN for NLP analysis

I am facing a problem regarding adding Convolutional Layer to a CNN.我面临一个关于将卷积层添加到 CNN 的问题。 With the help of various blog posts, I rebuild a CNN for my purposes.在各种博客文章的帮助下,我为我的目的重建了一个 CNN。 My code looks like that at the moment:我的代码现在看起来像这样:

def ConvNet(embeddings, max_sequence_length, num_words, embedding_dim, trainable=False, extra_conv=True):
    embedding_layer = Embedding(num_words,
                                embedding_dim,
                                weights=[embeddings],
                                input_length=max_sequence_length,
                                trainable=trainable)

    sequence_input = Input(shape=(max_sequence_length,), dtype='int32')
    embedded_sequences = embedding_layer(sequence_input)

    convs = []
    filter_sizes = [3, 4, 5]

    for filter_size in filter_sizes:
        l_conv = Conv1D(filters=128, kernel_size=filter_size, activation='relu')(embedded_sequences)
        l_pool = MaxPooling1D(pool_size=3)(l_conv)
        convs.append(l_pool)

    l_merge = concatenate([convs[0], convs[1], convs[2]], axis=1)

    # add a 1D convnet with global maxpooling, instead of Yoon Kim model
    conv = Conv1D(filters=128, kernel_size=3, activation='relu')(embedded_sequences)
    pool = MaxPooling1D(pool_size=3)(conv)

    if extra_conv == True:
        x = Dropout(0.5)(l_merge)
    else:
        x = Dropout(0.5)(pool)
    x = Flatten()(x)
    x = Dense(128, activation='relu')(x)
    preds = Dense(1, activation='linear')(x)

    model = Model(sequence_input, preds)
    model.compile(loss='mean_squared_error',
                  optimizer='adadelta',
                  metrics=['mean_squared_error'])
    model.summary()
    return model

The resulting model architecture is like that:生成的 model 架构是这样的:

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 1086)         0                                            
__________________________________________________________________________________________________
embedding_1 (Embedding)         (None, 1086, 300)    532500      input_1[0][0]                    
__________________________________________________________________________________________________
conv1d_1 (Conv1D)               (None, 1084, 128)    115328      embedding_1[0][0]                
__________________________________________________________________________________________________
conv1d_2 (Conv1D)               (None, 1083, 128)    153728      embedding_1[0][0]                
__________________________________________________________________________________________________
conv1d_3 (Conv1D)               (None, 1082, 128)    192128      embedding_1[0][0]                
__________________________________________________________________________________________________
max_pooling1d_1 (MaxPooling1D)  (None, 361, 128)     0           conv1d_1[0][0]                   
__________________________________________________________________________________________________
max_pooling1d_2 (MaxPooling1D)  (None, 361, 128)     0           conv1d_2[0][0]                   
__________________________________________________________________________________________________
max_pooling1d_3 (MaxPooling1D)  (None, 360, 128)     0           conv1d_3[0][0]                   
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 1082, 128)    0           max_pooling1d_1[0][0]            
                                                                 max_pooling1d_2[0][0]            
                                                                 max_pooling1d_3[0][0]            
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 1082, 128)    0           concatenate_1[0][0]              
__________________________________________________________________________________________________
flatten_1 (Flatten)             (None, 138496)       0           dropout_2[0][0]                  
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 128)          17727616    flatten_1[0][0]                  
__________________________________________________________________________________________________
dense_4 (Dense)                 (None, 1)            129         dense_3[0][0]                    
==================================================================================================
Total params: 18,721,429
Trainable params: 18,188,929
Non-trainable params: 532,500

So basically, the model looks like that at the moment:所以基本上,model 现在看起来像这样: 在此处输入图像描述

So my question is how I could here some more Convolutional layers in my CNN?所以我的问题是如何在我的 CNN 中添加更多的卷积层? I tried already a lot but somehow I am not able to do it?我已经尝试了很多,但不知何故我无法做到? I would like to add Convolutional and max pooling layer before concatenate.我想在连接之前添加卷积和最大池化层。

I really appreciate any help,我非常感谢任何帮助,

Thanks in advance, Lukas在此先感谢,卢卡斯

just append new conv and max pooling layer iniside you for loop.只是 append 新的 conv 和 max pooling layer iniside you for loop。 Like this:像这样:

from tensorflow.python.keras import Input, Model
from tensorflow.python.keras.layers import Embedding, Conv1D, MaxPooling1D, concatenate, Dropout, Flatten, Dense


def ConvNet(max_sequence_length, num_words, embedding_dim, trainable=False, extra_conv=True):
    embedding_layer = Embedding(num_words,
                                embedding_dim,
                                input_length=max_sequence_length,
                                trainable=trainable)

    sequence_input = Input(shape=(max_sequence_length,), dtype='int32')
    embedded_sequences = embedding_layer(sequence_input)

    convs = []
    filter_sizes = [3, 4, 5]

    for filter_size in filter_sizes:
        l_conv = Conv1D(filters=128, kernel_size=filter_size, activation='relu')(embedded_sequences)
        l_pool = MaxPooling1D(pool_size=3)(l_conv)
        # TODO edit parameters
        l_conv2 = Conv1D(filters=128, kernel_size=3, activation='relu')(l_pool)
        l_pool2 = MaxPooling1D(pool_size=3)(l_conv2)
        convs.append(l_pool2)

    l_merge = concatenate(convs, axis=1)

    # add a 1D convnet with global maxpooling, instead of Yoon Kim model
    conv = Conv1D(filters=128, kernel_size=3, activation='relu')(embedded_sequences)
    pool = MaxPooling1D(pool_size=3)(conv)

    if extra_conv == True:
        x = Dropout(0.5)(l_merge)
    else:
        x = Dropout(0.5)(pool)
    x = Flatten()(x)
    x = Dense(128, activation='relu')(x)
    preds = Dense(1, activation='linear')(x)

    model = Model(sequence_input, preds)
    model.compile(loss='mean_squared_error',
                  optimizer='adadelta',
                  metrics=['mean_squared_error'])
    model.summary()
    return model

if __name__ == "__main__":
    ConvNet(20, 10000, 100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM