When i use nn.CrossEntropyLoss(),there always is RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed

File “E:\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)
File “E:\python_workspace\codee\TorchSeg\model\dfn\voc.dfn.R101_v1c\network.py”, line 179, in forward
loss1 = self.criterion(pred_out[1], label)
File “E:\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)
File “E:\anaconda3\lib\site-packages\torch\nn\modules\loss.py”, line 904, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File “E:\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1970, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File “E:\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1295, in log_softmax
ret = input.log_softmax(dim)
RuntimeError: cuda runtime error (77) : an illegal memory access was encountered at C:/a/w/1/s/tmp_conda_3.6_090826/conda/conda-bld/pytorch_1550394668685/work/aten/src/ATen/native/cuda/SoftMax.cu:545:

The error happens in the code

if label is not None and aux_label is not None:
loss0 = self.criterion(pred_out[0], label)
loss1 = self.criterion(pred_out[1], label)
loss2 = self.criterion(pred_out[2], label)
loss3 = self.criterion(pred_out[3], label)

The I try the code downside.

criterion = nn.CrossEntropyLoss(reduction=‘mean’,ignore_index=255)
input=np.random.randint(255,size=(2,21,512,512))
input= torch.from_numpy(input)
target=np.random.randint(255,size=(2,512,512))
target= torch.from_numpy(target)
loss=criterion(input.float(),target.long())

Then the ‘RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes’ failed.’ happen.
But when i try the code

loss=nn.CrossEntropyLoss()
input = torch.randn(3,21,50,50, requires_grad=True)
target = torch.empty(3,50,50, dtype=torch.long).random_(5)
output = loss(input, target)

No RuntimeError happen.What is the difference of the last two codes?How can I adjust the second code?And how can I adjust the first code?
Please give me a hand

According to the documentation, the target must have only one dimension and as much elements as the batch size. In your case:

You should have rather torch.empty(3, dtype=torch.long).random_(5).

More over, the input should have only 2 dimensions.

The last code works will.Just the first two code has problems.

loss=nn.CrossEntropyLoss()
input = torch.randn(3,21,50,50, requires_grad=True)
target = torch.empty(3,50,50, dtype=torch.long).random_(5)
output = loss(input, target)
output
tensor(3.5024, grad_fn=)

CrossEntropyLoss() don’t need the target to have the same channel with input,but the n,h and w need to be same.

I tried these two code

import torch
import torch.nn as nn
import numpy as np
loss = nn.CrossEntropyLoss()
input = torch.randn(3,21,50, 50)
print(‘input.shape=’,input.shape)
print(‘input.type=’,input.type)
print(‘input.dtype=’,input.dtype)
target = torch.empty(3,50,50, dtype=torch.long).random_(5)
print(‘target.shape=’,target.shape)
print(‘target.type=’,target.type)
print(‘target.dtype=’,target.dtype)
output = loss(input, target)
print(‘output=’,output)
print(‘output.shape=’,output.shape)
print(‘output.type=’,output.type)
print(‘output.dtype=’,output.dtype)

And the output is

input.shape= torch.Size([3, 21, 50, 50])
input.type= <built-in method type of Tensor object at 0x000002E15BF11828>
input.dtype= torch.float32
target.shape= torch.Size([3, 50, 50])
target.type= <built-in method type of Tensor object at 0x000002E160005090>
target.dtype= torch.int64
output= tensor(3.5127)
output.shape= torch.Size([])
output.type= <built-in method type of Tensor object at 0x000002E160005120>
output.dtype= torch.float32

But when i use

loss = nn.CrossEntropyLoss()
input=np.random.randint(255,size=(3,21,50,50))
input= torch.from_numpy(input).float()
print(‘input.shape=’,input.shape)
print(‘input.type=’,input.type)
print(‘input.dtype=’,input.dtype)
target=np.random.randint(255,size=(3,50,50))
target= torch.from_numpy(target).long()
print(‘target.shape=’,target.shape)
print(‘target.type=’,target.type)
print(‘target.dtype=’,target.dtype)
output = loss(input, target)

Part of the output is:

input.shape= torch.Size([3, 21, 50, 50])
input.type= <built-in method type of Tensor object at 0x000001AA0E5A4870>
input.dtype= torch.float32
target.shape= torch.Size([3, 50, 50])
target.type= <built-in method type of Tensor object at 0x000001AA125270D8>
target.dtype= torch.int64

It’s same as the part of the output in the first code
But there 's a error:

File “E:/python_workspace/res50.py”, line 15, in
output = loss(input, target)
File “E:\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)
File “E:\anaconda3\lib\site-packages\torch\nn\modules\loss.py”, line 904, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File “E:\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1970, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File “E:\anaconda3\lib\site-packages\torch\nn\functional.py”, line 1792, in nll_loss
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes’ failed. at c:\a\w\1\s\tmp_conda_3.6_090826\conda\conda-bld\pytorch_1550394668685\work\aten\src\thnn\generic/SpatialClassNLLCriterion.c:110

Why the first works well,but the second has mistakes?