该模型是 BERT 基础模型的蒸馏版本。 它在本文中被首次提出。 蒸馏过程的代码可在此处找到。 此模型区分大小写:例如,它会区分 "english" 和 "English"。
关于预训练、用途、局限性和潜在偏差的所有训练细节(包含在下方)均与 DistilBERT-base-uncased 相同。 如果您想了解更多信息,我们强烈建议您查阅该模型的相关内容。
DistilBERT 是一个转换器模型,它比 BERT 更小、更快,并且在相同的语料库上以自监督的方式进行了预训练,以 BERT 基础模型作为教师模型。这意味着它仅在原始文本上进行预训练,无需人工以任何方式对文本进行标注(这也是它能够利用大量公开可用数据的原因),并通过一种自动流程,利用 BERT 基础模型从这些文本中生成输入和标签。更准确地说,它通过三个目标进行预训练:
通过这种方式,该模型能够学习到与其教师模型相同的英语内在表示,同时在推理或下游任务中速度更快。
您可以将原始模型用于掩码语言建模或下一句预测,但它主要旨在针对下游任务进行微调。请查看模型中心以寻找您感兴趣任务的微调版本。
请注意,此模型主要用于在需要利用整个句子(可能包含掩码)进行决策的任务上进行微调,例如序列分类、标记分类或问答任务。对于文本生成等任务,您应该考虑像 GPT2 这样的模型。
您可以直接将此模型与用于掩码语言建模的管道配合使用:
>>> 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(https://yknzhu.wixsite.com/mbweb)——一个包含 11,038 本未出版书籍的数据集,以及英文维基百科(https://en.wikipedia.org/wiki/English_Wikipedia,不包括列表、表格和标题)。
文本会被转换为小写,并使用 WordPiece 进行分词,词汇表大小为 30,000。然后,模型的输入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]以 0.5 的概率,句子 A 和句子 B 对应原始语料库中的两个连续句子,在其他情况下,则是语料库中的另一个随机句子。请注意,此处所指的“句子”是一段连续的文本,通常比单个句子更长。唯一的限制是,这两个“句子”组合后的总长度需少于 512 个 token。
每个句子的掩码处理细节如下:
[MASK]。该模型在 8 块 16 GB 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 |
@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}
}