pytorch-unsupervised-segmentation-tip icon indicating copy to clipboard operation
pytorch-unsupervised-segmentation-tip copied to clipboard

loss_fn_scr Errors

Open dataangel opened this issue 4 years ago • 2 comments

Traceback (most recent call last): File "demo.py", line 145, in loss = args.stepsize_sim * loss_fn(output[ inds_sim ], target[ inds_sim ]) + args.stepsize_scr * loss_fn_scr(output[inds_scr], target_scr[inds_scr]) File "d:\tools\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "d:\tools\Anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 962, in forward ignore_index=self.ignore_index, reduction=self.reduction) File "d:\tools\Anaconda3\lib\site-packages\torch\nn\functional.py", line 2468, in cross_entropy return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction) File "d:\tools\Anaconda3\lib\site-packages\torch\nn\functional.py", line 2264, in nll_loss ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index) RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' in call to _thnn_nll_loss_forward

dataangel avatar Nov 19 '20 16:11 dataangel

I got the error on the same line, but it was: RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'target'

The reason was in target_scr[inds_scr]. In the initial step, it has type int32, but should have int64. So I changed the code into:

        loss = args.stepsize_sim * loss_fn(output[inds_sim], target[inds_sim]) + \
               args.stepsize_scr * loss_fn_scr(output[inds_scr], target_scr[inds_scr].type(torch.int64)) + \
               args.stepsize_con * (lhpy + lhpz)

annamykol avatar Mar 04 '21 15:03 annamykol

There is a simple solution.

loss = args.stepsize_sim * loss_fn(output[ inds_sim ], target[ inds_sim ]) + \
       args.stepsize_scr * loss_fn_scr(output[ inds_scr ], target_scr[ inds_scr ].long()) + \
       args.stepsize_con * (lhpy + lhpz)

kimphys avatar Mar 26 '21 08:03 kimphys