* 支持 `C-DARTS <https://github.com/microsoft/nni/blob/v1.4/docs/zh_CN/NAS/CDARTS.rst>`__ 算法并增加 `the 示例 <https://github.com/microsoft/nni/tree/v1.4/examples/nas/cdarts>`__ using it
* 支持 `C-DARTS <https://github.com/microsoft/nni/blob/v1.4/docs/zh_CN/NAS/CDARTS.md>`__ 算法并增加 `the 示例 <https://github.com/microsoft/nni/tree/v1.4/examples/nas/cdarts>`__ using it
知识蒸馏,在 `Distilling the Knowledge in a Neural Network <https://arxiv.org/abs/1503.02531>`__ 中,压缩模型被训练成模拟预训练的大模型。 这种训练设置也称为"师生(teacher-student)"方式,其中大模型是教师,小模型是学生。
在 `Distilling the Knowledge in a Neural Network <https://arxiv.org/abs/1503.02531>`__\ 中提出了知识蒸馏(KD)的概念, 压缩后的模型被训练去模仿预训练的、较大的模型。 这种训练设置也称为"师生(teacher-student)"方式,其中大模型是教师,小模型是学生。 KD 通常用于微调剪枝后的模型。
.. image:: ../../img/distill.png
:target: ../../img/distill.png
:alt:
用法
^^^^^
...
...
@@ -19,24 +18,29 @@ PyTorch 代码
.. code-block:: python
from knowledge_distill.knowledge_distill import KnowledgeDistill
kd = KnowledgeDistill(kd_teacher_model, kd_T=5)
alpha = 1
beta = 0.8
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.cross_entropy(output, target)
# 只需要添加以下行来使用知识蒸馏微调模型
loss = alpha * loss + beta * kd.loss(data=data, student_out=output)
loss.backward()
for batch_idx, (data, target) in enumerate(train_loader):