language: zh
tags:
- exbert
license: apache-2.0
datasets:
- bookcorpus
- wikipedia
BERT基础模型(未区分大小写)
该模型是基于英语语料库,通过掩码语言建模(MLM)目标进行预训练的模型。它在这篇论文中首次提出,并在此代码库中首次发布。此模型不区分大小写:即不区分"english"和"English"。
免责声明:发布BERT的团队未为此模型编写模型卡,因此本模型卡由Hugging Face团队编写。
模型描述
BERT是一个基于大规模英语语料库,以自监督方式预训练的Transformer模型。这意味着它仅对原始文本进行预训练,无需人工标注(因此可以利用大量公开可用的数据),并通过自动过程从这些文本生成输入和标签。具体来说,它通过以下两个目标进行预训练:
- 掩码语言建模(MLM):模型随机掩盖输入句子中15%的单词,然后通过整个被掩盖的句子来预测被掩盖的单词。这与传统的循环神经网络(RNN)逐个查看单词或像GPT这样的自回归模型(内部掩盖未来标记)不同,它允许模型学习句子的双向表示。
- 下一句预测(NSP):在预训练过程中,模型将两个被掩盖的句子连接作为输入。有时这两个句子在原始文本中是连续的,有时不是。模型需要预测这两个句子是否连续。
通过这种方式,模型学习到英语语言的内在表示,可用于提取对下游任务有用的特征。例如,如果你有一个带标签句子的数据集,可以使用BERT模型生成的特征作为输入来训练标准分类器。
预期用途与限制
你可以直接使用原始模型进行掩码语言建模或下一句预测,但它主要用于在下游任务上进行微调。请参阅模型中心寻找你感兴趣任务的微调版本。
需要注意的是,此模型主要针对使用整个句子(可能被掩盖)进行决策的任务进行微调,例如序列分类、标记分类或问答。对于文本生成等任务,应使用类似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本未出版的书籍)和英文维基百科(不包括列表、表格和标题)上进行了预训练。
训练过程
预处理
文本被转换为小写并使用WordPiece进行标记化,词汇量为30,000。模型的输入形式如下:
[CLS] 句子A [SEP] 句子B [SEP]
有50%的概率,句子A和句子B在原始语料库中是连续的句子,其他情况下是语料库中的随机句子。这里的“句子”通常是指比单个句子更长的连续文本片段。唯一的限制是两个“句子”的组合长度不超过512个标记。
每个句子的掩盖过程细节如下:
- 15%的标记被掩盖。
- 在80%的情况下,被掩盖的标记被替换为
[MASK]
。
- 在10%的情况下,被掩盖的标记被替换为随机标记(不同于原标记)。
- 在剩余的10%情况下,被掩盖的标记保持不变。
预训练
模型在4个云TPU(共16个TPU芯片)上训练了100万步,批次大小为256。序列长度在90%的步骤中限制为128个标记,其余10%的步骤中限制为512个标记。使用的优化器是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 |
BibTeX条目和引用信息
@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}
}