Expected object of scalar type Long but got scalar type Float for argument #2 'target'

here is my dataloader i am thinking maybe problem occurs due to my dataset
def init(self, path, data_name, batchsize=1, steps=None, shuffle=False, transforms=None):
self.x, self.y = self.load_data(path, data_name)
self.transforms = transforms
self.steps = steps
if steps is not None:
self.idx_mapping = np.random.randint(0, self.x.shape[0], steps*batchsize)
self.steps = self.steps * batchsize

def __len__(self):
    return self.steps if self.steps is not None else self.x.shape[0]

def __getitem__(self, index):
    if self.steps is not None:
        index = self.idx_mapping[index]

    if self.transforms is not None:
        x, y = self.transforms(images=self.x[None, index], segmentation_maps=self.y[None, index])
    else:
        x, y = self.x[None, index], self.y[None, index]

    x, y = x.astype('float32')[0]/255., y.astype('float32')[0]/255.
    x, y = ToTensor()(x), ToTensor()(y)

    return x, y

Could you rerun the script with CUDA_LAUNCH_BLOCKING=1 python script.py args in the latest PyTorch version and post the stacktrace here, please?

ok here is the stacktrace.

File “main.py”, line 61, in
main(args, CORE)
File “main.py”, line 51, in main
CORE.train(args)
File “/home/ali/BioNet_project/pytorch_version/utils.py”, line 88, in train
l = criterion(output, y.long())
File “/home/ali/anaconda3/envs/test/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “/home/ali/BioNet_project/pytorch_version/loss.py”, line 21, in forward
cross_entropy = F.nll_loss(predicted.log(), target, size_average=False)
File “/home/ali/anaconda3/envs/test/lib/python3.8/site-packages/torch/nn/functional.py”, line 2387, in nll_loss
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: cuda runtime error (710) : device-side assert triggered at /opt/conda/conda-bld/pytorch_1614378062065/work/aten/src/THCUNN/generic/SpatialClassNLLCriterion.cu:134
/opt/conda/conda-bld/pytorch_1614378062065/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn_SpatialClassNLLCriterion_updateOutput_kernel: block: [1,0,0], thread: [960,0,0] Assertion t >= 0 && t < n_classes failed.
/opt/conda/conda-bld/pytorch_1614378062065/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn_SpatialClassNLLCriterion_updateOutput_kernel: block: [1,0,0], thread: [448,0,0] Assertion t >= 0 && t < n_classes failed.
/opt/conda/conda-bld/pytorch_1614378062065/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn_SpatialClassNLLCriterion_updateOutput_kernel: block: [1,0,0], thread: [449,0,0] Assertion t >= 0 && t < n_classes failed.
/opt/conda/conda-bld/pytorch_1614378062065/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn_SpatialClassNLLCriterion_updateOutput_kernel: block: [1,0,0], thread: [450,0,0] Assertion t >= 0 && t < n_classes failed.
/opt/conda/conda-bld/pytorch_1614378062065/work/aten/src/THCUNN/SpatialClassNLLCriterion.cu:106: cunn_SpatialClassNLLCriterion_updateOutput_kernel: block: [1,0,0], thread: [456,0,0] Assertion t >= 0 && t < n_classes failed.

Thanks for the update!
It seems my guess was right and the targets contain indeed invalid class indices.
nn.CrossEntropyLoss and nn.NLLLoss expect a model output in the shape [batch_size, nb_classes] and a target in the shape [batch_size] containing the class indices in [0, nb_classes-1].
Based on the error message the target is out of bounds and creates this error.