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

Dice Loss Error #2

Open
albertnanda opened this issue Apr 14, 2021 · 3 comments
Open

Dice Loss Error #2

albertnanda opened this issue Apr 14, 2021 · 3 comments

Comments

@albertnanda
Copy link

albertnanda commented Apr 14, 2021

I have two part question,

  1. The example given in the code bugs out i.e.
    >>> loss = DiceLoss(with_logits=True, ohem_ratio=0.1)

    IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
  2. The other question is related to the implementation, say the classifier has perfectly predicted the labels, but there would be still some dice loss because of loss = 1 - ((2 * interection + self.smooth) /
    (torch.sum(torch.square(flat_input, ), -1) + torch.sum(torch.square(flat_target), -1) + self.smooth))
    smooth. Is this the expected behavior or am I missing something.
input = torch.FloatTensor([[1., .0, .0, .0],[0., 1, .0, .0]])
target = torch.LongTensor([0, 1])
loss = DiceLoss(with_logits=False,reduction=None,ohem_ratio=0.)
input.requires_grad=True
output = loss(input, target)

Output
tensor([1.9998, 1.9998], grad_fn=)

@xiaoya-li
Copy link
Contributor

xiaoya-li commented Apr 15, 2021

Hey, thanks for asking.
Response to quesiton2:
As shown in

loss_fct = DiceLoss(with_logits=True, smooth=self.args.dice_smooth, ohem_ratio=self.args.dice_ohem,

we recommend using the following setting for multi-class tasks:

$ loss_fct = DiceLoss(square_denominator=True, with_logits=False, index_label_position=True,
                        smooth=1, ohem_ratio=0, alpha=0.01, reduction="none")
  • If input_probs = torch.FloatTensor([[0.9, 0.03, 0.03, 0.03],[0.03, 0.9, 0.03, 0.03]]) and target = torch.LongTensor([0, 1]), then the dice loss should be :
$ loss_fct(input_probs, target)
> tensor([0.0079, 0.0079])
  • If input_probs = torch.FloatTensor([[0.9, 0.1, .0, .0],[.0, 0.9, 0.1, .0]]) and target = torch.LongTensor([0, 1]), then the dice loss should be :
$ loss_fct(input_probs, target)
> tensor([0.0151, 0.0151])

These are in line with our expectations.

@albertnanda
Copy link
Author

albertnanda commented Apr 16, 2021

@xiaoya-li : Let me try it out, also can you recommend the settings for Multi-Label classification and NER task.
NER Task:

inp = torch.FloatTensor([[[.1,.2,.3,.4]]*4,[[.5,.5,0,0]]*4]) #2 sentences, 4 words and 4 tags per word
target = torch.LongTensor([[0,1,1,2],[0,3,2,3]]) #tags 0,3, 2 sentences and 4 words

@kk19990709
Copy link

I have also met question 1, do you have any solution?@albertnanda

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

3 participants