"""
@Description: TextCNN 网络
@Author: 吕明伟
@Date: 2021-4-6
"""
from tensorflow.keras import Input, Model
from tensorflow.keras.layers import Embedding, Dense, Conv1D, GlobalMaxPooling1D, Concatenate, Dropout

class TextCNN(object):
    def __init__(self, maxlen, max_features, embedding_dims,
                 class_num=5,
                 last_activation='softmax'):
        self.maxlen = maxlen
        self.max_features = max_features
        self.embedding_dims = embedding_dims
        self.class_num = class_num
        self.last_activation = last_activation

    def get_model(self):
        input = Input((self.maxlen,))
        embedding = Embedding(self.max_features, self.embedding_dims, input_length=self.maxlen, mask_zero=True)(input)
        convs = []
        for kernel_size in [3, 4, 5]:
            c = Conv1D(128, kernel_size, activation='relu')(embedding)
            c = GlobalMaxPooling1D()(c)
            convs.append(c)
        x = Concatenate()(convs)

        output = Dense(self.class_num, activation=self.last_activation)(x)
        model = Model(inputs=input, outputs=output)
        return model
tensorflow.keras.layers.Concatenate tensorflow.keras.Input tensorflow.keras.layers.Embedding tensorflow.keras.layers.GlobalMaxPooling1D tensorflow.keras.layers.Dense tensorflow.keras.layers.Conv1D tensorflow.keras.Model
[(20, 'tensorflow.keras.Input', 'Input', (['(self.maxlen,)'], {}), False, 'from tensorflow.keras import Input, Model\n'), (30, 'tensorflow.keras.Model', 'Model', ([], {'inputs': 'input', 'outputs': 'output'}), False, 'from tensorflow.keras import Input, Model\n'), (21, 'tensorflow.keras.layers.Embedding', 'Embedding', (['self.max_features', 'self.embedding_dims'], {'input_length': 'self.maxlen', 'mask_zero': '(True)'}), False, 'from tensorflow.keras.layers import Embedding, Dense, Conv1D, GlobalMaxPooling1D, Concatenate, Dropout\n'), (27, 'tensorflow.keras.layers.Concatenate', 'Concatenate', ([], {}), False, 'from tensorflow.keras.layers import Embedding, Dense, Conv1D, GlobalMaxPooling1D, Concatenate, Dropout\n'), (29, 'tensorflow.keras.layers.Dense', 'Dense', (['self.class_num'], {'activation': 'self.last_activation'}), False, 'from tensorflow.keras.layers import Embedding, Dense, Conv1D, GlobalMaxPooling1D, Concatenate, Dropout\n'), (24, 'tensorflow.keras.layers.Conv1D', 'Conv1D', (['(128)', 'kernel_size'], {'activation': '"""relu"""'}), False, 'from tensorflow.keras.layers import Embedding, Dense, Conv1D, GlobalMaxPooling1D, Concatenate, Dropout\n'), (25, 'tensorflow.keras.layers.GlobalMaxPooling1D', 'GlobalMaxPooling1D', ([], {}), False, 'from tensorflow.keras.layers import Embedding, Dense, Conv1D, GlobalMaxPooling1D, Concatenate, Dropout\n')]