NLP之DistilBERT:DistilBERT的简介、安装和使用方法、案例应用之详细攻略
目录
《DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter》翻译与解读
相关论文
《DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter》翻译与解读
地址 | |
时间 | 2019年10月2日 |
作者 | Victor Sanh, Lysandre Debut, Julien Chaumond, Thomas Wolf Hugging Face |
总结 | 这篇论文描述了DistilBERT模型,就是对原始BERT模型进行 distillation(蒸馏)后的一个更小、更快、更便宜的版本。 该论文提出的主要要点包括: 背景痛点:随着转移学习在自然语言处理中的广泛应用,在设备边缘和计算能力有限的场景下驱动这些大型预训练模型还存在挑战。 解决方案:利用知识蒸馏技术在预训练阶段对BERT模型进行压缩,获得了体积仅原始BERT模型40%、但语言理解能力保持97%的DistilBERT模型。 核心技术:提出了三元损失功能,结合语言模型损失、蒸馏损失和角度距离损失,最大限度提取更大模型在预训练阶段学习到的先验假设。 创新特点:DistilBERT模型规模更小、速度更快,预训练和应用成本更低,更适用于设备边缘计算。 优势:论文通过概念验证实验和设备端对比实验验证了DistilBERT在许多任务上的表现与原始BERT模型相当,同时在速度和资源消耗上具有明显优势。 总的来说,这篇论文通过知识蒸馏技术对BERT模型进行压缩,获得了体积和计算成本更低的DistilBERT模型,解决了大模型在边缘场景运用的挑战,同时保留了很好的下游任务表现,在模型推广和应用上具有明显优势。 |
Abstract
As Transfer Learning from large-scale pre-trained models becomes more prevalent in Natural Language Processing (NLP), operating these large models in on-the-edge and/or under constrained computational training or inference budgets remains challenging. In this work, we propose a method to pre-train a smaller general-purpose language representation model, called DistilBERT, which can then be fine-tuned with good performances on a wide range of tasks like its larger counterparts. While most prior work investigated the use of distillation for building task-specific models, we leverage knowledge distillation during the pre-training phase and show that it is possible to reduce the size of a BERT model by 40%, while retaining 97%of its language understanding capabilities and being 60% faster. To leverage the inductive biases learned by larger models during pre-training, we introduce a triple loss combining language modeling, distillation and cosine-distance losses. Our smaller, faster and lighter model is cheaper to pre-train and we demonstrate its capabilities for on-device computations in a proof-of-concept experiment and a comparative on-device study. | 随着从大规模预训练模型进行迁移学习在自然语言处理(NLP)中变得更为普遍,将这些大型模型运用于边缘计算和/或在受限的计算训练或推理预算下仍然具有挑战性。在这项工作中,我们提出了一种方法,即预训练一个较小的通用语言表示模型,称为DistilBERT,然后可以在广泛的任务上进行良好性能的微调,类似于其较大的对应物。尽管大多数先前的研究探讨了使用蒸馏来构建特定任务模型,但我们在预训练阶段利用知识蒸馏,并展示了可以将BERT模型的大小减小40%,同时保留其97%的语言理解能力并提高60%的速度。为了利用在预训练期间较大模型学到的归纳偏见,我们引入了一个三重损失,结合了语言建模、蒸馏和余弦距离损失。我们的较小、更快、更轻的模型更便宜进行预训练,并在一个概念验证实验和一个比较性的设备内研究中展示了其在设备上计算的能力。 |
6、Conclusion and future work
We introduced DistilBERT, a general-purpose pre-trained version of BERT, 40% smaller, 60% faster, that retains 97% of the language understanding capabilities. We showed that a general-purpose language model can be successfully trained with distillation and analyzed the various components with an ablation study. We further demonstrated that DistilBERT is a compelling option for edge applications. | 我们介绍了DistilBERT,这是BERT的通用预训练版本,体积缩小40%,速度提高60%,保留了97%的语言理解能力。我们展示了可以成功使用蒸馏训练通用语言模型,并通过消融研究分析了各个组件。我们进一步证明了DistilBERT是边缘应用的一个引人注目的选择。 |
DistilBERT的简介
huggingface地址:https://huggingface.co/docs/transformers/model_doc/distilbert
DistilBERT的安装和使用方法
1、使用提示
>> DistilBERT 没有 token_type_ids,您不需要指定哪个标记属于哪个段落。只需使用分隔标记 tokenizer.sep_token(或 [SEP])来分隔您的段落。
>> DistilBERT 没有选择输入位置的选项(position_ids 输入)。如果有必要,可以添加此选项,只需告诉我们您是否需要此选项。
>> 与 BERT 相同但更小。通过对预训练的 BERT 模型进行蒸馏训练,这意味着它已被训练为预测与较大模型相同的概率。实际目标是:
● 找到与教师模型相同的概率
● 正确预测掩码标记(但没有下一句目标)
● 学生和教师模型的隐藏状态之间的余弦相似度
2、资源
以下是一些官方 Hugging Face 和社区(由 ��� 表示)资源,可帮助您入门 DistilBERT。如果您有兴趣提交资源以包含在此处,请随时提出拉取请求,我们将进行审核!资源最好展示一些新的东西,而不是重复现有资源。
文本分类 | 文本分类 一篇关于使用 Python 入门使用 DistilBERT 进行情感分析的博客文章。 一篇关于如何使用 Blurr 对序列分类进行 DistilBERT 训练的博客文章。 一篇关于如何使用 Ray 调整 DistilBERT 超参数的博客文章。 一篇关于如何在 Hugging Face 和 Amazon SageMaker 中训练 DistilBERT 的博客文章。 一份关于如何对多标签分类进行 DistilBERT 微调的笔记本。��� 一份关于如何使用 PyTorch 对多类别分类进行 DistilBERT 微调的笔记本。��� 一份关于如何在 TensorFlow 中对文本分类进行 DistilBERT 微调的笔记本。��� DistilBertForSequenceClassification 受到此示例脚本和笔记本的支持。 TFDistilBertForSequenceClassification 受到此示例脚本和笔记本的支持。 FlaxDistilBertForSequenceClassification 受到此示例脚本和笔记本的支持。 文本分类任务指南 |
标记分类 | 标记分类 DistilBertForTokenClassification 受到此示例脚本和笔记本的支持。 TFDistilBertForTokenClassification 受到此示例脚本和笔记本的支持。 FlaxDistilBertForTokenClassification 受到此示例脚本的支持。 ��� Hugging Face 课程的标记分类章节。 标记分类任务指南 |
填充掩码 | 填充掩码 DistilBertForMaskedLM 受到此示例脚本和笔记本的支持。 TFDistilBertForMaskedLM 受到此示例脚本和笔记本的支持。 FlaxDistilBertForMaskedLM 受到此示例脚本和笔记本的支持。 ��� Hugging Face 课程的填充语言建模章节。 填充语言建模任务指南 |
问答 | 问答 DistilBertForQuestionAnswering 受到此示例脚本和笔记本的支持。 TFDistilBertForQuestionAnswering 受到此示例脚本和笔记本的支持。 FlaxDistilBertForQuestionAnswering 受到此示例脚本的支持。 ��� Hugging Face 课程的问答章节。 问答任务指南 |
3、多项选择
DistilBertForMultipleChoice 受到此示例脚本和笔记本的支持。
TFDistilBertForMultipleChoice 受到此示例脚本和笔记本的支持。
多项选择任务指南
4、优化器
一篇关于如何使用 ��� Optimum 和 Intel 对 DistilBERT 进行量化的博客文章。
一篇关于如何使用 ��� Optimum 优化 GPU 上的 Transformers 的博客文章。
一篇关于如何使用 Hugging Face Optimum 优化 Transformers 的博客文章。
5、推理
一篇关于如何使用 Hugging Face Transformers 和 AWS Inferentia 加速 BERT 推理,使用 DistilBERT。
一篇关于使用 Hugging Face’s Transformers、DistilBERT 和 Amazon SageMaker 进行无服务器推理的博客文章。
6、部署
一篇关于如何在 Google Cloud 上部署 DistilBERT 的博客文章。
一篇关于如何使用 Amazon SageMaker 部署 DistilBERT 的博客文章。
一篇关于如何使用 Hugging Face Transformers、Amazon SageMaker 和 Terraform 模块部署 BERT 的博客文章。
DistilBERT的案例应用
更新中……