模型库 / distilbert/distilbert-base-uncased

distilbert-base-uncased

distilbert fill-mask transformers en
distilbert/distilbert-base-uncased
16,186,558
下载量
1073
收藏数
22
浏览量
apache-2.0
许可

简介

该模型是BERT基础模型的蒸馏版本。该模型首次发表于此论文中。蒸馏过程的代码可在此处获取。该模型不区分大小写:它不会区分"english"和"English"。

模型卡片

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

模型配置

模型类型 distilbert
架构 DistilBertForMaskedLM

模型详情

已翻译

DistilBERT base model (uncased)

该模型是 BERT base model 的精简版本,首次在此论文中提出。蒸馏过程的代码可在此处找到。该模型不区分大小写:即对 "english" 和 "English" 不作区分。

模型描述

DistilBERT 是一个 transformer 模型,比 BERT 更小、更快,它使用 BERT base model 作为教师模型,以自监督方式在同一语料库上进行预训练。这意味着它仅基于原始文本进行预训练,没有任何人工标注(因此可以利用大量公开数据),并通过自动流程使用 BERT base model 从这些文本生成输入和标签。更准确地说,它的预训练包含三个目标:

  • 蒸馏损失(Distillation loss):模型被训练为返回与 BERT base model 相同的概率。
  • 掩码语言建模(Masked Language Modeling, MLM):这是 BERT base model 原始训练损失的一部分。当输入一个句子时,模型会随机掩码 15% 的单词,然后将整个掩码后的句子输入模型,并预测被掩码的单词。这与传统的循环神经网络(RNN)不同,后者通常逐个处理单词,也与 GPT 等自回归模型不同,后者在内部掩码未来的 token。这使得模型能够学习句子的双向表示。
  • 余弦嵌入损失(Cosine embedding loss):模型还被训练为生成与 BERT base model 尽可能接近的隐藏状态。

通过这种方式,模型学习到了与其教师模型相同的英语语言内部表示,同时在推理或下游任务中速度更快。

预期用途与局限性

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

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

如何使用

您可以直接使用 pipeline 进行掩码语言建模:

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

[{'sequence': "[CLS] hello i'm a role model. [SEP]",
  'score': 0.05292855575680733,
  'token': 2535,
  'token_str': 'role'},
 {'sequence': "[CLS] hello i'm a fashion model. [SEP]",
  'score': 0.03968575969338417,
  'token': 4827,
  'token_str': 'fashion'},
 {'sequence': "[CLS] hello i'm a business model. [SEP]",
  'score': 0.034743521362543106,
  'token': 2449,
  'token_str': 'business'},
 {'sequence': "[CLS] hello i'm a model model. [SEP]",
  'score': 0.03462274372577667,
  'token': 2944,
  'token_str': 'model'},
 {'sequence': "[CLS] hello i'm a modeling model. [SEP]",
  'score': 0.018145186826586723,
  'token': 11643,
  'token_str': 'modeling'}]

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

from transformers import DistilBertTokenizer, DistilBertModel
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertModel.from_pretrained("distilbert-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 DistilBertTokenizer, TFDistilBertModel
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = TFDistilBertModel.from_pretrained("distilbert-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='distilbert-base-uncased')
>>> unmasker("The White man worked as a [MASK].")

[{'sequence': '[CLS] the white man worked as a blacksmith. [SEP]',
  'score': 0.1235365942120552,
  'token': 20987,
  'token_str': 'blacksmith'},
 {'sequence': '[CLS] the white man worked as a carpenter. [SEP]',
  'score': 0.10142576694488525,
  'token': 10533,
  'token_str': 'carpenter'},
 {'sequence': '[CLS] the white man worked as a farmer. [SEP]',
  'score': 0.04985016956925392,
  'token': 7500,
  'token_str': 'farmer'},
 {'sequence': '[CLS] the white man worked as a miner. [SEP]',
  'score': 0.03932540491223335,
  'token': 18594,
  'token_str': 'miner'},
 {'sequence': '[CLS] the white man worked as a butcher. [SEP]',
  'score': 0.03351764753460884,
  'token': 14998,
  'token_str': 'butcher'}]

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

[{'sequence': '[CLS] the black woman worked as a waitress. [SEP]',
  'score': 0.13283951580524445,
  'token': 13877,
  'token_str': 'waitress'},
 {'sequence': '[CLS] the black woman worked as a nurse. [SEP]',
  'score': 0.12586183845996857,
  'token': 6821,
  'token_str': 'nurse'},
 {'sequence': '[CLS] the black woman worked as a maid. [SEP]',
  'score': 0.11708822101354599,
  'token': 10850,
  'token_str': 'maid'},
 {'sequence': '[CLS] the black woman worked as a prostitute. [SEP]',
  'score': 0.11499975621700287,
  'token': 19215,
  'token_str': 'prostitute'},
 {'sequence': '[CLS] the black woman worked as a housekeeper. [SEP]',
  'score': 0.04722772538661957,
  'token': 22583,
  'token_str': 'housekeeper'}]

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

训练数据

DistilBERT 在与 BERT 相同的数据上进行预训练,即 BookCorpus(包含 11,038 本未出版书籍的数据集)和 English Wikipedia(不包括列表、表格和标题)。

训练流程

预处理

文本被转换为小写,并使用 WordPiece 和大小为 30,000 的词汇表进行 token 化。模型的输入形式如下:

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

以 0.5 的概率,句子 A 和句子 B 对应原始语料库中的两个连续句子;在其他情况下,则是语料库中的另一个随机句子。需要注意的是,这里所谓的“句子”通常是指比单个句子更长的连续文本片段。唯一的约束是这两个“句子”的组合长度不超过 512 个 token。

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

预训练

该模型在 8 块 16 GB V100 GPU 上训练了 90 小时。所有超参数详情请参见训练代码

评估结果

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

Glue 测试结果:

任务 MNLI QQP QNLI SST-2 CoLA STS-B MRPC RTE
82.2 88.5 89.2 91.3 51.3 85.8 87.5 59.9

BibTeX 条目与引用信息

@article{Sanh2019DistilBERTAD,
  title={DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter},
  author={Victor Sanh and Lysandre Debut and Julien Chaumond and Thomas Wolf},
  journal={ArXiv},
  year={2019},
  volume={abs/1910.01108}
}

标签

tf jax rust distilbert exbert en dataset:bookcorpus dataset:wikipedia

操作


详细信息

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