RoBERTa

模型描述

Transformer 双向编码器表示(Bidirectional Encoder Representations from Transformers),即 BERT,是一种革命性的自监督预训练技术,旨在学习预测文本中被刻意隐藏(掩码)的部分。至关重要的是,BERT 所学习到的表示已被证明能很好地泛化到下游任务中。2018 年 BERT 首次发布时,它在许多自然语言处理(NLP)基准数据集上均取得了业内领先水平的效果。

RoBERTa 在 BERT 的语言掩码策略基础上进行了构建,并修改了 BERT 中的关键超参数,包括移除了 BERT 的下一句预测预训练目标,并使用更大的小批量(mini-batch)和学习率进行训练。此外,RoBERTa 的训练数据量比 BERT 多出一个数量级,且训练时间更长。这使得 RoBERTa 的表示相比 BERT 能更好地泛化到下游任务中。

需求

我们需要一些额外的 Python 依赖项来进行预处理

pip install regex requests hydra-core omegaconf

示例

加载 RoBERTa
import torch
roberta = torch.hub.load('pytorch/fairseq', 'roberta.large')
roberta.eval()  # disable dropout (or leave in train mode to finetune)
对输入文本应用字节对编码(BPE)
tokens = roberta.encode('Hello world!')
assert tokens.tolist() == [0, 31414, 232, 328, 2]
assert roberta.decode(tokens) == 'Hello world!'
从 RoBERTa 中提取特征
# Extract the last layer's features
last_layer_features = roberta.extract_features(tokens)
assert last_layer_features.size() == torch.Size([1, 5, 1024])

# Extract all layer's features (layer 0 is the embedding layer)
all_layers = roberta.extract_features(tokens, return_all_hiddens=True)
assert len(all_layers) == 25
assert torch.all(all_layers[-1] == last_layer_features)
将 RoBERTa 用于句子对分类任务
# Download RoBERTa already finetuned for MNLI
roberta = torch.hub.load('pytorch/fairseq', 'roberta.large.mnli')
roberta.eval()  # disable dropout for evaluation

with torch.no_grad():
    # Encode a pair of sentences and make a prediction
    tokens = roberta.encode('Roberta is a heavily optimized version of BERT.', 'Roberta is not very optimized.')
    prediction = roberta.predict('mnli', tokens).argmax().item()
    assert prediction == 0  # contradiction

    # Encode another pair of sentences
    tokens = roberta.encode('Roberta is a heavily optimized version of BERT.', 'Roberta is based on BERT.')
    prediction = roberta.predict('mnli', tokens).argmax().item()
    assert prediction == 2  # entailment
注册一个新的(随机初始化)分类头
roberta.register_classification_head('new_task', num_classes=3)
logprobs = roberta.predict('new_task', tokens)  # tensor([[-1.1050, -1.0672, -1.1245]], grad_fn=<LogSoftmaxBackward>)

参考文献

一种稳健优化的 BERT 预训练方法

模型类型: Nlp
提交方: Facebook AI (fairseq 团队)