模型:
预训练模型,使用掩码语言建模(MLM)目标,在英语文本上进行预训练。它于 this paper 年推出,并于 this repository 年首次发布。此模型是不区分大小写的:它不区分英语和English。
免责声明:发布BERT的团队没有为该模型编写模型卡片,因此Hugging Face团队编写了此模型卡片。
BERT 是一个在自我监督的方式下,基于大型英语数据集进行预训练的transformers 模型。这意味着它仅对原始文本进行了预训练,没有以任何方式人为标记它们(这就是为什么它可以使用大量的公开可用数据) ,并使用自动过程从这些文本中生成输入和标签。更确切地说,它使用两个目标进行预训练:
通过这种方式,模型学习了英语语言的内在表示,可以用来提取对下游任务有用的特征:例如,如果你有一个带有标签的句子数据集,你可以使用BERT模型生成的特征作为输入训练一个标准的分类器。
您可以使用原始模型进行掩码语言建模或下一句预测,但它主要用于在下游任务上进行微调。请参阅 model hub ,查找您感兴趣的任务上的微调版本。
请注意,此模型主要用于在使用整个句子(可能被掩码)进行决策的任务上进行微调,例如序列分类、标记分类或问题回答。对于文本生成等任务,您应该查看GPT2等模型。
您可以直接使用此模型进行掩码语言建模的管道:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-uncased')
>>> unmasker("Hello I'm a [MASK] model.")
[{'sequence': "[CLS] hello i'm a fashion model. [SEP]",
'score': 0.1073106899857521,
'token': 4827,
'token_str': 'fashion'},
{'sequence': "[CLS] hello i'm a role model. [SEP]",
'score': 0.08774490654468536,
'token': 2535,
'token_str': 'role'},
{'sequence': "[CLS] hello i'm a new model. [SEP]",
'score': 0.05338378623127937,
'token': 2047,
'token_str': 'new'},
{'sequence': "[CLS] hello i'm a super model. [SEP]",
'score': 0.04667217284440994,
'token': 3565,
'token_str': 'super'},
{'sequence': "[CLS] hello i'm a fine model. [SEP]",
'score': 0.027095865458250046,
'token': 2986,
'token_str': 'fine'}]
以下是如何在PyTorch中使用此模型获取给定文本的特征的方法:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained("bert-base-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-base-uncased')
model = TFBertModel.from_pretrained("bert-base-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-base-uncased')
>>> unmasker("The man worked as a [MASK].")
[{'sequence': '[CLS] the man worked as a carpenter. [SEP]',
'score': 0.09747550636529922,
'token': 10533,
'token_str': 'carpenter'},
{'sequence': '[CLS] the man worked as a waiter. [SEP]',
'score': 0.0523831807076931,
'token': 15610,
'token_str': 'waiter'},
{'sequence': '[CLS] the man worked as a barber. [SEP]',
'score': 0.04962705448269844,
'token': 13362,
'token_str': 'barber'},
{'sequence': '[CLS] the man worked as a mechanic. [SEP]',
'score': 0.03788609802722931,
'token': 15893,
'token_str': 'mechanic'},
{'sequence': '[CLS] the man worked as a salesman. [SEP]',
'score': 0.037680890411138535,
'token': 18968,
'token_str': 'salesman'}]
>>> unmasker("The woman worked as a [MASK].")
[{'sequence': '[CLS] the woman worked as a nurse. [SEP]',
'score': 0.21981462836265564,
'token': 6821,
'token_str': 'nurse'},
{'sequence': '[CLS] the woman worked as a waitress. [SEP]',
'score': 0.1597415804862976,
'token': 13877,
'token_str': 'waitress'},
{'sequence': '[CLS] the woman worked as a maid. [SEP]',
'score': 0.1154729500412941,
'token': 10850,
'token_str': 'maid'},
{'sequence': '[CLS] the woman worked as a prostitute. [SEP]',
'score': 0.037968918681144714,
'token': 19215,
'token_str': 'prostitute'},
{'sequence': '[CLS] the woman worked as a cook. [SEP]',
'score': 0.03042375110089779,
'token': 5660,
'token_str': 'cook'}]
这种偏见也会影响该模型的所有被微调的版本。
BERT模型在 BookCorpus 上进行了预训练,其中包括11,038本未发表的书籍和 English Wikipedia (不包括列表、表格和标题)。
文本经过小写处理,并使用WordPiece进行分词,词汇表大小为30,000。模型的输入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]
有50%的概率,句子A和句子B对应于原始文本中的两个连续句子,其他情况下,是语料库中的另一句随机句子。请注意,这里所说的句子是指通常比单个句子长的连续文本段落。唯一的约束条件是两个"句子"的结果具有少于512个标记的合并长度。
每个句子的屏蔽过程的详细信息如下:
该模型使用4台云TPU(总共16个TPU芯片)在Pod配置中进行了一百万步的训练,批处理大小为256。对于90%的步骤,序列长度限制为128个标记,对于剩余的10%,限制为512个标记。使用的优化器是Adam,学习率为1e-4,β 1 = 0.9,β 2 = 0.999,权重衰减为0.01,学习率热身为10,000步,然后线性衰减学习率。
在下游任务上进行微调时,该模型实现了以下结果:
Glue测试结果:
| Task | MNLI-(m/mm) | QQP | QNLI | SST-2 | CoLA | STS-B | MRPC | RTE | Average |
|---|---|---|---|---|---|---|---|---|---|
| 84.6/83.4 | 71.2 | 90.5 | 93.5 | 52.1 | 85.8 | 88.9 | 66.4 | 79.6 |
@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}
}