BloodAxe/pytorch-toolbelt

Loss.SoftCrossEntropyLoss() not work!!!

YanqingWu opened this issue · 4 comments

Loss.SoftCrossEntropyLoss() not work.

@YanqingWu without full case description it is useless issue.

@BloodAxe @konstantin Maksimov

import torch
import pytorch_toolbelt.losses as loss
sfce = loss.SoftCrossEntropyLoss()
outputs = torch.rand(10, 2).float()
targets = torch.randint(0, 2, (10, 1)).view(-1).long()
targets2 = torch.randint(0, 2, (10, 1)).long()
print('test 1: ', sfce(outputs, targets))
print('test 2: ', sfce(outputs, targets2))

two tests I get the same error: It should not be a dimension not match or some other common problem.
May be I have the wrong torch version?
print(torch.version)
I get : 1.3.1

Traceback (most recent call last):
File "/usr/local/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 7, in
print('test 1: ', sfce(outputs, targets))
File "/usr/local/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in call
result = self.forward(*input, **kwargs)
File "/usr/local/anaconda3/lib/python3.7/site-packages/pytorch_toolbelt/losses/soft_ce.py", line 15, in forward
not_ignored = label_target != self.ignore_index
TypeError: ne() received an invalid combination of arguments - got (NoneType), but expected one of:

  • (Tensor other)
    didn't match because some of the arguments have invalid types: (!NoneType!)
  • (Number other)
    didn't match because some of the arguments have invalid types: (!NoneType!)

Thanks for the reproduction code. As for now you can use quick workaround loss.SoftCrossEntropyLoss(ignore_index=-100), Correct default value will be fixed in 0.3.1 release.