# Text Classifier
此仓库是基于Tensorflow2.3的文本分类任务,分别支持:
* 随机初始Word Embedding + TextCNN
* 随机初始Word Embedding + Attention + TextCNN
* 随机初始Word Embedding + TextRCNN
* Word2Vec + TextCNN
* Word2Vec + Attention + TextCNN
* Word2Vec + TextRCNN
* Bert Embedding(没有微调,直接取向量) + TextCNN
* Bert Embedding(没有微调,直接取向量) + TextRCNN
代码支持二分类和多分类,此项目基于爬取的游戏评论做了个二元的情感分类作为demo。
## 环境
* python 3.6.7
* tensorflow==2.3.0
* gensim==3.8.3
* jieba==0.42.1
* sklearn==0.0
其他环境见requirements.txt
## 更新历史
日期|版本|描述
:---|:---|---
2018-12-01|v1.0.0|初始仓库
2020-10-20|v2.0.0|重构项目
2020-10-26|v2.1.0|加入F1、Precise、Recall分类指标,计算方式支持macro、micro、average、binary
2020-11-06|v2.2.0|加入TextRCNN
2020-11-19|v2.3.0|加入Attention
2020-11-26|v2.3.1|加入focal loss用于改善标签分布不平衡的情况
2020-11-19|v2.4.0|增加每个类别的指标,重构指标计算逻辑
2021-03-02|v2.5.0|使用Dataset替换自己写的数据加载器来加载数据
2021-03-15|v3.0.0|支持仅使用TextCNN/TextRCNN进行数据训练(基于词粒度的token,使用随机生成的Embedding层)
2021-03-16|v3.1.0|支持取用Bert的编码后接TextCNN/TextRCNN进行数据训练(此项目Bert不支持预训练);在log中打印配置
2021-03-17|v3.1.1|根据词频过滤一部分频率极低的词,不加入词表
## 数据集
我的另外一个爬虫项目[app_comments_spider](https://github.com/StanleyLsx/app_comments_spider)中爬取
## 原理
### Word2vec
可以参考我的博客文章[01-NLP介绍和词向量](https://lishouxian.cn/2020/04/06/NLP%E4%BB%8B%E7%BB%8D%E5%92%8C%E8%AF%8D%E5%90%91%E9%87%8F/#WordNet)和[02-词向量第二部分和词义](https://lishouxian.cn/2020/04/13/%E8%AF%8D%E5%90%91%E9%87%8F%E7%AC%AC%E4%BA%8C%E9%83%A8%E5%88%86%E5%92%8C%E8%AF%8D%E4%B9%89/)
也可看博客[刘建平Pinard](https://www.cnblogs.com/pinard/p/7160330.html)和文章[技术干货 | 漫谈Word2vec之skip-gram模型](https://mp.weixin.qq.com/s/reT4lAjwo4fHV4ctR9zbxQ?)
### TextCNN
![textcnn](https://img-blog.csdnimg.cn/20201021000109653.png)
### TextRCNN
![textrcnn](https://img-blog.csdnimg.cn/20201107140825534.png)
## 使用
### 配置
在config.py中配置好各个参数,文件中有详细参数说明
### 训练word2vec
在config.py中的mode中改成train_word2vec并运行
```
# [train_classifier, interactive_predict, train_word2vec]
mode = 'train_word2vec'
```
### 训练分类器
配置好下列参数
```
classifier_config = {
# 模型选择
'classifier': 'textcnn',
# 训练数据集
'train_file': 'data/data/train_data.csv',
# 引入外部的词嵌入,可选word2vec、Bert
# 此处只使用Bert Embedding,不对其做预训练
# None:使用随机初始化的Embedding
'embedding_method': 'Bert',
# 不外接词向量的时候需要自定义的向量维度
'embedding_dim': 300,
# 存放词表的地方
'token_file': 'data/data/token2id',
# 验证数据集
'dev_file': 'data/data/dev_data.csv',
# 类别和对应的id
'classes': {'negative': 0, 'positive': 1},
# 模型保存的文件夹
'checkpoints_dir': 'model/bert_textcnn',
# 模型保存的名字
'checkpoint_name': 'bert_textcnn',
# 卷集核的个数
'num_filters': 64,
# 学习率
'learning_rate': 0.001,
# 训练epoch
'epoch': 30,
# 最多保存max_to_keep个模型
'max_to_keep': 1,
# 每print_per_batch打印
'print_per_batch': 20,
# 是否提前结束
'is_early_stop': True,
# 是否引入attention
# 注意:textrcnn不支持
'use_attention': False,
# attention大小
'attention_dim': 300,
'patient': 8,
'batch_size': 64,
'max_sequence_length': 150,
# 遗忘率
'droupout_rate': 0.5,
# 隐藏层维度
# 使用textrcnn中需要设定
'hidden_dim': 200,
# 若为二分类则使用binary
# 多分类使用micro或macro
'metrics_average': 'binary',
# 类别样本比例失衡的时候可以考虑使用
'use_focal_loss': False
}
```
配置完参数之后开始训练模型
```
# [train_classifier, interactive_predict, train_word2vec]
mode = 'train_classifier'
```
* textcnn训练结果
![train_results_textcnn](https://img-blog.csdnimg.cn/2020110713592572.png)
* att-textcnn训练结果
![train_results_att-textcnn](https://img-blog.csdnimg.cn/20201119115846656.png)
* textrcnn训练结果
![train_results_textrcnn](https://img-blog.csdnimg.cn/20201107140248442.png)
### 测试
训练好textcnn可以开始测试
```
# [train_classifier, interactive_predict, train_word2vec]
mode = 'interactive_predict'
```
* 交互测试结果
![test](https://img-blog.csdnimg.cn/20201021000109568.png)
## 参考
* [app_comments_spider](https://github.com/StanleyLsx/app_comments_spider)
* [01-NLP介绍和词向量](https://lishouxian.cn/2020/04/06/NLP%E4%BB%8B%E7%BB%8D%E5%92%8C%E8%AF%8D%E5%90%91%E9%87%8F/#WordNet)
* [02-词向量第二部分和词义](https://lishouxian.cn/2020/04/13/%E8%AF%8D%E5%90%91%E9%87%8F%E7%AC%AC%E4%BA%8C%E9%83%A8%E5%88%86%E5%92%8C%E8%AF%8D%E4%B9%89/)
* [刘建平Pinard](https://www.cnblogs.com/pinard/p/7160330.html)
* [技术干货 | 漫谈Word2vec之skip-gram模型](https://mp.weixin.qq.com/s/reT4lAjwo4fHV4ctR9zbxQ?)
* [Recurrent Convolutional Neural Networks for Text Classification](http://zhengyima.com/my/pdfs/Textrcnn.pdf)
没有合适的资源?快使用搜索试试~ 我知道了~
text_classifier:该项目是使用TextCNNTextRCNN的文本分类任务,嵌入层可调用Word2Vec,Bert...
共59个文件
py:16个
index:7个
data-00000-of-00001:7个
需积分: 50 16 下载量 88 浏览量
2021-03-17
19:37:28
上传
评论 2
收藏 65.45MB ZIP 举报
温馨提示
文字分类器 此仓库是基于Tensorflow2.3的文本分类任务,分别支持: 随机初始单词嵌入+ TextCNN 随机初始词嵌入+注意+ TextCNN 随机初始单词嵌入+ TextRCNN Word2Vec + TextCNN Word2Vec +注意+ TextCNN Word2Vec + TextRCNN 伯特嵌入(没有微调,直接取向量)+ TextCNN Bert嵌入(没有微调,直接取向量)+ TextRCNN 代码支持二分类和多分类,此项目基于爬取的游戏评论正是个二元的情感分类作为演示。 环境 python 3.6.7 张量流== 2.3.0 gensim == 3.8.3 杰巴== 0.42.1 sklearn == 0.0 其他环境见requirements.txt 更新历史 日期 版本 描述 2018-12-01 v1.0.0 初始仓库 2020-10-
资源详情
资源评论
资源推荐
收起资源包目录
text_classifier-master.zip (59个子文件)
text_classifier-master
.gitignore 2KB
README.md 5KB
config.py 2KB
model
word2vec_textcnn
word2vec_textcnn-4.index 709B
checkpoint 188B
word2vec_textcnn-4.data-00000-of-00001 678KB
bert_textcnn
bert_textcnn-8.data-00000-of-00001 1.69MB
checkpoint 182B
bert_textcnn-8.index 709B
word2vec_textrcnn
word2vec_textrcnn-2.index 863B
checkpoint 191B
word2vec_textrcnn-2.data-00000-of-00001 4.94MB
word2vec_attention_textcnn
word2vec_attention_textcnn-11.index 955B
word2vec_attention_textcnn-11.data-00000-of-00001 1.01MB
checkpoint 211B
textcnn
textcnn-2.data-00000-of-00001 8MB
checkpoint 171B
textcnn-2.index 796B
textrcnn
textrcnn-1.data-00000-of-00001 21.42MB
textrcnn-1.index 946B
checkpoint 174B
w2v_model
w2v_model.pkl 17.19MB
attention_textcnn
attention_textcnn-7.data-00000-of-00001 17.49MB
checkpoint 192B
attention_textcnn-7.index 1KB
main.py 2KB
img
textrcnn_train.png 73KB
textcnn_train.png 76KB
textrcnn.png 170KB
predict_results.png 40KB
att-textcnn_train.png 59KB
textcnn.png 166KB
requirements.txt 837B
data
w2v_data
comments_data.csv 1.12MB
stop_words.txt 8KB
data
dev_data.csv 98KB
train_data.csv 1.02MB
token2id 75KB
engines
predict.py 2KB
utils
sentence2vec.py 3KB
logger.py 854B
word2vec.py 2KB
metrics.py 766B
__init__.py 162B
clean_data.py 309B
focal_loss.py 2KB
train.py 7KB
models
textcnn.py 4KB
textrcnn.py 2KB
__init__.py 159B
__init__.py 158B
data.py 9KB
logs
attention_textcnn.log 13KB
word2vec_textrcnn.log 8KB
textcnn.log 8KB
bert_textcnn.log 10KB
word2vec_attention_textcnn.log 18KB
textrcnn.log 7KB
word2vec_textcnn.log 11KB
共 59 条
- 1
初見目
- 粉丝: 19
- 资源: 4594
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0