模型简介
模型特点
模型能力
使用案例
🚀 中文RoBERTa轻量级模型
本项目提供了24个中文RoBERTa预训练模型,这些模型能够有效处理中文语言任务,为中文自然语言处理提供了强大的支持。
🚀 快速开始
你可以直接使用此模型进行掩码语言建模任务,以下以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)
✨ 主要特性
- 本项目提供了24个不同规模的中文RoBERTa模型,满足不同场景的需求。
- 模型可以通过UER-py或TencentPretrain进行预训练。
- 使用公开可用的语料库进行训练,并提供了所有训练细节,方便用户复现结果。
📦 安装指南
文档未提及安装相关内容,故跳过此章节。
💻 使用示例
基础用法
# 直接使用模型进行掩码语言建模
from transformers import pipeline
unmasker = pipeline('fill-mask', model='uer/chinese_roberta_L-8_H-512')
unmasker("中国的首都是[MASK]京。")
高级用法
# 在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)
📚 详细文档
模型描述
这是由UER-py预训练的24个中文RoBERTa模型集合,相关内容在这篇论文中有所介绍。此外,这些模型也可以通过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) |
以下是这24个模型在六个中文任务开发集上的得分:
模型名称 | 综合得分 | 书评分类 | 情感分析 | 语义相似度 | 新闻分类(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
训练数据
使用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}
}



