标签:show 代码 matrix flat strong 同方 out 二维 enc
Yoon Kim在论文(2014 EMNLP) Convolutional Neural Networks for Sentence Classification提出TextCNN。
将卷积神经网络CNN应用到文本分类任务,利用多个不同size的kernel来提取句子中的关键信息(类似于多窗口大小的ngram),从而能够更好地捕捉局部相关性。
TextCNN的详细过程原理图如下:
TextCNN详细过程:
通道(Channels):
一维卷积(conv-1d):
Pooling层:
利用CNN解决文本分类问题的文章还是很多的,比如这篇 A Convolutional Neural Network for Modelling Sentences 最有意思的输入是在 pooling 改成 (dynamic) k-max pooling ,pooling阶段保留 k 个最大的信息,保留了全局的序列信息。
比如在情感分析场景,举个例子:
“我觉得这个地方景色还不错,但是人也实在太多了”
虽然前半部分体现情感是正向的,全局文本表达的是偏负面的情感,利用 k-max pooling能够很好捕捉这类信息。
基于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
标签:show 代码 matrix flat strong 同方 out 二维 enc
原文地址:https://www.cnblogs.com/bymo/p/9675654.html