基于英语语言、采用掩码语言模型(MLM)目标训练的预训练模型。该模型在此论文中被提出,并首次在此仓库中发布。此模型为不区分大小写版本:即“english”和“English”对模型而言没有区别。
在原始README中添加了CANN版本依赖说明,并修改了示例代码部分。
BERT是一种基于Transformer的模型,它在大量英语语料库上以自监督的方式进行预训练。这意味着它仅在原始文本上进行预训练,无需人工对文本进行任何形式的标注(因此可以利用大量公开可用的数据),并通过自动流程从这些文本中生成输入和标签。更准确地说,它通过两个目标进行预训练:
通过这种方式,模型学习到英语语言的内部表示,该表示可用于提取对下游任务有用的特征:例如,如果您有一个带标签的句子数据集,您可以使用BERT模型生成的特征作为输入来训练一个标准分类器。
该模型具有以下配置:
您可以将原始模型用于掩码语言建模或下一句预测,但该模型主要旨在针对下游任务进行微调。
请注意,此模型主要用于在需要使用整个句子(可能包含掩码)来做出决策的任务上进行微调,例如序列分类、 token 分类或问答任务。对于文本生成等任务,您应考虑 GPT2 等模型。
您可以直接通过 pipeline 将此模型用于掩码语言建模:
>>> from openmind import pipeline
>>> unmasker = pipeline('fill-mask', model='PyTorch-NPU/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('PyTorch-NPU/bert_large_uncased')
model = BertModel.from_pretrained("PyTorch-NPU/bert_large_uncased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)即便用于训练此模型的数据集可被描述为相当中立,该模型仍可能产生带有偏见的预测:
>>> from openmind import pipeline
>>> unmasker = pipeline('fill-mask', model='PyTorch-NPU/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 和 English Wikipedia(不含列表、表格和标题)上进行预训练。其中,BookCorpus 是一个包含 11,038 本未出版书籍的数据集。
文本会先进行小写处理,再使用 WordPiece 进行分词,词汇表大小为 30,000。然后,模型的输入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]以 0.5 的概率,句子 A 和句子 B 对应原始语料库中的两个连续句子,在其他情况下,则是语料库中的另一个随机句子。请注意,此处所指的“句子”是一段连续的文本,通常比单个句子更长。唯一的限制是,这两个“句子”组合后的总长度需少于 512 个 token。
每个句子的掩码处理细节如下:
[MASK]。该模型在采用 Pod 配置的 4 个云 TPU(共 16 个 TPU 芯片)上进行训练,训练步数为一百万步,批处理大小为 256。在 90% 的训练步骤中,序列长度限制为 128 个 token,其余 10% 的步骤中则限制为 512 个 token。使用的优化器为 Adam,学习率为 1e-4,$\beta_{1} = 0.9$,$\beta_{2} = 0.999$,权重衰减为 0.01,学习率预热步数为 10,000 步,之后学习率呈线性衰减。
当在下游任务上进行微调时,该模型取得了以下结果:
| 模型 | SQUAD 1.1 F1/EM | Multi NLI 准确率 |
|---|---|---|
| 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}
}