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

bert-base-multilingual-uncased

google-bert fill-mask transformers multilingual af sq
google-bert/bert-base-multilingual-uncased
3,820,335
下载量
156
收藏数
9
浏览量
apache-2.0
许可

简介

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

模型卡片

许可协议 apache-2.0
语言
multilingual af sq ar an hy ast az ba eu bar be bn inc bs br bg my ca ceb ce zh cv hr cs da nl en et fi fr gl ka de el gu ht he hi hu is io id ga it ja jv kn kk ky ko la lv lt roa nds lm mk mg ms ml mr min ne new nb nn oc fa pms pl pt pa ro ru sco sr hr scn sk sl aze es su sw sv tl tg ta tt te tr uk ud uz vi vo war cy fry pnb yo
数据集
wikipedia

模型配置

模型类型 bert
架构 BertForMaskedLM

模型详情

已翻译

BERT 多语言基础模型(uncased)

该模型基于掩码语言建模(MLM)目标,在拥有最大 Wikipedia 的前 102 种语言上进行了预训练。
它在这篇论文中被提出,并首次发布在
这个仓库中。该模型为 uncased 模式:它不区分
"english" 和 "English"。

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

模型描述

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

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

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

预期用途与局限性

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

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

如何使用

你可以直接使用带有 pipeline 的模型进行掩码语言建模:

>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-multilingual-uncased')
>>> unmasker("Hello I'm a [MASK] model.")

[{'sequence': "[CLS] hello i'm a top model. [SEP]",
  'score': 0.1507750153541565,
  'token': 11397,
  'token_str': 'top'},
 {'sequence': "[CLS] hello i'm a fashion model. [SEP]",
  'score': 0.13075384497642517,
  'token': 23589,
  'token_str': 'fashion'},
 {'sequence': "[CLS] hello i'm a good model. [SEP]",
  'score': 0.036272723227739334,
  'token': 12050,
  'token_str': 'good'},
 {'sequence': "[CLS] hello i'm a new model. [SEP]",
  'score': 0.035954564809799194,
  'token': 10246,
  'token_str': 'new'},
 {'sequence': "[CLS] hello i'm a great model. [SEP]",
  'score': 0.028643041849136353,
  'token': 11838,
  'token_str': 'great'}]

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

from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-uncased')
model = BertModel.from_pretrained("bert-base-multilingual-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-multilingual-uncased')
model = TFBertModel.from_pretrained("bert-base-multilingual-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-multilingual-uncased')
>>> unmasker("The man worked as a [MASK].")

[{'sequence': '[CLS] the man worked as a teacher. [SEP]',
  'score': 0.07943806052207947,
  'token': 21733,
  'token_str': 'teacher'},
 {'sequence': '[CLS] the man worked as a lawyer. [SEP]',
  'score': 0.0629938617348671,
  'token': 34249,
  'token_str': 'lawyer'},
 {'sequence': '[CLS] the man worked as a farmer. [SEP]',
  'score': 0.03367974981665611,
  'token': 36799,
  'token_str': 'farmer'},
 {'sequence': '[CLS] the man worked as a journalist. [SEP]',
  'score': 0.03172805905342102,
  'token': 19477,
  'token_str': 'journalist'},
 {'sequence': '[CLS] the man worked as a carpenter. [SEP]',
  'score': 0.031021825969219208,
  'token': 33241,
  'token_str': 'carpenter'}]

>>> unmasker("The Black woman worked as a [MASK].")

[{'sequence': '[CLS] the black woman worked as a nurse. [SEP]',
  'score': 0.07045423984527588,
  'token': 52428,
  'token_str': 'nurse'},
 {'sequence': '[CLS] the black woman worked as a teacher. [SEP]',
  'score': 0.05178029090166092,
  'token': 21733,
  'token_str': 'teacher'},
 {'sequence': '[CLS] the black woman worked as a lawyer. [SEP]',
  'score': 0.032601192593574524,
  'token': 34249,
  'token_str': 'lawyer'},
 {'sequence': '[CLS] the black woman worked as a slave. [SEP]',
  'score': 0.030507225543260574,
  'token': 31173,
  'token_str': 'slave'},
 {'sequence': '[CLS] the black woman worked as a woman. [SEP]',
  'score': 0.027691684663295746,
  'token': 14050,
  'token_str': 'woman'}]

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

训练数据

BERT 模型在拥有最大 Wikipedia 的 102 种语言上进行了预训练。你可以在
此处找到完整列表。

训练过程

预处理

文本被转换为小写,并使用 WordPiece 和共享词汇表大小 110,000 进行 token 化。Wikipedia 规模较大的语言
被欠采样,而资源较少的语言则被过采样。对于中文、日文汉字和韩文汉字等
没有空格的语言,每个字符周围会添加一个 CJK Unicode 块。

模型的输入形式如下:

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

以 0.5 的概率,句子 A 和句子 B 对应原始语料库中两个连续的句子,而在
其他情况下,它们对应语料库中的另一个随机句子。请注意,这里所说的"句子"是指
一段连续的文本,通常比单个句子更长。唯一的限制是这两个"句子"的
组合长度不超过 512 个 token。

每个句子的遮蔽过程细节如下:
- 15% 的 token 被遮蔽。
- 在 80% 的情况下,被遮蔽的 token 被替换为 [MASK]
- 在 10% 的情况下,被遮蔽的 token 被替换为一个随机 token(与被替换的 token 不同)。
- 在剩余的 10% 情况下,被遮蔽的 token 保持不变。

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

标签

tf jax bert multilingual af sq ar an

操作


详细信息

厂商
google-bert
任务
fill-mask
框架
transformers
模型类型
bert
许可(HF)
apache-2.0
语言
multilingual, af, sq, ar, an, hy, ast, az, ba, eu, bar, be, bn, inc, bs, br, bg, my, ca, ceb, ce, zh, cv, hr, cs, da, nl, en, et, fi, fr, gl, ka, de, el, gu, ht, he, hi, hu, is, io, id, ga, it, ja, jv, kn, kk, ky, ko, la, lv, lt, roa, nds, lm, mk, mg, ms, ml, mr, min, ne, new, nb, nn, oc, fa, pms, pl, pt, pa, ro, ru, sco, sr, hr, scn, sk, sl, aze, es, su, sw, sv, tl, tg, ta, tt, te, tr, uk, ud, uz, vi, vo, war, cy, fry, pnb, yo