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

S200331082: <p>I’m getting this error saying：</p>
<pre><code class="lang-auto">RuntimeError: Expected object of type torch.LongTensor but found type torch.IntTensor for argument #3 'index'
</code></pre>
<p>Here is a full traceback.</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "main.py", line 156, in &lt;module&gt;
    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 &lt;listcomp&gt;
    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'
</code></pre>
<p>But what does it mean by <code>argument #3 "index"</code> ? I can’t find "<code>index</code> " argument, and I don’t know how to change the data type of the <code>lables</code> in <code>target.scatter_(0, lables, scores)</code>.<br>
Any comment (even though it’s short!) or just listing keywords to look at will be highly appreciated!</p>
<p>The code comes from an open source project: <a href="https://github.com/aioz-ai/MICCAI19-MedVQA/blob/master/README.md" rel="noopener nofollow ugc">MICCAI19-MedVQA/README.md at master · aioz-ai/MICCAI19-MedVQA (github.com)</a></p>

ptrblck: <p>The <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_.html"><code>scatter_</code> docs</a> name the arguments as:</p>
<pre><code class="lang-auto">Tensor.scatter_(dim, index, src, reduce=None) → Tensor
</code></pre>
<p>so the second argument is expected to be a <code>LongTensor</code> as also mentioned in the docs:</p>
<pre><code class="lang-auto">        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'.
</code></pre>
<p>Based on the stack trace it seems that your custom <code>Dataset</code> uses the <code>scatter_</code> operation in the <code>__getitem__</code>:</p>
<pre><code class="lang-auto">  File "E:\pyprojects\MICCAI19-MedVQA-master\dataset_RAD.py", line 243, in __getitem__
    target.scatter_(0, labels, scores)
</code></pre>
<p>so you would need to change <code>labels</code> to a <code>LongTensor</code> via:</p>
<pre><code class="lang-python">    target.scatter_(0, labels.long(), scores)
</code></pre>

S200331082: <p>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.<br>
I also noticed that you often answer other people’s questions in the discussion area. I would like to extend my high respect.</p>