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

Mistake in the focal loss implementation? #180

Open
seva100 opened this issue Dec 15, 2021 · 0 comments
Open

Mistake in the focal loss implementation? #180

seva100 opened this issue Dec 15, 2021 · 0 comments

Comments

@seva100
Copy link

seva100 commented Dec 15, 2021

Hi, thank you very much for releasing this codebase -- it has been very useful to my project.

I'm wondering if there is a mistake in the Focal Loss implementation. The code in loss/focal.py first calculates CrossEntropy loss, averages it over all samples, and then applies a modulating factor loss = (1 - p) ** self.gamma * logp. If I understand the original Focal Loss paper correctly, they propose to calculate CrossEntropy, apply the modulating factor, and only then average the result over all samples.
I wonder, is the order changed on purpose in this repository? I think this way it might be losing the idea of Focal loss...

If it's actually a mistake, a simple fix in the line

self.ce = nn.CrossEntropyLoss()

to self.ce = nn.CrossEntropyLoss(reduction='none') will suffice.

Other implementations also seem to be having a different order, e.g. see 1 and 2.

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