模型简介
模型特点
模型能力
使用案例
🚀 中文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模型,涵盖了从Tiny到Base的多种规格,满足不同场景的需求。
- 效果出色:在多个中文任务的开发集上取得了良好的成绩,如书籍评论、情感分析、语义匹配等任务。
- 易于使用:可以直接使用
transformers
库中的pipeline
进行掩码语言建模任务,也可以方便地在PyTorch和TensorFlow中使用。
📦 安装指南
文档未提及安装步骤,因此跳过此章节。
💻 使用示例
基础用法
# 直接使用pipeline进行掩码语言建模
from transformers import pipeline
unmasker = pipeline('fill-mask', model='uer/chinese_roberta_L-8_H-512')
result = unmasker("中国的首都是[MASK]京。")
print(result)
高级用法
# 在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)
print(output)
📚 详细文档
模型描述
这组24个中文RoBERTa模型由UER-py预训练得到,相关内容在这篇论文中有所介绍。此外,这些模型也可以通过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_128] | [2/256][2_256] | [2/512][2_512] | [2/768][2_768] |
L=4 | [4/128][4_128] | [4/256 (Mini)][4_256] | [4/512 (Small)][4_512] | [4/768][4_768] |
L=6 | [6/128][6_128] | [6/256][6_256] | [6/512][6_512] | [6/768][6_768] |
L=8 | [8/128][8_128] | [8/256][8_256] | [8/512 (Medium)][8_512] | [8/768][8_768] |
L=10 | [10/128][10_128] | [10/256][10_256] | [10/512][10_512] | [10/768][10_768] |
L=12 | [12/128][12_128] | [12/256][12_256] | [12/512][12_512] | [12/768 (Base)][12_768] |
以下是这些模型在六个中文任务开发集上的得分:
模型 | 得分 | 书籍评论 | 情感分析 | 语义匹配 | 新闻分类(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作为训练数据。我们发现,在CLUECorpusSmall上预训练的模型表现优于在CLUECorpus2020上预训练的模型,尽管CLUECorpus2020的规模比CLUECorpusSmall大得多。
训练过程
模型在腾讯云上使用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
🔧 技术细节
文档未提供足够的技术细节(具体技术说明少于50字),因此跳过此章节。
📄 许可证
文档未提及许可证信息,因此跳过此章节。
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}
}



