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

typo in KLDivTeacherList? #18

Open
jihyukkim-nlp opened this issue Jan 9, 2022 · 0 comments
Open

typo in KLDivTeacherList? #18

jihyukkim-nlp opened this issue Jan 9, 2022 · 0 comments

Comments

@jihyukkim-nlp
Copy link

jihyukkim-nlp commented Jan 9, 2022

Thank you for sharing codes.
I wonder whether there is a typo in the implementation of KLDivTeacherList class.

The implementation is

class KLDivTeacherList(nn.Module):
    def __init__(self):
        super(KLDivTeacherList, self).__init__()
        self.kl = torch.nn.KLDivLoss(reduction="batchmean")
    def forward(self, scores, labels):
        loss = self.kl(scores.softmax(-1),labels.softmax(-1)) # is this a typo?
        return loss

However, PyTorch documentation for KLDivLoss (https://pytorch.org/docs/1.9.1/generated/torch.nn.KLDivLoss.html) says

  • As with NLLLoss, the input given is expected to contain log-probabilities.
  • The targets are interpreted as probabilities by default,

So, from what I understand, the forward function should be

def forward(self, scores, labels):
    # loss = self.kl(scores.softmax(-1),labels.softmax(-1)) # is this a typo?
    # before: softmax of scores

    # after : log-softmax of scores
    loss = self.kl(torch.nn.functional.log_softmax(scores, dim=-1), torch.nn.functional.softmax(labels, dim=-1))
    return loss

I wonder if this is a typo or if I'm missing something.
Thanks in advance.

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

1 participant