MCPcopy Index your code
hub / github.com/649453932/Bert-Chinese-Text-Classification-Pytorch

github.com/649453932/Bert-Chinese-Text-Classification-Pytorch @main sqlite

repository ↗ · DeepWiki ↗
444 symbols 1,095 edges 27 files 161 documented · 36%
README

Bert-Chinese-Text-Classification-Pytorch

LICENSE

中文文本分类,Bert,ERNIE,基于pytorch,开箱即用。

介绍

模型介绍、数据流动过程:~~还没写完,写好之后再贴博客地址。~~ 工作忙,懒得写了,类似文章有很多。

机器:一块2080Ti , 训练时间:30分钟。

环境

python 3.7
pytorch 1.1
tqdm
sklearn
tensorboardX
~~pytorch_pretrained_bert~~(预训练代码也上传了, 不需要这个库了)

中文数据集

我从THUCNews中抽取了20万条新闻标题,已上传至github,文本长度在20到30之间。一共10个类别,每类2万条。数据以字为单位输入模型。

类别:财经、房产、股票、教育、科技、社会、时政、体育、游戏、娱乐。

数据集划分:

数据集 数据量
训练集 18万
验证集 1万
测试集 1万

更换自己的数据集

  • 按照我数据集的格式来格式化你的中文数据集。

效果

模型 acc 备注
bert 94.83% 单纯的bert
ERNIE 94.61% 说好的中文碾压bert呢
bert_CNN 94.44% bert + CNN
bert_RNN 94.57% bert + RNN
bert_RCNN 94.51% bert + RCNN
bert_DPCNN 94.47% bert + DPCNN

原始的bert效果就很好了,把bert当作embedding层送入其它模型,效果反而降了,之后会尝试长文本的效果对比。

CNN、RNN、DPCNN、RCNN、RNN+Attention、FastText等模型效果,请见我另外一个仓库

预训练语言模型

bert模型放在 bert_pretain目录下,ERNIE模型放在ERNIE_pretrain目录下,每个目录下都是三个文件: - pytorch_model.bin
- bert_config.json
- vocab.txt

预训练模型下载地址:
bert_Chinese: 模型 https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-chinese.tar.gz
词表 https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-chinese-vocab.txt
来自这里
备用:模型的网盘地址:https://pan.baidu.com/s/1qSAD5gwClq7xlgzl_4W3Pw

ERNIE_Chinese: http://image.nghuyong.top/ERNIE.zip
来自这里
备用:网盘地址:https://pan.baidu.com/s/1lEPdDN1-YQJmKEd_g9rLgw

解压后,按照上面说的放在对应目录下,文件名称确认无误即可。

使用说明

下载好预训练模型就可以跑了。

# 训练并测试:
# bert
python run.py --model bert

# bert + 其它
python run.py --model bert_CNN

# ERNIE
python run.py --model ERNIE

参数

模型都在models目录下,超参定义和模型定义在同一文件中。

未完待续

  • 封装预测功能

对应论文

[1] BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
[2] ERNIE: Enhanced Representation through Knowledge Integration

Core symbols most depended-on inside this repo

cached_path
called by 14
pytorch_pretrained/file_utils.py
from_pretrained
called by 12
pytorch_pretrained/tokenization.py
load
called by 11
pytorch_pretrained/modeling.py
encode_file
called by 9
pytorch_pretrained/tokenization_transfo_xl.py
init_weight
called by 6
pytorch_pretrained/modeling_transfo_xl.py
_compute_logit
called by 6
pytorch_pretrained/modeling_transfo_xl_utilities.py
init_bias
called by 4
pytorch_pretrained/modeling_transfo_xl.py
count_file
called by 4
pytorch_pretrained/tokenization_transfo_xl.py

Shape

Method 302
Class 95
Function 47

Languages

Python100%

Modules by API surface

pytorch_pretrained/modeling.py86 symbols
pytorch_pretrained/modeling_transfo_xl.py65 symbols
pytorch_pretrained/modeling_openai.py52 symbols
pytorch_pretrained/modeling_gpt2.py49 symbols
pytorch_pretrained/tokenization_transfo_xl.py41 symbols
pytorch_pretrained/tokenization.py23 symbols
pytorch_pretrained/optimization.py23 symbols
pytorch_pretrained/tokenization_gpt2.py15 symbols
pytorch_pretrained/tokenization_openai.py14 symbols
pytorch_pretrained/file_utils.py12 symbols
utils.py10 symbols
pytorch_pretrained/modeling_transfo_xl_utilities.py9 symbols

For agents

$ claude mcp add Bert-Chinese-Text-Classification-Pytorch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact