ONNX 模型库
返回模型

说明文档

DistilBERT base 模型模型卡

本模型是 BERT base 模型 的蒸馏版本。 该模型来自这篇论文。 蒸馏过程的代码可以在这里找到。 本模型是区分大小写的:它会区分 english 和 English。

有关预训练的所有细节、用途、局限性以及潜在偏见(见下文)与 DistilBERT-base-uncased 相同。我们强烈建议您查看该模型以了解更多详情。

模型描述

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

  • 蒸馏损失:训练模型返回与BERT base模型相同的概率。
  • 掩码语言建模(MLM):这是BERT base模型原始训练损失的一部分。当输入一个句子时,模型会随机掩码15%的词,然后通过整个掩码后的句子运行模型,预测被掩码的词。这与传统的循环神经网络(RNN,通常一个词接一个词地处理)或自回归模型(如GPT,在内部掩码未来的token)不同。它允许模型学习句子的双向表示。
  • 余弦嵌入损失:模型还被训练生成尽可能接近BERT base模型的隐藏状态。

这样,模型学习了与其教师模型相同的英语内部表示,同时在推理或下游任务中更快。

预期用途与限制

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

请注意,本模型主要针对使用整个句子(可能被掩码)进行决策的任务进行微调,例如序列分类、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本未出版书籍的数据集)和英文Wikipedia(不包括列表、表格和标题)。

训练过程

预处理

文本使用WordPiece进行小写处理和分词,词汇量为30,000。模型的输入格式如下:

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

有50%的概率,句子A和句子B对应于原始语料库中两个连续的句子,在其他情况下,它是语料库中另一个随机句子。请注意,这里所谓的句子是通常长于单个句子的连续文本片段。唯一的限制是,两个"句子"组合后的长度少于512个token。

每个句子的掩码程序细节如下:

  • 15%的token被掩码。
  • 在80%的情况下,被掩码的token被替换为[MASK]
  • 在10%的情况下,被掩码的token被替换为另一个随机token(与其替换的token不同)。
  • 在剩余10%的情况下,被掩码的token保持原样。

预训练

模型在8个16GB V100上训练了90小时。查看训练代码了解所有超参数详情。

评估结果

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

GLUE测试结果:

任务 MNLI QQP QNLI SST-2 CoLA STS-B MRPC RTE
81.5 87.8 88.2 90.4 47.2 85.5 85.6 60.6

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

<a href="https://huggingface.co/exbert/?model=distilbert-base-uncased"> <img width="300px" src="https://cdn-media.huggingface.co/exbert/button.png"> </a>

distilbert/distilbert-base-cased

作者 distilbert

fill-mask transformers
↓ 164.9K ♥ 60

创建时间: 2022-03-02 23:29:04+00:00

更新时间: 2024-05-06 13:46:22+00:00

在 Hugging Face 上查看

文件 (10)

.gitattributes
README.md
config.json
model.onnx ONNX
model.safetensors
pytorch_model.bin
tf_model.h5
tokenizer.json
tokenizer_config.json
vocab.txt