Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why using log_softmax instead of softmax? #233

Closed
nguyenvulong opened this issue Jul 4, 2022 · 1 comment
Closed

Why using log_softmax instead of softmax? #233

nguyenvulong opened this issue Jul 4, 2022 · 1 comment

Comments

@nguyenvulong
Copy link

nguyenvulong commented Jul 4, 2022

Same question has been asked here and here . These repositories (I think you already know them) are other attempts to implement knowledge distillation algorithms.

Could you please explain why it used log_softmax instead of softmax?

def forward(self, student_output, teacher_output, targets=None, *args, **kwargs):
soft_loss = super().forward(torch.log_softmax(student_output / self.temperature, dim=1),
torch.softmax(teacher_output / self.temperature, dim=1))
if self.alpha is None or self.alpha == 0 or targets is None:
return soft_loss
hard_loss = self.cross_entropy_loss(student_output, targets)
return self.alpha * hard_loss + self.beta * (self.temperature ** 2) * soft_loss

@yoshitomo-matsubara
Copy link
Owner

Hi @nguyenvulong

See KLDivLoss in PyTorch document.

To avoid underflow issues when computing this quantity, this loss expects the argument input in the log-space. The argument target may also be provided in the log-space if log_target= True.

Also, please use Discussions above (instead of Issues) for questions.
As explained here, I want to keep Issues mainly for bug reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants