模型简介
模型特点
模型能力
使用案例
🚀 中文RoBERTa微型模型
本项目提供了24个中文RoBERTa模型,由UER-py进行预训练,相关内容可参考这篇论文。此外,这些模型也可以使用TencentPretrain进行预训练,相关介绍见这篇论文,TencentPretrain继承了UER-py,支持参数超过十亿的模型,并将其扩展为多模态预训练框架。
Turc等人的研究表明,标准的BERT方法在各种规模的模型上都很有效。基于他们的研究,我们发布了这24个中文RoBERTa模型。为了方便用户复现实验结果,我们使用了公开可用的语料库,并提供了所有训练细节。
🚀 快速开始
你可以从UER-py模型库页面下载这24个中文RoBERTa微型模型,也可以通过HuggingFace从以下链接下载:
H=128 | H=256 | H=512 | H=768 | |
---|---|---|---|---|
L=2 | 2/128 (Tiny) | 2/256 | 2/512 | 2/768 |
L=4 | 4/128 | 4/256 (Mini) | 4/512 (Small) | 4/768 |
L=6 | 6/128 | 6/256 | 6/512 | 6/768 |
L=8 | 8/128 | 8/256 | 8/512 (Medium) | 8/768 |
L=10 | 10/128 | 10/256 | 10/512 | 10/768 |
L=12 | 12/128 | 12/256 | 12/512 | 12/768 (Base) |
以下是这些模型在六个中文任务开发集上的得分:
模型 | 得分 | 书评任务 | 中文情感语料库 | 中文自然语言推理 | 新闻分类(CLUE) | 长文本分类(CLUE) | 自然语言推理(CLUE) |
---|---|---|---|---|---|---|---|
RoBERTa-Tiny | 72.3 | 83.4 | 91.4 | 81.8 | 62.0 | 55.0 | 60.3 |
RoBERTa-Mini | 75.9 | 85.7 | 93.7 | 86.1 | 63.9 | 58.3 | 67.4 |
RoBERTa-Small | 76.9 | 87.5 | 93.4 | 86.5 | 65.1 | 59.4 | 69.7 |
RoBERTa-Medium | 78.0 | 88.7 | 94.8 | 88.1 | 65.6 | 59.5 | 71.2 |
RoBERTa-Base | 79.7 | 90.1 | 95.2 | 89.2 | 67.0 | 60.9 | 75.5 |
对于每个任务,我们从以下列表中选择最佳的微调超参数,并使用序列长度128进行训练:
- 训练轮数:3、5、8
- 批次大小:32、64
- 学习率:3e-5、1e-4、3e-4
✨ 主要特性
- 多种模型规模:提供了24种不同规模的中文RoBERTa模型,满足不同场景的需求。
- 公开语料库训练:使用公开可用的语料库进行训练,方便用户复现结果。
- 多工具支持:既可以使用UER-py进行预训练,也可以使用TencentPretrain。
💻 使用示例
基础用法
你可以直接使用填充掩码语言模型的管道来使用这个模型(以RoBERTa-Medium为例):
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='uer/chinese_roberta_L-8_H-512')
>>> unmasker("中国的首都是[MASK]京。")
[
{'sequence': '[CLS] 中 国 的 首 都 是 北 京 。 [SEP]',
'score': 0.8701988458633423,
'token': 1266,
'token_str': '北'},
{'sequence': '[CLS] 中 国 的 首 都 是 南 京 。 [SEP]',
'score': 0.1194809079170227,
'token': 1298,
'token_str': '南'},
{'sequence': '[CLS] 中 国 的 首 都 是 东 京 。 [SEP]',
'score': 0.0037803512532263994,
'token': 691,
'token_str': '东'},
{'sequence': '[CLS] 中 国 的 首 都 是 普 京 。 [SEP]',
'score': 0.0017127094324678183,
'token': 3249,
'token_str': '普'},
{'sequence': '[CLS] 中 国 的 首 都 是 望 京 。 [SEP]',
'score': 0.001687526935711503,
'token': 3307,
'token_str': '望'}
]
高级用法
以下是如何在PyTorch中使用这个模型来获取给定文本的特征:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('uer/chinese_roberta_L-8_H-512')
model = BertModel.from_pretrained("uer/chinese_roberta_L-8_H-512")
text = "用你喜欢的任何文本替换我。"
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
在TensorFlow中的使用方法如下:
from transformers import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('uer/chinese_roberta_L-8_H-512')
model = TFBertModel.from_pretrained("uer/chinese_roberta_L-8_H-512")
text = "用你喜欢的任何文本替换我。"
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
📦 安装指南
文档中未提及安装相关内容,可参考UER-py和TencentPretrain的官方文档进行安装。
📚 详细文档
训练数据
使用CLUECorpusSmall作为训练数据。我们发现,尽管CLUECorpus2020比CLUECorpusSmall大得多,但在CLUECorpusSmall上预训练的模型性能优于在CLUECorpus2020上预训练的模型。
训练过程
模型在腾讯云上使用UER-py进行预训练。我们首先使用序列长度128进行1,000,000步的预训练,然后使用序列长度512进行额外的250,000步预训练。不同模型大小使用相同的超参数。
以RoBERTa-Medium为例:
阶段1
python3 preprocess.py --corpus_path corpora/cluecorpussmall.txt \
--vocab_path models/google_zh_vocab.txt \
--dataset_path cluecorpussmall_seq128_dataset.pt \
--processes_num 32 --seq_length 128 \
--dynamic_masking --data_processor mlm
python3 pretrain.py --dataset_path cluecorpussmall_seq128_dataset.pt \
--vocab_path models/google_zh_vocab.txt \
--config_path models/bert/medium_config.json \
--output_model_path models/cluecorpussmall_roberta_medium_seq128_model.bin \
--world_size 8 --gpu_ranks 0 1 2 3 4 5 6 7 \
--total_steps 1000000 --save_checkpoint_steps 100000 --report_steps 50000 \
--learning_rate 1e-4 --batch_size 64 \
--data_processor mlm --target mlm
阶段2
python3 preprocess.py --corpus_path corpora/cluecorpussmall.txt \
--vocab_path models/google_zh_vocab.txt \
--dataset_path cluecorpussmall_seq512_dataset.pt \
--processes_num 32 --seq_length 512 \
--dynamic_masking --data_processor mlm
python3 pretrain.py --dataset_path cluecorpussmall_seq512_dataset.pt \
--vocab_path models/google_zh_vocab.txt \
--pretrained_model_path models/cluecorpussmall_roberta_medium_seq128_model.bin-1000000 \
--config_path models/bert/medium_config.json \
--output_model_path models/cluecorpussmall_roberta_medium_seq512_model.bin \
--world_size 8 --gpu_ranks 0 1 2 3 4 5 6 7 \
--total_steps 250000 --save_checkpoint_steps 50000 --report_steps 10000 \
--learning_rate 5e-5 --batch_size 16 \
--data_processor mlm --target mlm
最后,我们将预训练模型转换为Huggingface的格式:
python3 scripts/convert_bert_from_uer_to_huggingface.py --input_model_path models/cluecorpussmall_roberta_medium_seq512_model.bin-250000 \
--output_model_path pytorch_model.bin \
--layers_num 8 --type mlm
BibTeX引用和引用信息
@article{devlin2018bert,
title={Bert: Pre-training of deep bidirectional transformers for language understanding},
author={Devlin, Jacob and Chang, Ming-Wei and Lee, Kenton and Toutanova, Kristina},
journal={arXiv preprint arXiv:1810.04805},
year={2018}
}
@article{liu2019roberta,
title={Roberta: A robustly optimized bert pretraining approach},
author={Liu, Yinhan and Ott, Myle and Goyal, Naman and Du, Jingfei and Joshi, Mandar and Chen, Danqi and Levy, Omer and Lewis, Mike and Zettlemoyer, Luke and Stoyanov, Veselin},
journal={arXiv preprint arXiv:1907.11692},
year={2019}
}
@article{turc2019,
title={Well-Read Students Learn Better: On the Importance of Pre-training Compact Models},
author={Turc, Iulia and Chang, Ming-Wei and Lee, Kenton and Toutanova, Kristina},
journal={arXiv preprint arXiv:1908.08962v2 },
year={2019}
}
@article{zhao2019uer,
title={UER: An Open-Source Toolkit for Pre-training Models},
author={Zhao, Zhe and Chen, Hui and Zhang, Jinbin and Zhao, Xin and Liu, Tao and Lu, Wei and Chen, Xi and Deng, Haotang and Ju, Qi and Du, Xiaoyong},
journal={EMNLP-IJCNLP 2019},
pages={241},
year={2019}
}
@article{zhao2023tencentpretrain,
title={TencentPretrain: A Scalable and Flexible Toolkit for Pre-training Models of Different Modalities},
author={Zhao, Zhe and Li, Yudong and Hou, Cheng and Zhao, Jing and others},
journal={ACL 2023},
pages={217},
year={2023}
}



