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

an error on dice_loss.py #16

Open
kongzhinvwang2 opened this issue Sep 12, 2021 · 2 comments
Open

an error on dice_loss.py #16

kongzhinvwang2 opened this issue Sep 12, 2021 · 2 comments

Comments

@kongzhinvwang2
Copy link

您好,我现在想在ner的任务中使用dice_loss,我的设置如下:
a = torch.rand(13,3)
b = torch.tensor([0,1,1,1,1,1,1,1,1,1,1,1,2])
f = DiceLoss(with_logits=True,smooth=1, ohem_ratio=0.3,alpha=0.01)
f(a,b)
当我运行之后,报错如下:
发生异常: TypeError
unsupported operand type(s) for &: 'int' and 'Tensor'

报错的位置在 _multiple_class
cond = (torch.argmax(flat_input, dim=1) == label_idx & flat_input[:, label_idx] >= threshold) | pos_example.view(-1)

或许是先运行了label_idx & flat...
由于我没有仔细阅读论文中的算法描述,所以并不清楚这一部分的逻辑,也不知道如何修改,特来请教!

@xiaoya-li
Copy link
Contributor

xiaoya-li commented Sep 22, 2021

您好,参考论文中dice-loss的实现, 需要将计算损失函数设置为f = DiceLoss(with_logits=True,smooth=1, ohem_ratio=0,alpha=0.01)。
这样您提到的将a = torch.rand(13,3)b = torch.tensor([0,1,1,1,1,1,1,1,1,1,1,1,2])作为输入,就可以正常计算模型的损失。

@zhuyuedlut
Copy link

我觉得是报错的地方应该修改为:
cond = ((torch.argmax(flat_input, dim=1) == label_idx) & (flat_input[:, label_idx] >= threshold)) | pos_example.view(-1)
@kongzhinvwang2 @xiaoya-li 不知道是否正确

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