vandit15 / Class-balanced-loss-pytorch

Pytorch implementation of the paper "Class-Balanced Loss Based on Effective Number of Samples"
MIT License
781 stars 120 forks source link

why modulator? #9

Open lai199508 opened 4 years ago

lai199508 commented 4 years ago

Hi, i'm interested in your work! Now i have problem, why your implement code of focal loss use "modulator = torch.exp(-gamma labels logits - gamma torch.log(1 + torch.exp(-1.0 logits)))" ?

xiaoyu825 commented 4 years ago

it's just a transfer of the formula,but "labels" should not show in the code.i think it is modulator = torch.exp(-gamma logits - gamma torch.log(1 + torch.exp(-1.0 * logits)))

wutong16 commented 4 years ago

it's the formula of focal loss, and "labels" should be reserved. image notice that y' = 1 / (1 + torch.exp(-1.0 * logits)), and both logits and labels are matrixs.

for where labels is 0, modulator = (1 / (1 + torch.exp(-1.0 * logits))) gamma, and for where labels is 1, modulator = (torch.exp(-1.0 logits) / (1 + torch.exp(-1.0 logits)) gamma = (1 / (1 + torch.exp(logits)) ** gamma

matching the formula well.