模型:
uer/roberta-medium-word-chinese-cluecorpussmall
这是由 UER-py 预训练的一组5个基于词语的中文RoBERTa模型,该模型在 this paper 中进行了介绍。
大多数中文预训练模型基于汉字。与基于字符的模型相比,基于词语的模型在速度上更快(因为序列长度更短)并且根据我们的实验结果具有更好的性能。为此,我们发布了5个不同大小的基于词语的中文RoBERTa模型。为了方便用户复现结果,我们使用了公开可用的语料库和分词工具,并提供了所有训练细节。
请注意,托管推理API(右侧)的输出结果显示不正确。当预测的词语具有多个字符时,显示的是单个词语而不是整个句子。您可以点击"JSON Output"查看正常的输出结果。
您可以从以下链接下载5个中文RoBERTa微型模型,也可以从HuggingFace获取:
| Link | |
|---|---|
| word-based RoBERTa-Tiny | 12313321 |
| word-based RoBERTa-Mini | 12314321 |
| word-based RoBERTa-Small | 12315321 |
| word-based RoBERTa-Medium | 12316321 |
| word-based RoBERTa-Base | 12317321 |
与 char-based models 相比,基于词语的模型在大多数情况下取得更好的结果。以下是六个中文任务的开发集上得分:
| Model | Score | book_review | chnsenticorp | lcqmc | tnews(CLUE) | iflytek(CLUE) | ocnli(CLUE) |
|---|---|---|---|---|---|---|---|
| RoBERTa-Tiny(char) | 72.3 | 83.4 | 91.4 | 81.8 | 62.0 | 55.0 | 60.3 |
| RoBERTa-Tiny(word) | 74.4(+2.1) | 86.7 | 93.2 | 82.0 | 66.4 | 58.2 | 59.6 |
| RoBERTa-Mini(char) | 75.9 | 85.7 | 93.7 | 86.1 | 63.9 | 58.3 | 67.4 |
| RoBERTa-Mini(word) | 76.9(+1.0) | 88.5 | 94.1 | 85.4 | 66.9 | 59.2 | 67.3 |
| RoBERTa-Small(char) | 76.9 | 87.5 | 93.4 | 86.5 | 65.1 | 59.4 | 69.7 |
| RoBERTa-Small(word) | 78.4(+1.5) | 89.7 | 94.7 | 87.4 | 67.6 | 60.9 | 69.8 |
| RoBERTa-Medium(char) | 78.0 | 88.7 | 94.8 | 88.1 | 65.6 | 59.5 | 71.2 |
| RoBERTa-Medium(word) | 79.1(+1.1) | 90.0 | 95.1 | 88.0 | 67.8 | 60.6 | 73.0 |
| RoBERTa-Base(char) | 79.7 | 90.1 | 95.2 | 89.2 | 67.0 | 60.9 | 75.5 |
| RoBERTa-Base(word) | 80.4(+0.7) | 91.1 | 95.7 | 89.4 | 68.0 | 61.5 | 76.8 |
对于每个任务,我们从下列列表中选择了最佳的微调超参数,并使用序列长度为128进行训练:
您可以直接使用此模型进行掩码语言建模的流水线(以基于词语的RoBERTa-Medium为例):
>>> 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不支持句子片段,因此这里使用AlbertTokenizer。
我们使用 CLUECorpusSmall 作为训练数据。在中文分词中使用了Google的 sentencepiece 。句子片段模型是在CLUECorpusSmall语料库上训练的:
>>> 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 在 Tencent Cloud 上进行预训练。我们使用序列长度为128进行100万步的预训练,然后使用序列长度为512进行额外的25万步的预训练。我们在不同的模型大小上使用相同的超参数。