ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

nlp论文:文本分类:《Bag of Tricks for Efficient Text Classification》

nlp论文:文本分类:《Bag of Tricks for Efficient Text Classification》 文章目录零、NLP三种经典的词嵌入模型1.Word2Vec(浅层神经网络)2.FastText(子词词袋)3.GloVe(共现矩阵分解)一、论文和代码网址二、代码复现1.Linux系统下拉取代码、编译2.数据集情感分析3.训练4.评估模型用训练集评估模型精度5.测试三、代码结构四、模型架构五、fastText模型评价零、NLP三种经典的词嵌入模型NLP表示学习的经典基模三种词嵌入模型Word2Vec(浅层神经网络)、FastText(子词词袋)、GloVe(共现矩阵分解)1.Word2Vec(浅层神经网络)2.FastText(子词词袋)FastText有两种常见用法(1)作为词向量模型类似 Word2Vec 的 Skip-gram/CBOW但加入字符 n-gram 子词信息输出词嵌入。(2)作为文本分类器用词袋 / n-gram 特征 线性分类器。所以“子词词袋”更接近它的文本分类用法不是它词向量部分的完整描述。本文基于的就是FastText的文本二分类。判断电影影评是积极的还是消极的。3.GloVe(共现矩阵分解)一、论文和代码网址论文https://arxiv.org/pdf/1607.01759v2代码https://github.com/facebookresearch/fastText二、代码复现1.Linux系统下拉取代码、编译wgethttps://github.com/facebookresearch/fastText/archive/v0.9.2.zipunzipv0.9.2.zipcdfastText-0.9.2make2.数据集情感分析IMDB 评论25000条https://ai.stanford.edu/~amaas/data/sentiment/假设我们要做情感分析任务任务是根据电影评论判断情感是积极还是消极。那么我们可以准备如下格式的训练数据train.txt__label__positive This movie is fantastic, I loved it!__label__negative I hated this movie, it was awful. __label__positive Absolutely amazing, best movie Ive seen! __label__negative Terrible, waste of time. __label__positive Great movie, will watch again. __label__negative Didnt enjoy it at all, very boring.3.训练修改epoch轮次vimclassification-example.sh找到-epoch从5改为20训练模型命令./fasttext supervised-inputtrain_25000.txt-outputmodel ./fasttext supervised-inputtrain_25000.txt-outputmodel-lr0.01-epoch300-dim300-neg10-losshs【 loss0.137】 ./fasttext supervised-inputtrain_25000.txt-outputmodel-lr0.05-epoch50-dim300-neg30-lossns 【ns:很差】 ./fasttext supervised-inputtrain_25000.txt-outputmodel-lr0.1-epoch50-dim300-neg30-losshs【 loss0.111】最终输出Read 5M words Number of words:281111Number of labels:2Progress:100.0% words/sec/thread:835193lr:0.000000avg.loss:0.137626ETA: 0h 0m 0s4.评估模型用训练集评估模型精度./fasttexttestmodel.bin train.txt ./fasttexttestmodel.bin train_25000.txt ./fasttexttestmodel.bin test_label.txtN25000P10.996R10.996N107P10.897R10.8975.测试test.txtI loved this movie, it was fantastic!The movie was very boring and predictable. I didnt enjoy it at all, such a disappointment.预测命令./fasttext predict-prob model.bin train_25000.txt这将输出每行文本的预测标签。预测结果会以以下格式显示__label__positive0.81732__label__negative0.698711__label__positive0.999979__label__positive0.862__label__positive0.999925__label__positive0.992208__label__positive0.999989__label__positive0.910747__label__positive0.848151__label__negative1.00001__label__positive1.00001__label__negative0.832305__label__positive0.999992__label__negative0.973684__label__positive0.99999__label__negative0.965739三、代码结构C辅以Python实现1.训练过程fasttext.cc(1)train()函数voidFastText::train(constArgsargs,constTrainCallbackcallback){2.模型model.cc模型计算与更新model.cc 中主要处理 隐藏层计算、预测 和 模型参数更新 的过程。每次训练时都会通过 反向传播 来更新模型的 词向量。3.损失函数loss.cc损失函数loss.cc中实现了多种 损失函数(如负采样、层次化 softmax 和 标准softmax)每个损失函数都有自己的forward方法负责计算损失并进行反向传播。四、模型架构五、fastText模型评价1.工作原理(1)词向量通过词向量(Word Embeddings)的方式来表示每个词。将每个词拆分成了子词(subwords)利用n-gram技术以更好地处理词形变化(复数形式、时态变化)和生僻词。(2)线性分类器fastText 使用一个线性分类器来进行文本分类任务。它会将文本中的所有词的向量表示(或子词的向量)平均起来得到文本的向量表示然后通过一个线性分类器(例如 logistic regression)来进行预测。这种方法非常快速尤其适合文本分类任务。2.优势没有复杂的神经网络因此对于大规模文本训练速度很快。加速训练(1)Hierarchical Softmax(2)负样本Negative Samples负样本是随机从词汇表中选择的 不相关的词汇这些词不与给定的中心词共同出现在同一上下文中。例如在训练 “cat” 的词向量时负样本可能是从整个词汇表中随机选择的单词 “dog”、“apple”、“car” 等这些词不与 “cat” 出现在同一上下文中。3.缺陷对于双重否定句无法准确判断。4.改进改用LSTM、GRU、Transformer、BERT等深度学习模型。含有自注意力机制的模型对中长语句的理解能力更强。能更好地处理文本中的长距离依赖和上下文信息捕捉到更丰富的句法和语义信息。fastText太轻量级以至于无法加入早停和Dropout采用的是负采样和层次化softmax来训练词向量。
返回列表