模型概述
模型特點
模型能力
使用案例
🚀 中文基於詞的RoBERTa輕量級模型
本項目提供了5個基於中文詞的RoBERTa預訓練模型,相比基於字符的模型,具有更快的處理速度和更好的性能。項目使用公開語料和分詞工具,並提供了詳細的訓練細節,方便用戶復現結果。
🚀 快速開始
你可以直接使用以下代碼示例調用模型進行掩碼語言建模任務:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='uer/roberta-medium-word-chinese-cluecorpussmall')
>>> unmasker("[MASK]的首都是北京。")
[
{'sequence': '中國 的首都是北京。',
'score': 0.21525809168815613,
'token': 2873,
'token_str': '中國'},
{'sequence': '北京 的首都是北京。',
'score': 0.15194718539714813,
'token': 9502,
'token_str': '北京'},
{'sequence': '我們 的首都是北京。',
'score': 0.08854265511035919,
'token': 4215,
'token_str': '我們'},
{'sequence': '美國 的首都是北京。',
'score': 0.06808705627918243,
'token': 7810,
'token_str': '美國'},
{'sequence': '日本 的首都是北京。',
'score': 0.06071401759982109,
'token': 7788,
'token_str': '日本'}
]
在PyTorch中使用該模型獲取給定文本的特徵:
from transformers import AlbertTokenizer, BertModel
tokenizer = AlbertTokenizer.from_pretrained('uer/roberta-medium-word-chinese-cluecorpussmall')
model = BertModel.from_pretrained("uer/roberta-medium-word-chinese-cluecorpussmall")
text = "用你喜歡的任何文本替換我。"
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
在TensorFlow中使用該模型獲取給定文本的特徵:
from transformers import AlbertTokenizer, TFBertModel
tokenizer = AlbertTokenizer.from_pretrained('uer/roberta-medium-word-chinese-cluecorpussmall')
model = TFBertModel.from_pretrained("uer/roberta-medium-word-chinese-cluecorpussmall")
text = "用你喜歡的任何文本替換我。"
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
由於BertTokenizer
不支持sentencepiece
,因此這裡使用AlbertTokenizer
。
✨ 主要特性
- 基於詞的預訓練:與基於字符的模型相比,基於詞的模型具有更快的處理速度和更好的性能。
- 多模型選擇:提供了5種不同大小的中文基於詞的RoBERTa模型,滿足不同需求。
- 公開數據和細節:使用公開可用的語料庫和分詞工具,並提供所有訓練細節,方便用戶復現結果。
📦 安裝指南
文檔中未提及安裝步驟,若有需求可參考相關依賴庫的官方文檔進行安裝,如transformers
、sentencepiece
等。
💻 使用示例
基礎用法
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='uer/roberta-medium-word-chinese-cluecorpussmall')
>>> unmasker("[MASK]的首都是北京。")
[
{'sequence': '中國 的首都是北京。',
'score': 0.21525809168815613,
'token': 2873,
'token_str': '中國'},
{'sequence': '北京 的首都是北京。',
'score': 0.15194718539714813,
'token': 9502,
'token_str': '北京'},
{'sequence': '我們 的首都是北京。',
'score': 0.08854265511035919,
'token': 4215,
'token_str': '我們'},
{'sequence': '美國 的首都是北京。',
'score': 0.06808705627918243,
'token': 7810,
'token_str': '美國'},
{'sequence': '日本 的首都是北京。',
'score': 0.06071401759982109,
'token': 7788,
'token_str': '日本'}
]
高級用法
在不同深度學習框架中使用該模型獲取給定文本的特徵:
# PyTorch
from transformers import AlbertTokenizer, BertModel
tokenizer = AlbertTokenizer.from_pretrained('uer/roberta-medium-word-chinese-cluecorpussmall')
model = BertModel.from_pretrained("uer/roberta-medium-word-chinese-cluecorpussmall")
text = "用你喜歡的任何文本替換我。"
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
# TensorFlow
from transformers import AlbertTokenizer, TFBertModel
tokenizer = AlbertTokenizer.from_pretrained('uer/roberta-medium-word-chinese-cluecorpussmall')
model = TFBertModel.from_pretrained("uer/roberta-medium-word-chinese-cluecorpussmall")
text = "用你喜歡的任何文本替換我。"
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
📚 詳細文檔
模型描述
這是由UER-py預訓練的一組5箇中文基於詞的RoBERTa模型,相關內容在這篇論文中介紹。此外,這些模型也可以通過騰訊預訓練框架TencentPretrain進行預訓練,該框架在這篇論文中介紹,它繼承了UER-py以支持參數超過十億的模型,並將其擴展為多模態預訓練框架。
大多數中文預訓練權重基於中文字符。與基於字符的模型相比,根據我們的實驗結果,基於詞的模型更快(因為序列長度更短)且性能更好。為此,我們發佈了5個不同大小的中文基於詞的RoBERTa模型。為了方便用戶復現結果,我們使用了公開可用的語料庫和分詞工具,並提供了所有訓練細節。
你可以從UER-py模型庫頁面下載這5箇中文RoBERTa輕量級模型,也可以通過HuggingFace從以下鏈接下載:
鏈接 | |
---|---|
基於詞的RoBERTa-Tiny | L=2/H=128 (Tiny) |
基於詞的RoBERTa-Mini | L=4/H=256 (Mini) |
基於詞的RoBERTa-Small | L=4/H=512 (Small) |
基於詞的RoBERTa-Medium | L=8/H=512 (Medium) |
基於詞的RoBERTa-Base | L=12/H=768 (Base) |
與基於字符的模型相比,基於詞的模型在大多數情況下取得了更好的結果。以下是六個中文任務開發集上的得分:
模型 | 得分 | 書籍評論 | 中文情感語料庫 | 中文自然語言推理 | 新聞分類(CLUE) | 訊飛文本分類(CLUE) | 中文自然語言推理(CLUE) |
---|---|---|---|---|---|---|---|
RoBERTa-Tiny(字符) | 72.3 | 83.4 | 91.4 | 81.8 | 62.0 | 55.0 | 60.3 |
RoBERTa-Tiny(詞) | 74.4(+2.1) | 86.7 | 93.2 | 82.0 | 66.4 | 58.2 | 59.6 |
RoBERTa-Mini(字符) | 75.9 | 85.7 | 93.7 | 86.1 | 63.9 | 58.3 | 67.4 |
RoBERTa-Mini(詞) | 76.9(+1.0) | 88.5 | 94.1 | 85.4 | 66.9 | 59.2 | 67.3 |
RoBERTa-Small(字符) | 76.9 | 87.5 | 93.4 | 86.5 | 65.1 | 59.4 | 69.7 |
RoBERTa-Small(詞) | 78.4(+1.5) | 89.7 | 94.7 | 87.4 | 67.6 | 60.9 | 69.8 |
RoBERTa-Medium(字符) | 78.0 | 88.7 | 94.8 | 88.1 | 65.6 | 59.5 | 71.2 |
RoBERTa-Medium(詞) | 79.1(+1.1) | 90.0 | 95.1 | 88.0 | 67.8 | 60.6 | 73.0 |
RoBERTa-Base(字符) | 79.7 | 90.1 | 95.2 | 89.2 | 67.0 | 60.9 | 75.5 |
RoBERTa-Base(詞) | 80.4(+0.7) | 91.1 | 95.7 | 89.4 | 68.0 | 61.5 | 76.8 |
對於每個任務,我們從以下列表中選擇最佳的微調超參數,並使用序列長度為128進行訓練:
- 訓練輪數:3, 5, 8
- 批次大小:32, 64
- 學習率:3e-5, 1e-4, 3e-4
訓練數據
使用CLUECorpusSmall作為訓練數據。使用谷歌的sentencepiece進行分詞。在CLUECorpusSmall語料庫上訓練sentencepiece模型:
>>> import sentencepiece as spm
>>> spm.SentencePieceTrainer.train(input='cluecorpussmall.txt',
model_prefix='cluecorpussmall_spm',
vocab_size=100000,
max_sentence_length=1024,
max_sentencepiece_length=6,
user_defined_symbols=['[MASK]','[unused1]','[unused2]',
'[unused3]','[unused4]','[unused5]','[unused6]',
'[unused7]','[unused8]','[unused9]','[unused10]'],
pad_id=0,
pad_piece='[PAD]',
unk_id=1,
unk_piece='[UNK]',
bos_id=2,
bos_piece='[CLS]',
eos_id=3,
eos_piece='[SEP]',
train_extremely_large_corpus=True
)
訓練過程
模型在騰訊雲上使用UER-py進行預訓練。我們使用序列長度為128進行1000000步的預訓練,然後使用序列長度為512進行額外250000步的預訓練。我們在不同模型大小上使用相同的超參數。
以基於詞的RoBERTa-Medium為例:
階段1
python3 preprocess.py --corpus_path corpora/cluecorpussmall.txt \
--spm_model_path models/cluecorpussmall_spm.model \
--dataset_path cluecorpussmall_word_seq128_dataset.pt \
--processes_num 32 --seq_length 128 \
--dynamic_masking --data_processor mlm
python3 pretrain.py --dataset_path cluecorpussmall_word_seq128_dataset.pt \
--spm_model_path models/cluecorpussmall_spm.model \
--config_path models/bert/medium_config.json \
--output_model_path models/cluecorpussmall_word_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 \
--spm_model_path models/cluecorpussmall_spm.model \
--dataset_path cluecorpussmall_word_seq512_dataset.pt \
--processes_num 32 --seq_length 512 \
--dynamic_masking --data_processor mlm
python3 pretrain.py --dataset_path cluecorpussmall_word_seq512_dataset.pt \
--spm_model_path models/cluecorpussmall_spm.model \
--pretrained_model_path models/cluecorpussmall_word_roberta_medium_seq128_model.bin-1000000 \
--config_path models/bert/medium_config.json \
--output_model_path models/cluecorpussmall_word_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_word_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{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}
}



