python - 与交叉熵损失相比,为什么蒸馏损失不收敛(减少)?
问题描述
我在持续学习(又名增量学习)模型中使用交叉熵 && 蒸馏损失。但是,蒸馏损失不会收敛,而交叉熵损失会收敛。
这是我的蒸馏损失和训练部分的代码。
蒸馏损失代码:
def loss_fn_distillation(outputs, soft_labels, temperature, current_step, total_step, total_label):
current_label = (total_label / total_step) * (current_step + 1)
previous_label = (total_label / total_step) * current_step
soft_labels = V(soft_labels.data, requires_grad=False).cuda()
soft_labels = torch.softmax(soft_labels / temperature, dim=1)
outputs = F.log_softmax(outputs[:,:-int(current_label-previous_label)]/temperature, dim = 1)
distill_loss = torch.sum(outputs * soft_labels, dim=1, keepdim=False)
distill_loss = -torch.mean(distill_loss, dim=0, keepdim=False)
return V(distill_loss, requires_grad=True).cuda()
训练部分代码:
outputs = net(inputs)
ce_loss = criterion(outputs, targets)
if(i>0) :
soft_label = previous_net(inputs)
distill_loss = loss_fn_distillation(outputs=outputs, soft_labels=soft_label, temperature=2,
current_step=i, total_step=step, total_label=number_label)
print(ce_loss, distill_loss)
loss = distill_loss + ce_loss
else :
loss = ce_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch 的交叉熵损失和蒸馏损失的结果:
我会很感激任何反馈。谢谢你。
解决方案
推荐阅读
- java - 如何给接口提供@After @Before注释
- javascript - 如何使用 django 只显示一些选项并选择?
- rx-java2 - 在 RxAndroidBle 流中抛出 UndeliverableException
- javascript - 如何在chart.js中显示两个数组之间的减法结果?
- sql - 连接所有组合
- c++ - 在迭代它时删除 std::shared_ptr 的 std::vector 中的最后一个元素会导致分段错误
- javascript - 比较数组的日期项并找到最大的
- android - 默认视图卡在顶部,无论如何要删除它
- javascript - 如何在 reactjs 应用程序中找到带有酶的元素?
- windows - 在 FOR 语句中的变量上使用子字符串