模型:
bert-large-cased
BERT是一个在英语语言上进行预训练的transformers模型,使用掩码语言建模(MLM)的目标进行预训练。它于...年推出并首次发布。该模型区分大小写:它能区分english和English。
免责声明:发布BERT的团队没有为此模型编写模型卡片,因此该模型卡片由Hugging Face团队编写。
BERT是一个以自监督方式在大型英语语料库上进行预训练的transformers模型。这意味着它仅使用原始文本进行预训练,没有人工以任何方式标记这些文本(这就是为什么它可以使用很多公开可用的数据)通过自动生成输入和标签的自动过程从这些文本中生成。更准确地说,它以两个目标进行预训练:
通过这种方式,模型学习了英语语言的内部表示,可以用于提取对下游任务有用的特征。例如,如果你有一个带标签句子的数据集,可以使用BERT模型生成的特征作为输入训练一个标准分类器。
此模型的配置如下:
可以直接使用原始模型进行掩码语言建模或下一个句子预测,但主要用于在下游任务中进行微调。查看链接以查找与您感兴趣的任务相关的微调版本。
请注意,此模型主要旨在对使用整个句子(可能被屏蔽)进行决策的任务进行微调,例如序列分类、标记分类或问答。对于文本生成等任务,应考虑使用像GPT2这样的模型。
您可以使用此模型直接进行掩码语言建模的管道:
>>> from transformers import pipeline >>> unmasker = pipeline('fill-mask', model='bert-large-cased') >>> unmasker("Hello I'm a [MASK] model.") [ { "sequence":"[CLS] Hello I'm a male model. [SEP]", "score":0.22748498618602753, "token":2581, "token_str":"male" }, { "sequence":"[CLS] Hello I'm a fashion model. [SEP]", "score":0.09146175533533096, "token":4633, "token_str":"fashion" }, { "sequence":"[CLS] Hello I'm a new model. [SEP]", "score":0.05823173746466637, "token":1207, "token_str":"new" }, { "sequence":"[CLS] Hello I'm a super model. [SEP]", "score":0.04488750174641609, "token":7688, "token_str":"super" }, { "sequence":"[CLS] Hello I'm a famous model. [SEP]", "score":0.03271442651748657, "token":2505, "token_str":"famous" } ]
这是如何在PyTorch中使用此模型获取给定文本的特征:
from transformers import BertTokenizer, BertModel tokenizer = BertTokenizer.from_pretrained('bert-large-cased') model = BertModel.from_pretrained("bert-large-cased") 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-large-cased') model = TFBertModel.from_pretrained("bert-large-cased") 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-large-cased') >>> unmasker("The man worked as a [MASK].") [ { "sequence":"[CLS] The man worked as a doctor. [SEP]", "score":0.0645911768078804, "token":3995, "token_str":"doctor" }, { "sequence":"[CLS] The man worked as a cop. [SEP]", "score":0.057450827211141586, "token":9947, "token_str":"cop" }, { "sequence":"[CLS] The man worked as a mechanic. [SEP]", "score":0.04392256215214729, "token":19459, "token_str":"mechanic" }, { "sequence":"[CLS] The man worked as a waiter. [SEP]", "score":0.03755280375480652, "token":17989, "token_str":"waiter" }, { "sequence":"[CLS] The man worked as a teacher. [SEP]", "score":0.03458863124251366, "token":3218, "token_str":"teacher" } ] >>> unmasker("The woman worked as a [MASK].") [ { "sequence":"[CLS] The woman worked as a nurse. [SEP]", "score":0.2572779953479767, "token":7439, "token_str":"nurse" }, { "sequence":"[CLS] The woman worked as a waitress. [SEP]", "score":0.16706500947475433, "token":15098, "token_str":"waitress" }, { "sequence":"[CLS] The woman worked as a teacher. [SEP]", "score":0.04587847739458084, "token":3218, "token_str":"teacher" }, { "sequence":"[CLS] The woman worked as a secretary. [SEP]", "score":0.03577028587460518, "token":4848, "token_str":"secretary" }, { "sequence":"[CLS] The woman worked as a maid. [SEP]", "score":0.03298963978886604, "token":13487, "token_str":"maid" } ]
这种偏见也会影响该模型的所有微调版本。
BERT模型在 BookCorpus 上进行了预训练,该数据集由11038本未出版的书籍和 English Wikipedia 个文档(不包括列表、表格和标题)组成。
文本经过小写处理并使用WordPiece进行标记化,并使用30000个单词的词汇表大小。模型的输入形式如下:
[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,学习率预热进行了1万个步骤,并在此后线性降低学习率。
在下游任务上进行微调时,该模型实现了以下结果:
Model | SQUAD 1.1 F1/EM | Multi NLI Accuracy |
---|---|---|
BERT-Large, Cased (Original) | 91.5/84.8 | 86.09 |
@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} }