RuntimeError: Expected object of scalar type Long but got scalar type Int for sequence element 1 in sequence argument at position #1 'tensors'

freeman0301: <p>Hi all,<br>
I am new to Pytorch and machine learning and I recently tried to run <a href="https://github.com/matthew-zhu/MACC-RL/blob/master/DQN-loc.py" rel="nofollow noopener">https://github.com/matthew-zhu/MACC-RL/blob/master/DQN-loc.py</a> and i am constantly getting the following error:</p>
<p>Line 141:    reward_batch = torch.cat(batch.reward)<br>
RuntimeError: Expected object of scalar type Long but got scalar type Int for sequence element 1 in sequence argument at position <span class="hashtag">#1</span> ‘tensors’</p>
<p>I tried to find its solutions for half a day but still no luck <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"><br>
From reading different posts (i.e. <a href="https://github.com/rusty1s/pytorch_cluster/issues/19" rel="nofollow noopener">https://github.com/rusty1s/pytorch_cluster/issues/19</a>), I believe this error occurred because I am running Windows 10? Has anyone encountered this issue and how to resolve it?</p>
<p>Python: 3.7<br>
Torch: 1.4.0+cu92</p>

ptrblck: <p>What <code>type</code> is <code>batch.reward</code>? If it’s a <code>list</code>, could you check if all tensors have the same <code>dtype</code>?<br>
Based on the error message it seems that some tensors might be stored as <code>IntTensor</code>s while others might be <code>LongTensor</code>s.<br>
If that’s the case, convert them to the same type via <code>tensor.int()</code> or <code>tensor.long()</code>.</p>

freeman0301: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,<br>
Yes. The batch.reward is an array/tuple of tensors. Some of the tensors have dtype = torch.int32 and some has dtype = torch.int64.<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/3/4337fb480dc02865e9ec9dd3a4ad6f8d26b4b735.png" data-download-href="https://discuss.pytorch.org/uploads/default/4337fb480dc02865e9ec9dd3a4ad6f8d26b4b735" title="batch.reward type"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/3/4337fb480dc02865e9ec9dd3a4ad6f8d26b4b735.png" alt="batch.reward type" data-base62-sha1="9ADVGKFSHGTG2tJAG5C0cSlTmx7" width="690" height="189" data-dominant-color="414345"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">batch.reward type</span><span class="informations">974×268 17.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I just want to have all tensors in batch.rewards as Int. Am i doing this correct as shown below?<br>
count = 0<br>
For single_reward in batch.reward:<br>
{if count == 0: reward_tuple = single_reward.int()<br>
else: reward_tuple = torch.cat([reward_tuple, single_reward.int()], dim=0) <span class="hashtag-raw">#add</span> tensor to tuple<br>
count= count + 1 }<br>
batch.reward = reward_tuple</p>

ptrblck: <p>Your code should be working. Alternatively to concatenating the new data to the same tensor, you could also store all transformed tensors in a new list and transform it to a tensor afterwards.</p>