Yoon Kim在論文(2014 EMNLP) Convolutional Neural Networks for Sentence Classification提出TextCNN。網絡
將卷積神經網絡CNN應用到文本分類任務,利用多個不一樣size的kernel來提取句子中的關鍵信息(相似於多窗口大小的ngram),從而可以更好地捕捉局部相關性。app
TextCNN的詳細過程原理圖以下:框架
TextCNN詳細過程:ide
通道(Channels):學習
一維卷積(conv-1d):spa
Pooling層:設計
利用CNN解決文本分類問題的文章仍是不少的,好比這篇 A Convolutional Neural Network for Modelling Sentences 最有意思的輸入是在 pooling 改爲 (dynamic) k-max pooling ,pooling階段保留 k 個最大的信息,保留了全局的序列信息。code
好比在情感分析場景,舉個例子:blog
「我以爲這個地方景色還不錯,可是人也實在太多了」
雖然前半部分體現情感是正向的,全局文本表達的是偏負面的情感,利用 k-max pooling可以很好捕捉這類信息。token
基於Keras深度學習框架的實現代碼以下:
import logging from keras import Input from keras.layers import Conv1D, MaxPool1D, Dense, Flatten, concatenate, Embedding from keras.models import Model from keras.utils import plot_model def textcnn(max_sequence_length, max_token_num, embedding_dim, output_dim, model_img_path=None, embedding_matrix=None): """ TextCNN: 1. embedding layers, 2.convolution layer, 3.max-pooling, 4.softmax layer. """ x_input = Input(shape=(max_sequence_length,)) logging.info("x_input.shape: %s" % str(x_input.shape)) # (?, 60) if embedding_matrix is None: x_emb = Embedding(input_dim=max_token_num, output_dim=embedding_dim, input_length=max_sequence_length)(x_input) else: x_emb = Embedding(input_dim=max_token_num, output_dim=embedding_dim, input_length=max_sequence_length, weights=[embedding_matrix], trainable=True)(x_input) logging.info("x_emb.shape: %s" % str(x_emb.shape)) # (?, 60, 300) pool_output = [] kernel_sizes = [2, 3, 4] for kernel_size in kernel_sizes: c = Conv1D(filters=2, kernel_size=kernel_size, strides=1)(x_emb) p = MaxPool1D(pool_size=int(c.shape[1]))(c) pool_output.append(p) logging.info("kernel_size: %s \t c.shape: %s \t p.shape: %s" % (kernel_size, str(c.shape), str(p.shape))) pool_output = concatenate([p for p in pool_output]) logging.info("pool_output.shape: %s" % str(pool_output.shape)) # (?, 1, 6) x_flatten = Flatten()(pool_output) # (?, 6) y = Dense(output_dim, activation='softmax')(x_flatten) # (?, 2) logging.info("y.shape: %s \n" % str(y.shape)) model = Model([x_input], outputs=[y]) if model_img_path: plot_model(model, to_file=model_img_path, show_shapes=True, show_layer_names=False) model.summary() return model
特徵:這裏用的是詞向量表示方式
plot_model()畫出的TextCNN模型結構圖以下:
參考:https://zhuanlan.zhihu.com/p/25928551