RuntimeError: Expected object of type torch.LongTensor but found type torch.IntTensor for argument #3 'index'

I’m getting this error saying:

RuntimeError: Expected object of type torch.LongTensor but found type torch.IntTensor for argument #3 'index'

Here is a full traceback.

Traceback (most recent call last):
  File "main.py", line 156, in <module>
    train(args, model, train_loader, eval_loader, args.epochs, args.output, optim, epoch)
  File "E:\pyprojects\MICCAI19-MedVQA-master\train.py", line 81, in train
    for i, (v, q, a, _, _, _) in enumerate(train_loader):
  File "E:\Anaconda\envs\pt\lib\site-packages\torch\utils\data\dataloader.py", line 314, in __next__
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "E:\Anaconda\envs\pt\lib\site-packages\torch\utils\data\dataloader.py", line 314, in <listcomp>
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "E:\pyprojects\MICCAI19-MedVQA-master\dataset_RAD.py", line 243, in __getitem__
    target.scatter_(0, labels, scores)
RuntimeError: Expected object of type torch.LongTensor but found type torch.IntTensor for argument #3 'index'

But what does it mean by argument #3 "index" ? I can’t find "index " argument, and I don’t know how to change the data type of the lables in target.scatter_(0, lables, scores).
Any comment (even though it’s short!) or just listing keywords to look at will be highly appreciated!

The code comes from an open source project: MICCAI19-MedVQA/README.md at master · aioz-ai/MICCAI19-MedVQA (github.com)

The scatter_ docs name the arguments as:

Tensor.scatter_(dim, index, src, reduce=None) → Tensor

so the second argument is expected to be a LongTensor as also mentioned in the docs:

        dim (int) – the axis along which to index

        index (LongTensor) – the indices of elements to scatter, can be either empty or of the same dimensionality as src. When empty, the operation returns self unchanged.

        src (Tensor or float) – the source element(s) to scatter.

        reduce (str, optional) – reduction operation to apply, can be either 'add' or 'multiply'.

Based on the stack trace it seems that your custom Dataset uses the scatter_ operation in the __getitem__:

  File "E:\pyprojects\MICCAI19-MedVQA-master\dataset_RAD.py", line 243, in __getitem__
    target.scatter_(0, labels, scores)

so you would need to change labels to a LongTensor via:

    target.scatter_(0, labels.long(), scores)

I should say that your suggestion is amazing.Using your method, I corrected the subsequent exceptionUsing your method, I corrected the subsequent exception.Thank you so^5 much.
I also noticed that you often answer other people’s questions in the discussion area. I would like to extend my high respect.

1 Like