pytorch implementation of multi-label text classification, includes kinds of models and pretrained. Especially for Chinese preprocessing.
This is a repo focused on Multi-Label Text Classification, the main structure was forked from lonePatient/Bert-Multi-Label-Text-Classification. We did several improvements:
# create a venv and install the dependencies
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
Prepare dataset
Read Dataset below.
Add train.csv
and test.csv
to dataset/
Each line of the train.csv
has two fields (fact and meta). Each line of the test.csv
has only one field: fact
, the output is under outputs/result
If you want to evaluate your test score, please modify main.py
line 181: is_train=False
to is_train=True
, make sure your test dataset has two fields like the train dataset.
Paths and filenames can be defined in configs/basic_config.py
Prepare pretrained model
pretrain/bert/base-uncased/
, here we used a domain model
configs/basic_config.py
config.json
, pytorch_model.bin
and bert_vocab.txt
Define a pipeline
pipeline.yml
Run ./run.sh
python main.py --do_data
python main.py --do_train --save_best
python main.py --do_test
python main.py --do_data --train_data_num 100
python main.py --do_train --save_best
python main.py --do_test --test_data_num 10
thunlp/CAIL: Chinese AI & Law Challenge Task 1
类别信息是 meta 中的 accusation,共 202 种类别,每个 fact 可能有多个类别。
预处理主要做了以下工作:
尝试做了一些特征工程,不过并未实际运用在模型当中。主要包括几个方面:
模型可选择的非常多,不过总体来看可以分为使用预训练模型和不使用预训练模型两种。一般情况下,使用预训练模型的效果要好于不使用。预训练模型可以使用词向量,也可以使用 Bert 和基于 Bert 的不同变形。
这里我们首先选择基本的、不使用预训练模型的 TextCNN 作为 Baseline,该模型如下图所示:
TextCNN 类似于 Ngram 滑动窗口提取特征,MaxPooling 获取重要特征,多个通道获取不同类型的特征。最大的问题是 MaxPooling 丢失了内部结构信息。
然后选择 Bert 作为预训练模型,我们选择了领域 Bert 模型,分别尝试了直接使用 Bert 的 classification 信息,TextRCNN 和 TextDPCNN。之所以选择这两个模型,是因为它们在之前的测评中显示的结果较好。
TextRCNN 相当于 RNN + CNN,其基本结构如下图所示:
RNN 我们采用双向 LSTM,结果与 embedding 拼接后再接一个 Maxpooling 获取重要特征。
DPCNN 可以看作是多个叠加的 CNN,结果如下图所示:
先做了两次宽度为 3,filter 数量为 250 个的卷积,然后开始做两两相邻的 MaxPooling(丢失很少的信息,提取更抽象的特征)。每个 block 中,池化后的结果与卷积后的结果相加。
除了上面介绍的几种模型外,还有其他一些比较常见的模型:
此外,一般也会做模型融合:
因为 GPU 太贵了,就没有一一尝试了。一般比较好的结果应该是:
数据不平衡问题
补充数据,或用相关数据增强数据
对数目小的类别进行过采样
调整 loss 中样本权重
标签相似问题
191130 add 中文处理
191127 updated usage details
191126 created