模型库 / google-bert/bert-base-uncased

bert-base-uncased

google-bert fill-mask transformers en
google-bert/bert-base-uncased
62,660,343
下载量
2837
收藏数
87
浏览量
apache-2.0
许可

简介

基于掩码语言建模(MLM)目标在英语上预训练的模型。该模型首次发表于此论文,并首次在此仓库中发布。此模型不区分大小写:即对"english"和"English"不作区分。

模型卡片

许可协议 apache-2.0
语言
en
数据集
bookcorpus wikipedia
exbert

模型配置

模型类型 bert
架构 BertForMaskedLM

模型详情

已翻译

BERT base model (uncased)

该模型使用掩码语言建模(MLM)目标在英语上进行预训练。它首次在此论文中提出,并首次发布于此仓库。该模型为 uncased 版本:不区分 english 和 English。

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

模型描述

BERT 是一个 transformer 模型,以自监督方式在大量英文数据上进行了预训练。这意味着它仅基于原始文本进行预训练,没有任何人工标注(因此可以使用大量公开数据),并通过自动流程从这些文本中生成输入和标签。更准确地说,它通过两个目标进行预训练:

  • 掩码语言建模(MLM):给定一个句子,模型随机掩盖输入中 15% 的单词,然后将整个掩码句子输入模型,并预测被掩盖的单词。这与传统的循环神经网络(RNNs)不同,后者通常逐个处理单词,也与 GPT 等自回归模型不同,后者在内部掩盖未来的 token。这种方法使模型能够学习句子的双向表示。
  • 下一句预测(NSP):在预训练期间,模型将两个掩码句子拼接作为输入。有时这两个句子在原始文本中是相邻的,有时则不是。模型需要预测这两个句子是否连续。

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

模型变体

BERT 最初发布了 base 和 large 两种变体,分别支持 cased 和 uncased 输入文本。uncased 模型还会去除重音标记。
中文和多语言的 uncased 及 cased 版本随后很快发布。
在后续工作中,通过引入全词掩码(whole word masking)改进了预处理,替代了子词掩码(subpiece masking),并发布了两个模型。
此后还发布了其他 24 个较小的模型。

详细的发布历史可在 GitHub 上的 google-research/bert readme 中查看。

模型 参数量 语言
bert-base-uncased 110M 英语
bert-large-uncased 340M 英语
bert-base-cased 110M 英语
bert-large-cased 340M 英语
bert-base-chinese 110M 中文
bert-base-multilingual-cased 110M 多语言
bert-large-uncased-whole-word-masking 340M 英语
bert-large-cased-whole-word-masking 340M 英语

预期用途与限制

你可以直接使用原始模型进行掩码语言建模或下一句预测,但其主要用途是在下游任务上进行微调。请查看 model hub 寻找你感兴趣任务的微调版本。

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

如何使用

你可以通过 pipeline 直接使用此模型进行掩码语言建模:

>>> 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 的词汇量进行 token 化。模型的输入

标签

tf jax rust coreml onnx bert exbert en

操作


详细信息

厂商
google-bert
任务
fill-mask
框架
transformers
模型类型
bert
许可(HF)
apache-2.0
语言
en