该模型是 BERT 基础模型的蒸馏版本。它在此论文中被首次提出。蒸馏过程的代码可在此处找到。此模型为 uncased 版本:即“english”和“English”对其而言没有区别。
修改示例代码部分。
DistilBERT 是一个 transformers 模型,它比 BERT 更小、更快。它以 BERT 基础模型作为教师模型,在相同的语料库上以自监督的方式进行预训练。这意味着它仅在原始文本上进行预训练,无需人工对文本进行任何形式的标注(这也是它能够利用大量公开可用数据的原因),并通过一种自动过程,利用 BERT 基础模型从这些文本中生成输入和标签。更准确地说,它是在三个目标下进行预训练的:
通过这种方式,该模型学习到了与其教师模型相同的英语语言内部表示,同时在推理或下游任务中速度更快。
您可以将原始模型用于掩码语言建模或下一句预测,但它主要旨在针对下游任务进行微调。
请注意,此模型主要用于在需要利用整个句子(可能带有掩码)来做决策的任务上进行微调,例如序列分类、标记分类或问答任务。对于文本生成等任务,您应该考虑像 GPT2 这样的模型。
您可以直接将此模型与用于掩码语言建模的 pipeline 配合使用:
>>> from openmind import pipeline
>>> unmasker = pipeline('fill-mask', model='PyTorch-NPU/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 中使用此模型获取给定文本特征的方法:
import openmind
from transformers import DistilBertTokenizer, DistilBertModel
tokenizer = DistilBertTokenizer.from_pretrained('PyTorch-NPU/distilbert_base_uncased')
model = DistilBertModel.from_pretrained("PyTorch-NPU/distilbert_base_uncased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)即便用于训练此模型的数据可被描述为相当中立,该模型仍可能产生有偏见的预测。它还继承了其教师模型的部分偏见。
>>> from openmind import pipeline
>>> unmasker = pipeline('fill-mask', model='PyTorch-NPU/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。模型的输入形式如下:
[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 |
|---|---|---|---|---|---|---|---|---|
| 82.2 | 88.5 | 89.2 | 91.3 | 51.3 | 85.8 | 87.5 | 59.9 |
@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}
}