HuggingFace镜像/bert-large-cased-whole-word-masking
模型介绍文件和版本分析
下载使用量0

BERT large model (cased) whole word masking

基于英语语言、使用掩码语言模型(MLM)目标训练的预训练模型。该模型的相关介绍见于

此模型区分大小写:例如,它会区分 "english" 和 "English"。

与其他 BERT 模型不同,此模型采用了一种新技术进行训练:全词掩码(Whole Word Masking)。在这种情况下,一个单词所对应的所有 tokens 会被同时掩码。整体掩码率保持不变。

训练过程是相同的——每个被掩码的 WordPiece token 都是独立进行预测的。

免责声明:发布 BERT 的团队并未为此模型撰写模型卡片,因此本模型卡片由 Hugging Face 团队编写。

模型描述

在大量英语语料库上以自监督方式预训练的模型。这意味着它仅在原始文本上进行预训练,无需人工以任何方式进行标注(这也是它能够利用大量公开可用数据的原因),并通过自动流程从这些文本中生成输入和标签。更准确地说,它是在两个目标下进行预训练的:

  • 掩码语言模型(MLM):给定一个句子,模型随机掩码输入中 15% 的单词,然后将整个被掩码的句子输入模型,并预测被掩码的单词。这与传统的循环神经网络(RNNs)通常逐个处理单词,或者与像 GPT 这样的自回归模型在内部掩码未来 tokens 的方式不同。它允许模型学习句子的双向表示。
  • 下一句预测(NSP):模型在预训练期间将两个掩码句子连接作为输入。有时它们对应于原始文本中相邻的句子,有时则不是。然后模型需要预测这两个句子是否前后相连。

通过这种方式,模型学习到英语语言的内部表示,该表示可用于提取对下游任务有用的特征:例如,如果您有一个带标签的句子数据集,您可以使用 BERT 模型生成的特征作为输入来训练一个标准分类器。

该模型具有以下配置:

  • 24 层
  • 1024 隐藏维度
  • 16 个注意力头
  • 3.36 亿参数。

预期用途与局限性

您可以将原始模型用于掩码语言建模或下一句预测任务,但该模型主要旨在针对下游任务进行微调。

请注意,此模型主要用于在需要使用整个句子(可能包含掩码)进行决策的任务上进行微调,例如序列分类、 token 分类或问答任务。对于文本生成等任务,您应考虑 GPT2 等模型。

使用方法

您可以直接通过 pipeline 将此模型用于掩码语言建模:

>>> from openmind import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-large-cased-whole-word-masking')
>>> unmasker("Hello I'm a [MASK] model.")
[
   {
      "sequence":"[CLS] Hello I'm a fashion model. [SEP]",
      "score":0.1474294513463974,
      "token":4633,
      "token_str":"fashion"
   },
   {
      "sequence":"[CLS] Hello I'm a magazine model. [SEP]",
      "score":0.05430116504430771,
      "token":2435,
      "token_str":"magazine"
   },
   {
      "sequence":"[CLS] Hello I'm a male model. [SEP]",
      "score":0.039395421743392944,
      "token":2581,
      "token_str":"male"
   },
   {
      "sequence":"[CLS] Hello I'm a former model. [SEP]",
      "score":0.036936815828084946,
      "token":1393,
      "token_str":"former"
   },
   {
      "sequence":"[CLS] Hello I'm a professional model. [SEP]",
      "score":0.03663451969623566,
      "token":1848,
      "token_str":"professional"
   }
]

以下是如何在 PyTorch 中使用该模型获取给定文本特征的方法:

from openmind import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-large-cased-whole-word-masking')
model = BertModel.from_pretrained("bert-large-cased-whole-word-masking")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)

而在 TensorFlow 中:

from openmind import BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('bert-large-cased-whole-word-masking')
model = TFBertModel.from_pretrained("bert-large-cased-whole-word-masking")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)

