基于Tensorflow的LSTM-CNN文本分类模型

本文介绍了基于Tensorflow实现的LSTM-CNN文本分类模型,结合LSTM的长期依赖和CNN的局部特征提取能力,用于文本语义理解。实验结果显示,在训练集和验证集上的准确率分别达到87.31%和91.17%,相较于纯LSTM模型有明显提升。
摘要由CSDN通过智能技术生成

题记

    前段间再看QA方面的文章,读了一篇paper(《LSTM-based deep learning model for non-factoid answer selection》)中,使用了LSTM-CNN模型来做answer与question的语义抽取。受此启发,使用这个模型对文本语义信息进行抽取,加上一个softmax函数形成文本分类模型。

1.LSTM(Long Short-Term Memory)

    LSTM在NLP中的应用实在太广泛了,在Machine Translation,Text Classification,QA等领域都有着成熟的应用,具体通过对RNN的结构进行改进,加入Memory Cell与三个门控单元,对历史信息进行有效的控制。而不是像RNN一样每次都将前一时刻的hidden state完全洗掉,从而增强了其处理长文本序列的能力,也解决了vanishing gradient的问题。

    具体结构如图所示:


    Input Gate决定当前时刻LSTM单元的Input vector对memory cell中信息的改变量,Forget Gate决定上一时刻历史信息对当前时刻memory cell中的信息的影响程度,Output Gate对memory cell中信息的输出量进行控制。

    将Input Gate,Output Gate,Forget Gate表示为:,LSTM更新方法为:





</

  • 21
    点赞
  • 186
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
以下是基于LSTM-CNN文本分类算法的Python代码: ```python import numpy as np import tensorflow as tf class LSTM_CNN_Model: def __init__(self, sequence_length, num_classes, vocab_size, embedding_size, filter_sizes, num_filters, hidden_size, dropout_rate): self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name='input_x') self.input_y = tf.placeholder(tf.float32, [None, num_classes], name='input_y') self.dropout_rate = tf.placeholder(tf.float32, name='dropout_rate') self.global_step = tf.Variable(0, trainable=False, name='global_step') with tf.name_scope('embedding'): self.W = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), name='W') self.embedded_chars = tf.nn.embedding_lookup(self.W, self.input_x) self.embedded_chars_expanded = tf.expand_dims(self.embedded_chars, -1) pooled_outputs = [] for i, filter_size in enumerate(filter_sizes): with tf.name_scope('conv-maxpool-%s' % filter_size): filter_shape = [filter_size, embedding_size, 1, num_filters] W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name='W') b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name='b') conv = tf.nn.conv2d(self.embedded_chars_expanded, W, strides=[1, 1, 1, 1], padding='VALID', name='conv') h = tf.nn.relu(tf.nn.bias_add(conv, b), name='relu') pooled = tf.nn.max_pool(h, ksize=[1, sequence_length - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name='pool') pooled_outputs.append(pooled) num_filters_total = num_filters * len(filter_sizes) self.h_pool = tf.concat(pooled_outputs, 3) self.h_pool_flat = tf.reshape(self.h_pool, [-1, num_filters_total]) with tf.name_scope('lstm'): lstm_cell = tf.contrib.rnn.BasicLSTMCell(hidden_size) lstm_cell = tf.contrib.rnn.DropoutWrapper(lstm_cell, output_keep_prob=1.0 - self.dropout_rate) outputs, _ = tf.nn.dynamic_rnn(lstm_cell, self.embedded_chars, dtype=tf.float32) lstm_out = tf.reduce_mean(outputs, axis=1) with tf.name_scope('output'): W = tf.Variable(tf.truncated_normal([num_filters_total + hidden_size, num_classes], stddev=0.1), name='W') b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name='b') self.scores = tf.nn.xw_plus_b(tf.concat([self.h_pool_flat, lstm_out], axis=1), W, b, name='scores') self.predictions = tf.argmax(self.scores, 1, name='predictions') with tf.name_scope('loss'): losses = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.scores, labels=self.input_y) self.loss = tf.reduce_mean(losses) with tf.name_scope('accuracy'): correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, 'float'), name='accuracy') self.optimizer = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(self.loss, global_step=self.global_step) ``` 这段代码实现了一个LSTMCNN结合的文本分类模型,使用了dropout和Adam优化器。其中,`sequence_length`表示输入序列的长度,`num_classes`表示分类数,`vocab_size`表示词汇表的大小,`embedding_size`表示词向量的维度,`filter_sizes`表示卷积核的大小,`num_filters`表示卷积核的数量,`hidden_size`表示LSTM隐藏层的大小,`dropout_rate`表示dropout的概率。在模型的构建中,首先将输入的词序列转换为词向量,然后通过卷积核进行卷积和池化操作,得到一个固定长度的向量表示。接着,将词向量输入到LSTM中,得到一个平均池化的向量表示。最后将卷积和LSTM的输出拼接在一起,通过一个全连接层进行分类。
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值