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？