在英语语言上预训练的模型,使用了遮蔽语言模型(MLM)目标。它在2019年被引入,并在同年首次发布。该模型区分大小写:它将english和English视为不同。
免责声明:发布RoBERTa的团队并未为该模型编写模型卡片,因此此模型卡片由Hugging Face团队撰写。
RoBERTa是一个在大型英文语料库上进行自监督预训练的transformers模型。这意味着它只使用原始文本进行了预训练,而没有以任何方式对其进行人工标记(这就是为什么它可以使用大量公开可用的数据),使用自动化过程从这些文本中生成输入和标签。
更具体地说,它是用遮蔽语言模型(MLM)目标进行预训练的。模型随机屏蔽输入中的15%单词,然后通过整个屏蔽的句子运行,然后需要预测屏蔽的单词。这与通常一次看到一个单词的传统循环神经网络(RNN)或像GPT这样在内部屏蔽未来标记的自回归模型不同。它使模型能够学习句子的双向表示。
通过这种方式,模型学习了英语语言的内部表示,然后可以用于提取对下游任务有用的特征:例如,如果您有一个带标签句子的数据集,您可以使用BERT模型产生的特征作为输入训练标准分类器。
您可以使用原始模型进行遮蔽语言建模,但这主要用于在下游任务上进行微调。您可以查看您感兴趣的任务上进行微调的版本。
请注意,此模型主要用于在使用整个句子(可能遮蔽)进行决策的任务上进行微调,例如序列分类、标记分类或问答。对于文本生成等任务,您应该查看像GPT2这样的模型。
您可以使用这个模型直接通过一个用于遮蔽语言建模的流程进行使用:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='roberta-base')
>>> unmasker("Hello I'm a <mask> model.")
[{'sequence': "<s>Hello I'm a male model.</s>",
'score': 0.3306540250778198,
'token': 2943,
'token_str': 'Ġmale'},
{'sequence': "<s>Hello I'm a female model.</s>",
'score': 0.04655390977859497,
'token': 2182,
'token_str': 'Ġfemale'},
{'sequence': "<s>Hello I'm a professional model.</s>",
'score': 0.04232972860336304,
'token': 2038,
'token_str': 'Ġprofessional'},
{'sequence': "<s>Hello I'm a fashion model.</s>",
'score': 0.037216778844594955,
'token': 2734,
'token_str': 'Ġfashion'},
{'sequence': "<s>Hello I'm a Russian model.</s>",
'score': 0.03253649175167084,
'token': 1083,
'token_str': 'ĠRussian'}]
以下是如何在PyTorch中使用此模型来获取给定文本的特征:
from transformers import RobertaTokenizer, RobertaModel
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = RobertaModel.from_pretrained('roberta-base')
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
以及在TensorFlow中:
from transformers import RobertaTokenizer, TFRobertaModel
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = TFRobertaModel.from_pretrained('roberta-base')
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
此模型的训练数据包含来自互联网的大量未经筛选的内容,远非中立。因此,模型可能会产生偏见的预测:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='roberta-base')
>>> unmasker("The man worked as a <mask>.")
[{'sequence': '<s>The man worked as a mechanic.</s>',
'score': 0.08702439814805984,
'token': 25682,
'token_str': 'Ġmechanic'},
{'sequence': '<s>The man worked as a waiter.</s>',
'score': 0.0819653645157814,
'token': 38233,
'token_str': 'Ġwaiter'},
{'sequence': '<s>The man worked as a butcher.</s>',
'score': 0.073323555290699,
'token': 32364,
'token_str': 'Ġbutcher'},
{'sequence': '<s>The man worked as a miner.</s>',
'score': 0.046322137117385864,
'token': 18678,
'token_str': 'Ġminer'},
{'sequence': '<s>The man worked as a guard.</s>',
'score': 0.040150221437215805,
'token': 2510,
'token_str': 'Ġguard'}]
>>> unmasker("The Black woman worked as a <mask>.")
[{'sequence': '<s>The Black woman worked as a waitress.</s>',
'score': 0.22177888453006744,
'token': 35698,
'token_str': 'Ġwaitress'},
{'sequence': '<s>The Black woman worked as a prostitute.</s>',
'score': 0.19288744032382965,
'token': 36289,
'token_str': 'Ġprostitute'},
{'sequence': '<s>The Black woman worked as a maid.</s>',
'score': 0.06498628109693527,
'token': 29754,
'token_str': 'Ġmaid'},
{'sequence': '<s>The Black woman worked as a secretary.</s>',
'score': 0.05375480651855469,
'token': 2971,
'token_str': 'Ġsecretary'},
{'sequence': '<s>The Black woman worked as a nurse.</s>',
'score': 0.05245552211999893,
'token': 9008,
'token_str': 'Ġnurse'}]
这种偏见也会影响到此模型的所有微调版本。
RoBERTa模型在以下五个数据集的结合上进行了预训练:
总共这些数据集的文本量达到了160GB。
文本使用基于字节的字节对编码(BPE)进行标记化,并使用50,000个词汇表大小。模型的输入使用包含512个连续标记的片段,这些片段可以跨越多个文档。新文档的开头用""标记,结束用""标记。
每个句子的屏蔽过程的详细信息如下:
与BERT不同,屏蔽是在预训练期间动态进行的(例如,它在每个纪元中都会发生变化,而不是固定的)。
该模型使用1024个V100 GPU进行了500K步的训练,批量大小为8K,序列长度为512。使用的优化器是Adam,学习率为6e-4, β 1 = 0.9, β 2 = 0.98,ε = 1e-6,权重衰减率为0.01,学习率在训练的前24,000步进行预热,然后进行线性衰减。
在下游任务上微调时,该模型实现了以下结果:
GLUE测试结果:
| Task | MNLI | QQP | QNLI | SST-2 | CoLA | STS-B | MRPC | RTE |
|---|---|---|---|---|---|---|---|---|
| 87.6 | 91.9 | 92.8 | 94.8 | 63.6 | 91.2 | 90.2 | 78.7 |
@article{DBLP:journals/corr/abs-1907-11692,
author = {Yinhan Liu and
Myle Ott and
Naman Goyal and
Jingfei Du and
Mandar Joshi and
Danqi Chen and
Omer Levy and
Mike Lewis and
Luke Zettlemoyer and
Veselin Stoyanov},
title = {RoBERTa: {A} Robustly Optimized {BERT} Pretraining Approach},
journal = {CoRR},
volume = {abs/1907.11692},
year = {2019},
url = {http://arxiv.org/abs/1907.11692},
archivePrefix = {arXiv},
eprint = {1907.11692},
timestamp = {Thu, 01 Aug 2019 08:59:33 +0200},
biburl = {https://dblp.org/rec/journals/corr/abs-1907-11692.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}