基于英语语言、使用掩码语言模型(MLM)目标训练的预训练模型。该模型在此论文中被首次提出,并在此仓库中首次发布。本模型区分大小写:例如,“english”和“English”会被视为不同的词。
免责声明:发布 BERT 的团队未为此模型撰写模型卡片,因此本模型卡片由 Hugging Face 团队编写。
BERT 是一个基于 Transformer 的模型,它在大规模英语语料库上以自监督的方式进行预训练。这意味着它仅在原始文本上进行预训练,无需人工对文本进行任何形式的标注(这也是它能够利用大量公开可用数据的原因),并通过自动流程从这些文本中生成输入和标签。更准确地说,它是在两个目标下进行预训练的:
通过这种方式,模型学习到英语语言的内部表示,这些表示随后可用于提取对下游任务有用的特征:例如,如果你有一个带标签的句子数据集,你可以使用 BERT 模型生成的特征作为输入来训练一个标准分类器。
您可以将原始模型用于掩码语言建模或下一句预测,但它主要旨在针对下游任务进行微调。请参阅模型中心,查找您感兴趣的任务的微调版本。
请注意,此模型主要用于在需要使用整个句子(可能包含掩码)进行决策的任务上进行微调,例如序列分类、 token 分类或问答。对于文本生成等任务,您应该考虑 GPT2 等模型。
您可以直接通过 pipeline 将此模型用于掩码语言建模:
>>> from mindnlp.transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-cased')
>>> unmasker("Hello I'm a [MASK] model.")
[{'sequence': "[CLS] Hello I'm a fashion model. [SEP]",
'score': 0.09019174426794052,
'token': 4633,
'token_str': 'fashion'},
{'sequence': "[CLS] Hello I'm a new model. [SEP]",
'score': 0.06349995732307434,
'token': 1207,
'token_str': 'new'},
{'sequence': "[CLS] Hello I'm a male model. [SEP]",
'score': 0.06228214129805565,
'token': 2581,
'token_str': 'male'},
{'sequence': "[CLS] Hello I'm a professional model. [SEP]",
'score': 0.0441727414727211,
'token': 1848,
'token_str': 'professional'},
{'sequence': "[CLS] Hello I'm a super model. [SEP]",
'score': 0.03326151892542839,
'token': 7688,
'token_str': 'super'}]以下是如何在 MindSpore 中使用该模型获取给定文本特征的方法:
from mindnlp.transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
model = BertModel.from_pretrained("bert-base-cased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='ms')
output = model(**encoded_input)或
from mindnlp.transformers import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
model = TFBertModel.from_pretrained("bert-base-cased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='ms')
output = model(encoded_input)即便用于训练此模型的数据可被描述为相当中立,该模型仍可能产生有偏差的预测:
>>> from mindnlp.transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-cased')
>>> unmasker("The man worked as a [MASK].")
[{'sequence': '[CLS] The man worked as a lawyer. [SEP]',
'score': 0.04804691672325134,
'token': 4545,
'token_str': 'lawyer'},
{'sequence': '[CLS] The man worked as a waiter. [SEP]',
'score': 0.037494491785764694,
'token': 17989,
'token_str': 'waiter'},
{'sequence': '[CLS] The man worked as a cop. [SEP]',
'score': 0.035512614995241165,
'token': 9947,
'token_str': 'cop'},
{'sequence': '[CLS] The man worked as a detective. [SEP]',
'score': 0.031271643936634064,
'token': 9140,
'token_str': 'detective'},
{'sequence': '[CLS] The man worked as a doctor. [SEP]',
'score': 0.027423162013292313,
'token': 3995,
'token_str': 'doctor'}]
>>> unmasker("The woman worked as a [MASK].")
[{'sequence': '[CLS] The woman worked as a nurse. [SEP]',
'score': 0.16927455365657806,
'token': 7439,
'token_str': 'nurse'},
{'sequence': '[CLS] The woman worked as a waitress. [SEP]',
'score': 0.1501094549894333,
'token': 15098,
'token_str': 'waitress'},
{'sequence': '[CLS] The woman worked as a maid. [SEP]',
'score': 0.05600163713097572,
'token': 13487,
'token_str': 'maid'},
{'sequence': '[CLS] The woman worked as a housekeeper. [SEP]',
'score': 0.04838843643665314,
'token': 26458,
'token_str': 'housekeeper'},
{'sequence': '[CLS] The woman worked as a cook. [SEP]',
'score': 0.029980547726154327,
'token': 9834,
'token_str': 'cook'}]这种偏差也会影响该模型的所有微调版本。
BERT 模型是在 BookCorpus 和 English Wikipedia(不包含列表、表格和标题)上进行预训练的。其中,BookCorpus 是一个包含 11,038 本未出版书籍的数据集。
文本使用 WordPiece 进行分词,词汇表大小为 30,000。然后,模型的输入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]以 0.5 的概率,句子 A 和句子 B 对应原始语料库中的两个连续句子,在其他情况下,则是语料库中的另一个随机句子。请注意,此处所指的“句子”是一段连续的文本,通常比单个句子更长。唯一的限制是,这两个“句子”组合后的总长度需少于 512 个 tokens。
每个句子的掩码处理细节如下:
[MASK]。该模型在采用 Pod 配置的 4 个云 TPU(总共 16 个 TPU 芯片)上进行训练,训练步数为一百万步,批处理大小为 256。在 90% 的训练步骤中,序列长度限制为 128 个 tokens,在剩余 10% 的步骤中则为 512 个 tokens。使用的优化器为 Adam,学习率为 1e-4,$\beta_{1} = 0.9$,$\beta_{2} = 0.999$,权重衰减为 0.01,学习率预热步数为 10,000 步,之后学习率呈线性衰减。
当在下游任务上进行微调时,该模型取得了以下结果:
GLUE 测试结果:
| 任务 | MNLI-(m/mm) | QQP | QNLI | SST-2 | CoLA | STS-B | MRPC | RTE | 平均值 |
|---|---|---|---|---|---|---|---|---|---|
| 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}
}