模型:
bert-large-uncased
BERT是一个在英语语料库上进行自监督训练的transformers模型。这意味着它仅使用原始文本进行预训练,而不会以任何方式对其进行标记(这就是为什么它可以使用大量公开可用的数据),它通过自动过程从这些文本中生成输入和标签。更确切地说,它通过以下两个目标进行预训练:
通过这种方式,该模型学习到了英语语言的内部表示,这可以用于提取对下游任务有用的特征:例如,如果您有一个带标签的句子数据集,可以使用由BERT模型生成的特征作为输入训练标准分类器。
此模型具有以下配置:
您可以直接使用原始模型进行掩码语言建模或下一个句子预测,但它主要用于在下游任务上进行微调。查看你感兴趣的任务的精调版本,请参阅 model hub 。
请注意,此模型主要旨在对使用整个句子(可能是掩码的)进行决策的任务进行微调,例如序列分类、标记分类或问题回答。对于文本生成等任务,您应该查看像GPT2这样的模型。
您可以使用管道直接使用此模型进行掩码语言建模:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-large-uncased')
>>> unmasker("Hello I'm a [MASK] model.")
[{'sequence': "[CLS] hello i'm a fashion model. [SEP]",
'score': 0.1886913776397705,
'token': 4827,
'token_str': 'fashion'},
{'sequence': "[CLS] hello i'm a professional model. [SEP]",
'score': 0.07157472521066666,
'token': 2658,
'token_str': 'professional'},
{'sequence': "[CLS] hello i'm a male model. [SEP]",
'score': 0.04053466394543648,
'token': 3287,
'token_str': 'male'},
{'sequence': "[CLS] hello i'm a role model. [SEP]",
'score': 0.03891477733850479,
'token': 2535,
'token_str': 'role'},
{'sequence': "[CLS] hello i'm a fitness model. [SEP]",
'score': 0.03038121573626995,
'token': 10516,
'token_str': 'fitness'}]
以下是如何在PyTorch中使用此模型获取给定文本的特征:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')
model = BertModel.from_pretrained("bert-large-uncased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
以及在TensorFlow中:
from transformers import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')
model = TFBertModel.from_pretrained("bert-large-uncased")
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='bert-large-uncased')
>>> unmasker("The man worked as a [MASK].")
[{'sequence': '[CLS] the man worked as a bartender. [SEP]',
'score': 0.10426565259695053,
'token': 15812,
'token_str': 'bartender'},
{'sequence': '[CLS] the man worked as a waiter. [SEP]',
'score': 0.10232779383659363,
'token': 15610,
'token_str': 'waiter'},
{'sequence': '[CLS] the man worked as a mechanic. [SEP]',
'score': 0.06281787157058716,
'token': 15893,
'token_str': 'mechanic'},
{'sequence': '[CLS] the man worked as a lawyer. [SEP]',
'score': 0.050936125218868256,
'token': 5160,
'token_str': 'lawyer'},
{'sequence': '[CLS] the man worked as a carpenter. [SEP]',
'score': 0.041034240275621414,
'token': 10533,
'token_str': 'carpenter'}]
>>> unmasker("The woman worked as a [MASK].")
[{'sequence': '[CLS] the woman worked as a waitress. [SEP]',
'score': 0.28473711013793945,
'token': 13877,
'token_str': 'waitress'},
{'sequence': '[CLS] the woman worked as a nurse. [SEP]',
'score': 0.11336520314216614,
'token': 6821,
'token_str': 'nurse'},
{'sequence': '[CLS] the woman worked as a bartender. [SEP]',
'score': 0.09574324637651443,
'token': 15812,
'token_str': 'bartender'},
{'sequence': '[CLS] the woman worked as a maid. [SEP]',
'score': 0.06351090222597122,
'token': 10850,
'token_str': 'maid'},
{'sequence': '[CLS] the woman worked as a secretary. [SEP]',
'score': 0.048970773816108704,
'token': 3187,
'token_str': 'secretary'}]
此偏差也会影响该模型的所有经过微调的版本。
BERT模型在 BookCorpus 上进行了预训练,该数据集包含11,038本未公开的图书和 English Wikipedia (不包括列表、表和标题)。
文本使用WordPiece和词汇量为30,000进行小写化和标记化。模型的输入格式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]
在50%的情况下,句子A和句子B对应于原始语料库中的两个连续句子;在其他情况下,它是语料库中的另一个随机句子。请注意,这里认为的一个句子是一个连续的文本段落,通常比一个句子长。唯一的限制是带有这两个“句子”的结果总长度小于512个令牌。
每个句子的掩码过程的详细信息如下:
该模型在4个云TPU(16个TPU芯片总共)上进行了一百万个步骤的预训练,批量大小为256。90%的步骤的序列长度限制为128个标记,而剩余的10%为512个。使用的优化器是学习率为1e-4的Adam, β 1 = 0.9 \beta_{1} = 0.9 β 1 = 0 . 9 , β 2 = 0.999 \beta_{2} = 0.999 β 2 = 0 . 9 9 9 ,权重衰减为0.01,学习率预热步骤为10,000步,学习率线性衰减。
在经过微调的下游任务上,该模型实现了以下结果:
| Model | SQUAD 1.1 F1/EM | Multi NLI Accuracy |
|---|---|---|
| BERT-Large, Uncased (Original) | 91.0/84.3 | 86.05 |
@article{DBLP:journals/corr/abs-1810-04805,
author = {Jacob Devlin and
Ming{-}Wei Chang and
Kenton Lee and
Kristina Toutanova},
title = {{BERT:} Pre-training of Deep Bidirectional Transformers for Language
Understanding},
journal = {CoRR},
volume = {abs/1810.04805},
year = {2018},
url = {http://arxiv.org/abs/1810.04805},
archivePrefix = {arXiv},
eprint = {1810.04805},
timestamp = {Tue, 30 Oct 2018 20:39:56 +0100},
biburl = {https://dblp.org/rec/journals/corr/abs-1810-04805.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}