在这个教程中,我们所做的任务为MRPC数据上的文本匹配,因为在BERT中,文本匹配本质是一个双句文本分类,因此下面代码用文本分类的方法进行任务。本次教程依托PAI-PyTorch上开发的EasyNLP,用户仅需要配置好相关命令参数,改动少量代码就可以在PAI上跑BERT文本分类任务。
1. 用户自定义模式
跑脚本
cd EasyNLP/examples/self_defined_examples/sh run_user_defined_local.sh
下载数据
export CUDA_VISIBLE_DEVICES=0# Local training example# cur_path=/tmp/EasyNLPcur_path=/home/admin/workspace/EasyNLP/cd ${cur_path}if [ ! -f ./tmp/train.tsv ]; thenwget http://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/release/tutorials/classification/train.tsvwget http://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/release/tutorials/classification/dev.tsvmkdir tmp/mv *.tsv tmp/fi
跑训练脚本
这里预训练模型是: bert-small-uncased
DISTRIBUTED_ARGS="--nproc_per_node 1 --nnodes 1 --node_rank 0 --master_addr localhost --master_port 6009"python -m torch.distributed.launch $DISTRIBUTED_ARGS \examples/self_defined_examples/main.py \--mode train \--tables=tmp/train.tsv,tmp/dev.tsv \--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1 \--first_sequence=sent1 \--second_sequence=sent2 \--label_name=label \--label_enumerate_values=0,1 \--checkpoint_dir=./tmp/classification_model/ \--learning_rate=3e-5 \--epoch_num=3 \--random_seed=42 \--logging_steps=1 \--save_checkpoint_steps=50 \--sequence_length=128 \--micro_batch_size=10 \--app_name=text_classify \--use_amp \--user_defined_parameters='pretrain_model_name_or_path=bert-small-uncased'
2. AppZoo模式
下面以BERT 文本分类/匹配为例子,测试AppZoo功能。BERT 文本分类/匹配采用BERT类模型训练模型,输出分类标签。在easynlp 命令中选择 text_classify 即可调用这个模型。
其他功能详见:https://www.yuque.com/easyx/easynlp/kkhkai
首先可以下载 训练集 和 评估集,其中 train.csv , dev.csv 是用\t 分隔的 .csv 文件。
wget http://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/easytexminer/tutorials/classification/train.tsvwget http://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/easytexminer/tutorials/classification/dev.tsv
模型训练
easynlp \--mode=train \--worker_gpu=1 \--tables=train.tsv,dev.tsv \--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1 \--first_sequence=sent1 \--second_sequence=sent2 \--label_name=label \--label_enumerate_values=0,1 \--checkpoint_dir=./classification_model \--learning_rate=3e-5 \--epoch_num=3 \--random_seed=42 \--save_checkpoint_steps=50 \--sequence_length=128 \--micro_batch_size=32 \--app_name=text_classify \--user_defined_parameters='pretrain_model_name_or_path=bert-small-uncased'
模型评估
easynlp \--mode=evaluate \--worker_gpu=1 \--tables=dev.tsv \--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1 \--first_sequence=sent1 \--second_sequence=sent2 \--label_name=label \--label_enumerate_values=0,1 \--checkpoint_path=./classification_model \--micro_batch_size=32 \--sequence_length=128 \--app_name=text_classify
模型预测
easynlp \
--mode=predict \
--worker_gpu=1 \
--tables=dev.tsv \
--outputs=dev.pred.tsv \
--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1 \
--output_schema=predictions,probabilities,logits,output \
--append_cols=label \
--first_sequence=sent1 \
--second_sequence=sent2 \
--checkpoint_path=./classification_model \
--micro_batch_size=32 \
--sequence_length=128 \
--app_name=text_classify
参数说明:
- output_schema:需要输出的结果类型,默认有四种:predictions(预测结果),probabilities(预测的概率),logits(预测的logits,即softmax之前的值),output(输出值)
- append_cols:需要append的输入数据的column,多个column可以用逗号分隔
