模型简介
模型特点
模型能力
使用案例
🚀 BERT基础模型(无大小写区分)
BERT基础模型(无大小写区分)是一个使用掩码语言建模(MLM)目标在英语语料上进行预训练的模型。它能够学习英语语言的双向表示,可用于提取对下游任务有用的特征。
🚀 快速开始
你可以直接使用这个模型进行掩码语言建模,也可以将其微调用于下游任务。以下是使用示例:
>>> 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)
✨ 主要特性
- 双向表示学习:通过掩码语言建模(MLM)目标,模型能够学习句子的双向表示,这与传统的循环神经网络(RNNs)和自回归模型(如GPT)不同。
- 多任务学习:除了MLM,模型还使用了下一句预测(NSP)目标进行预训练,使其能够学习句子之间的关系。
- 可微调性:预训练的模型可以在下游任务上进行微调,如序列分类、标记分类或问答任务。
💻 使用示例
基础用法
>>> 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'}]
高级用法
在下游任务上微调模型:
# 这里可以添加微调模型的代码示例
# 由于原文档未提供,可根据实际情况补充
📚 详细文档
预期用途和限制
你可以使用原始模型进行掩码语言建模或下一句预测,但它主要用于在下游任务上进行微调。该模型主要针对需要使用整个句子(可能是掩码后的句子)来做决策的任务进行微调,如序列分类、标记分类或问答任务。对于文本生成等任务,你应该考虑使用像GPT2这样的模型。
局限性和偏差
尽管该模型使用的训练数据可以被认为是相当中立的,但模型可能会有有偏差的预测:
>>> 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和英文维基百科(不包括列表、表格和标题)上进行预训练。BookCorpus是一个由11,038本未出版书籍组成的数据集。
训练过程
预处理
文本被转换为小写,并使用WordPiece进行分词,词汇表大小为30,000。模型的输入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]
有0.5的概率,句子A和句子B对应原始语料库中的两个连续句子,在其他情况下,句子B是语料库中的另一个随机句子。这里的“句子”通常是一段连续的文本,长度通常大于单个句子。唯一的限制是两个“句子”的组合长度小于512个标记。
每个句子的掩码过程细节如下:
- 15%的标记被掩码。
- 在80%的情况下,被掩码的标记被替换为
[MASK]
。 - 在10%的情况下,被掩码的标记被替换为一个与它们不同的随机标记。
- 在剩下的10%的情况下,被掩码的标记保持不变。
预训练
模型在4个云TPU(Pod配置,共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 |
🔧 技术细节
BERT是一个基于Transformer架构的模型,通过自监督学习的方式在大量英语数据上进行预训练。它使用了掩码语言建模(MLM)和下一句预测(NSP)两个目标,使得模型能够学习到英语语言的内部表示。这种表示可以用于提取对下游任务有用的特征,例如在有标签的句子数据集上训练标准分类器。
📄 许可证
本模型使用Apache-2.0许可证。
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}
}




