# DistilBERT

**DistilBERT**是BERT语言模型的一个更小、更快的版本，其制作方法是：以BERT base为教师模型，通过知识蒸馏预训练一个紧凑的学生模型。它由[Hugging Face](https://www.edgepedia.org/zh/hugging-face)的Victor Sanh、Lysandre Debut、Julien Chaumond和Thomas Wolf于2019年10月推出，将BERT模型的规模缩小了40%，同时保留了97%的语言理解能力，运行速度提高了60%。<sup>[1](https://arxiv.org/abs/1910.01108)</sup> 与早期大多数蒸馏工作构建任务专用压缩模型不同，DistilBERT在预训练阶段就应用蒸馏，从而产生一个通用语言表示，可以像其更大的对应模型一样，在广泛的任务上进行微调。<sup>[1](https://arxiv.org/abs/1910.01108)</sup>

| 关键事实 | 详情 |
|---|---|
| 发布 | arXiv论文于2019年10月2日提交；被NeurIPS 2019第5届节能机器学习与认知计算研讨会接收<sup>[1](https://arxiv.org/abs/1910.01108)</sup> |
| 规模与速度 | 比BERT小40%，保留97%的语言理解能力，速度快60%<sup>[1](https://arxiv.org/abs/1910.01108)</sup> |
| 训练损失 | 三重损失，结合语言建模损失、蒸馏损失和余弦距离损失<sup>[1](https://arxiv.org/abs/1910.01108)</sup> |
| 预训练数据 | BookCorpus和英文维基百科，与BERT相同的语料库；WordPiece分词器，词汇量30,000词<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup> |
| 训练成本 | 8块16 GB V100 GPU训练90小时<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup> |
| 许可证 | Apache-2.0<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup> |

## 预训练期间的蒸馏如何运作

知识蒸馏训练一个较小的学生网络来模仿较大的教师网络的行为。<sup>[3](https://doi.org/10.1162/tacl_a_00349)</sup> DistilBERT在预训练阶段应用这一思想：训练学生模型返回与BERT base模型相同的概率（蒸馏损失），解决BERT本身训练时所使用的掩码语言建模任务，并产生尽可能接近教师的隐藏状态，用余弦嵌入损失来衡量。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup> 论文将其描述为结合语言建模、蒸馏和余弦距离项的三重损失，引入这些项是为了利用较大模型在预训练期间学到的归纳偏置。<sup>[1](https://arxiv.org/abs/1910.01108)</sup>

掩码语言建模目标的工作原理是：随机掩蔽输入句子中15%的词，要求模型预测这些词；80%的掩蔽词元被替换为掩码词元，10%被替换为随机词元，10%保持不变。这训练出句子的双向表示，不同于在内部掩蔽未来词元的自回归模型。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup>

## 架构与训练

DistilBERT通过省略词元类型嵌入和池化器来近似BERT，这促成了40%的规模缩减。<sup>[4](https://www.sciencedirect.com/topics/computer-science/distilbert)</sup> 发布的不区分大小写版检查点以safetensors格式保存了6700万个参数。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup> 文本被转换为小写并用[WordPiece](https://www.edgepedia.org/zh/wordpiece)分词，输入遵循BERT格式：一个`[CLS]`词元、两个由`[SEP]`分隔的句子片段，合并长度在512个词元以下；以0.5的概率，两个片段是语料库中连续的句子，否则第二个是随机句子。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup>

预训练使用了与BERT相同的数据——BookCorpus（11,038本未出版书籍）和排除列表、表格及标题的英文维基百科，在八块16 GB V100 GPU上耗时90小时。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup> 作者指出，该模型的预训练成本低于BERT，并在概念验证实验和比较性设备端研究中展示了其设备端计算能力。<sup>[1](https://arxiv.org/abs/1910.01108)</sup>

## 性能

一份对论文表1的课程总结指出，DistilBERT在GLUE开发集上保留了BERT 97%的性能。<sup>[5](https://princeton-nlp.github.io/cos484/lectures/lec16.pdf)</sup> Hugging Face模型卡列出的微调后GLUE测试分数为：MNLI 82.2、QQP 88.5、QNLI 89.2、SST-2 91.3、CoLA 51.3、STS-B 85.8、MRPC 87.5、RTE 59.9。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup>

后来一项在GLUE上比较优化Transformer模型的基准测试，部署在中端智能手机和[Raspberry Pi 5](https://www.edgepedia.org/zh/raspberry-pi-5)上，测得DistilBERT为6600万参数、160毫秒延迟、平均GLUE准确率79.1%，而BERT base为1.1亿参数、420毫秒、82.2%；TinyBERT以1450万参数和75毫秒达到77.0%，[MobileBERT](https://www.edgepedia.org/zh/mobilebert)以2500万参数和95毫秒达到80.3%。<sup>[6](https://www.acejournal.org/2025/06/18/efficient-transformers-for-on-device-nlp)</sup> 在同一研究中，量化将延迟改善了30%至60%，准确率损失不到1%，而蒸馏产生的模型规模为原来的1/2至1/4，准确率具有竞争力。<sup>[6](https://www.acejournal.org/2025/06/18/efficient-transformers-for-on-device-nlp)</sup>

## 与其他紧凑BERT模型的关系

DistilBERT、MobileBERT（Sun等，2020）和TinyBERT（Jiao等，2020）是生产BERT轻量版本的突出尝试，这些版本以显著更少的可训练参数紧密模仿BERT的性能。<sup>[7](https://pmc.ncbi.nlm.nih.gov/articles/PMC10027428/)</sup> 它们的区别在于学生模型匹配什么：DistilBERT匹配输出KL散度加隐藏状态余弦相似度；TinyBERT增加了softmax前注意力匹配，以及先通用后任务专用蒸馏并配合数据增强的两阶段方案；MobileBERT将每一层重新设计为倒瓶颈结构，并通过教师-助教模型进行训练。<sup>[8](https://user.ceng.metu.edu.tr/~adhd/blogs/knowledge-distillation/index.html)</sup> 一篇关于信息检索中BERT的综述同样区分了DistilBERT——它在预训练中使用蒸馏来构建轻量级通用预训练模型——与TinyBERT——它将蒸馏用于任务专用模型。<sup>[9](https://doi.org/10.1145/3648471)</sup>

## 原始基准之外的应用

DistilBERT被用作问答、文本分类和情感分析中微调任务模型的基础，并且是Hugging Face问答教程中的默认示例模型，在SQuAD上进行了微调。<sup>[10](https://huggingface.co/docs/transformers/en/tasks/question_answering)</sup> 存在领域变体，例如BERTino，一种针对[意大利语](https://www.edgepedia.org/zh/italian-language)的DistilBERT风格轻量模型。<sup>[11](https://arxiv.org/abs/2303.18121)</sup> 在生物医学NLP中，研究人员在PubMed语料库上继续预训练DistilBERT、MobileBERT和TinyBERT达200,000步，以创建紧凑的生物医学Transformer，并指出比较时应考虑模型规模的巨大差异，例如1500万对6000万参数。<sup>[7](https://pmc.ncbi.nlm.nih.gov/articles/PMC10027428/)</sup>

该模型也在文本之外进行了测试。一项2026年的设备端故障检测基准测试在三个工业数据集（NASA C-MAPSS涡扇退化、SECOM半导体制造和UCI AI4I 2020预测性维护）上，将DistilBERT、TinyBERT-6L、TinyBERT-4L和MobileBERT与随机森林、XGBoost、SVM和逻辑回归进行比较，传感器数据被序列化为文本。在分离良好的C-MAPSS数据上，轻量Transformer以87.8%的F1匹配传统机器学习，但模型规模是后者的100倍，CPU延迟是后者的9,000倍；在严重不平衡的数据集上，两个家族都表现不佳。<sup>[12](https://arxiv.org/abs/2606.24173)</sup> 同一研究发现，INT8动态量化将模型规模缩小25%，同时保持86.9%的F1，而一个自适应流水线将97.9%的预测通过量化分诊模型路由，以19.5毫秒的平均延迟达到87.6%的F1。<sup>[12](https://arxiv.org/abs/2606.24173)</sup>

## 局限性

模型卡指出，尽管训练数据可以描述为相当中性，但模型可能产生有偏见的预测，并继承其教师模型的部分偏见，这种偏见会延续到所有微调版本中。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup> 原始模型主要用于在使用整句做决策的任务上进行微调，例如序列分类、词元分类或问答，而非文本生成。<sup>[2](https://huggingface.co/distilbert/distilbert-base-uncased)</sup>

## References

1. [Victor Sanh, Lysandre Debut, Julien Chaumond, Thomas Wolf (2019). DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter. arXiv:1910.01108.](https://arxiv.org/abs/1910.01108)
2. [distilbert/distilbert-base-uncased model card, Hugging Face](https://huggingface.co/distilbert/distilbert-base-uncased)
3. [A Primer in BERTology: What We Know About How BERT Works, Transactions of the Association for Computational Linguistics](https://doi.org/10.1162/tacl_a_00349)
4. [DistilBERT, an overview, ScienceDirect Topics](https://www.sciencedirect.com/topics/computer-science/distilbert)
5. [COS 484 lecture 16, Princeton NLP](https://princeton-nlp.github.io/cos484/lectures/lec16.pdf)
6. [Efficient Transformers for On-Device NLP Applications, ACE Journal (2025)](https://www.acejournal.org/2025/06/18/efficient-transformers-for-on-device-nlp)
7. [On the effectiveness of compact biomedical transformers, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC10027428/)
8. [Knowledge Distillation: Soft Labels, Temperature, and Dark Knowledge, Adnan Harun Dogan, METU](https://user.ceng.metu.edu.tr/~adhd/blogs/knowledge-distillation/index.html)
9. [Utilizing BERT for Information Retrieval: Survey, Applications, Resources, and Challenges, ACM Computing Surveys](https://doi.org/10.1145/3648471)
10. [Question answering, Hugging Face Transformers documentation](https://huggingface.co/docs/transformers/en/tasks/question_answering)
11. [BERTino: an Italian DistilBERT model, arXiv:2303.18121](https://arxiv.org/abs/2303.18121)
12. [Lightweight Transformer Models for On-Device Fault Detection: A Benchmark Study on Resource-Constrained Deployment, arXiv:2606.24173](https://arxiv.org/abs/2606.24173)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Language and vision AI › Natural language processing › NLP software, people, and community › Specialized NLP software and applications

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/distilbert

English original: https://www.edgepedia.org/distilbert
