模型概述
模型特點
模型能力
使用案例
🚀 RoBERTa大模型
RoBERTa大模型是一個使用掩碼語言建模(MLM)目標在英文語料上預訓練的模型。它能學習到英文的雙向表示,其特徵可用於下游任務。該模型區分大小寫,對“english”和“English”會有不同處理。
🚀 快速開始
你可以直接使用此模型進行掩碼語言建模,不過它主要用於在下游任務中進行微調。可查看模型中心,尋找針對你感興趣任務的微調版本。
✨ 主要特性
- 雙向表示學習:通過掩碼語言建模(MLM)目標,模型能學習到句子的雙向表示,這與傳統的循環神經網絡(RNNs)和自迴歸模型(如GPT)不同。
- 可用於下游任務:學習到的英文內部表示可提取特徵,用於下游任務,如使用BERT模型生成的特徵作為輸入來訓練標準分類器。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
你可以使用管道進行掩碼語言建模:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='roberta-large')
>>> unmasker("Hello I'm a <mask> model.")
[{'sequence': "<s>Hello I'm a male model.</s>",
'score': 0.3317350447177887,
'token': 2943,
'token_str': 'Ġmale'},
{'sequence': "<s>Hello I'm a fashion model.</s>",
'score': 0.14171843230724335,
'token': 2734,
'token_str': 'Ġfashion'},
{'sequence': "<s>Hello I'm a professional model.</s>",
'score': 0.04291723668575287,
'token': 2038,
'token_str': 'Ġprofessional'},
{'sequence': "<s>Hello I'm a freelance model.</s>",
'score': 0.02134818211197853,
'token': 18150,
'token_str': 'Ġfreelance'},
{'sequence': "<s>Hello I'm a young model.</s>",
'score': 0.021098261699080467,
'token': 664,
'token_str': 'Ġyoung'}]
高級用法
以下是在PyTorch中使用此模型獲取給定文本特徵的方法:
from transformers import RobertaTokenizer, RobertaModel
tokenizer = RobertaTokenizer.from_pretrained('roberta-large')
model = RobertaModel.from_pretrained('roberta-large')
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-large')
model = TFRobertaModel.from_pretrained('roberta-large')
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
📚 詳細文檔
預期用途和侷限性
此模型可用於掩碼語言建模,但主要用於下游任務的微調。該模型主要針對使用整個句子(可能是掩碼後的句子)進行決策的任務進行微調,如序列分類、標記分類或問答任務。對於文本生成等任務,你應該考慮像GPT2這樣的模型。
侷限性和偏差
該模型的訓練數據包含大量來自互聯網的未過濾內容,因此模型可能存在有偏差的預測,這種偏差也會影響該模型的所有微調版本。示例如下:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='roberta-large')
>>> unmasker("The man worked as a <mask>.")
[{'sequence': '<s>The man worked as a mechanic.</s>',
'score': 0.08260300755500793,
'token': 25682,
'token_str': 'Ġmechanic'},
{'sequence': '<s>The man worked as a driver.</s>',
'score': 0.05736079439520836,
'token': 1393,
'token_str': 'Ġdriver'},
{'sequence': '<s>The man worked as a teacher.</s>',
'score': 0.04709019884467125,
'token': 3254,
'token_str': 'Ġteacher'},
{'sequence': '<s>The man worked as a bartender.</s>',
'score': 0.04641604796051979,
'token': 33080,
'token_str': 'Ġbartender'},
{'sequence': '<s>The man worked as a waiter.</s>',
'score': 0.04239227622747421,
'token': 38233,
'token_str': 'Ġwaiter'}]
>>> unmasker("The woman worked as a <mask>.")
[{'sequence': '<s>The woman worked as a nurse.</s>',
'score': 0.2667474150657654,
'token': 9008,
'token_str': 'Ġnurse'},
{'sequence': '<s>The woman worked as a waitress.</s>',
'score': 0.12280137836933136,
'token': 35698,
'token_str': 'Ġwaitress'},
{'sequence': '<s>The woman worked as a teacher.</s>',
'score': 0.09747499972581863,
'token': 3254,
'token_str': 'Ġteacher'},
{'sequence': '<s>The woman worked as a secretary.</s>',
'score': 0.05783602222800255,
'token': 2971,
'token_str': 'Ġsecretary'},
{'sequence': '<s>The woman worked as a cleaner.</s>',
'score': 0.05576248839497566,
'token': 16126,
'token_str': 'Ġcleaner'}]
訓練數據
RoBERTa模型在五個數據集的組合上進行預訓練:
- BookCorpus,一個由11,038本未出版書籍組成的數據集;
- 英文維基百科(不包括列表、表格和標題);
- CC-News,一個包含2016年9月至2019年2月期間抓取的6300萬篇英文新聞文章的數據集。
- OpenWebText,一個用於訓練GPT - 2的WebText數據集的開源復刻版。
- Stories,一個包含CommonCrawl數據子集的數據集,經過過濾以匹配Winograd模式的故事風格。
這些數據集總共包含160GB的文本。
訓練過程
預處理
文本使用字節版本的字節對編碼(BPE)進行分詞,詞彙表大小為50,000。模型的輸入採用512個連續標記的片段,這些片段可能跨越多個文檔。新文檔的開頭用<s>
標記,結尾用</s>
標記。
每個句子的掩碼過程細節如下:
- 15%的標記被掩碼。
- 80%的情況下,被掩碼的標記被
<mask>
替換。 - 10%的情況下,被掩碼的標記被一個與它們所替換的標記不同的隨機標記替換。
- 10%的情況下,被掩碼的標記保持不變。
與BERT不同,掩碼在預訓練期間是動態進行的(例如,它在每個epoch都會改變,而不是固定的)。
預訓練
該模型在1024個V100 GPU上訓練了500K步,批量大小為8K,序列長度為512。使用的優化器是Adam,學習率為4e - 4,\(\beta_{1} = 0.9\),\(\beta_{2} = 0.98\),\(\epsilon = 1e - 6\),權重衰減為0.01,學習率在30,000步內進行預熱,之後學習率線性衰減。
評估結果
在下游任務上進行微調時,該模型取得了以下結果:
Glue測試結果:
任務 | MNLI | QQP | QNLI | SST - 2 | CoLA | STS - B | MRPC | RTE |
---|---|---|---|---|---|---|---|---|
90.2 | 92.2 | 94.7 | 96.4 | 68.0 | 96.4 | 90.9 | 86.6 |
BibTeX引用和引用信息
@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}
}
可視化工具

📄 許可證
本項目採用MIT許可證。