局限性与偏见

即便用于训练此模型的数据可被描述为相当中立,该模型仍可能产生有偏见的预测:

>>> from openmind import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-large-cased-whole-word-masking')
>>> unmasker("The man worked as a [MASK].")
[
   {
      "sequence":"[CLS] The man worked as a carpenter. [SEP]",
      "score":0.09021259099245071,
      "token":25169,
      "token_str":"carpenter"
   },
   {
      "sequence":"[CLS] The man worked as a cook. [SEP]",
      "score":0.08125395327806473,
      "token":9834,
      "token_str":"cook"
   },
   {
      "sequence":"[CLS] The man worked as a mechanic. [SEP]",
      "score":0.07524766772985458,
      "token":19459,
      "token_str":"mechanic"
   },
   {
      "sequence":"[CLS] The man worked as a waiter. [SEP]",
      "score":0.07397029548883438,
      "token":17989,
      "token_str":"waiter"
   },
   {
      "sequence":"[CLS] The man worked as a guard. [SEP]",
      "score":0.05848982185125351,
      "token":3542,
      "token_str":"guard"
   }
]


>>> unmasker("The woman worked as a [MASK].")
[
   {
      "sequence":"[CLS] The woman worked as a maid. [SEP]",
      "score":0.19436432421207428,
      "token":13487,
      "token_str":"maid"
   },
   {
      "sequence":"[CLS] The woman worked as a waitress. [SEP]",
      "score":0.16161060333251953,
      "token":15098,
      "token_str":"waitress"
   },
   {
      "sequence":"[CLS] The woman worked as a nurse. [SEP]",
      "score":0.14942803978919983,
      "token":7439,
      "token_str":"nurse"
   },
   {
      "sequence":"[CLS] The woman worked as a secretary. [SEP]",
      "score":0.10373266786336899,
      "token":4848,
      "token_str":"secretary"
   },
   {
      "sequence":"[CLS] The woman worked as a cook. [SEP]",
      "score":0.06384387612342834,
      "token":9834,
      "token_str":"cook"
   }
]

这种偏差也会影响该模型的所有微调版本。

训练数据

BERT 模型在 BookCorpus(一个包含 11,038 本未出版书籍的数据集)和 English Wikipedia(不含列表、表格和标题)上进行了预训练。

训练过程

预处理

文本经过小写处理,并使用 WordPiece 进行分词,词汇表大小为 30,000。然后,模型的输入形式如下:

[CLS] Sentence A [SEP] Sentence B [SEP]

以 0.5 的概率,句子 A 和句子 B 对应原始语料库中的两个连续句子,在其他情况下,则是语料库中的另一个随机句子。请注意,此处所指的“句子”是一段连续的文本,通常比单个句子更长。唯一的限制是这两个“句子”组合后的总长度不超过 512 个标记。

每个句子的掩码处理细节如下:

  • 15% 的标记被掩码。
  • 在 80% 的情况下,被掩码的标记会替换为 [MASK]。
  • 在 10% 的情况下,被掩码的标记会替换为一个随机的(不同的)标记。
  • 在剩余 10% 的情况下,被掩码的标记保持不变。

预训练

该模型在采用 Pod 配置的 4 个云 TPU(共 16 个 TPU 芯片)上进行训练,训练步数为一百万步,批处理大小为 256。在 90% 的训练步骤中,序列长度限制为 128 个标记,在剩余 10% 的步骤中则为 512 个标记。使用的优化器为 Adam,学习率为 1e-4,$\beta_{1} = 0.9$,$\beta_{2} = 0.999$,权重衰减为 0.01,学习率预热 10,000 步,之后学习率线性衰减。

评估结果

当在下游任务上进行微调时,该模型取得了以下结果:

模型SQUAD 1.1 F1/EMMulti NLI 准确率
BERT-Large, Cased (Whole Word Masking)92.9/86.786.46

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}
}