Hi all,
I am new to Pytorch and machine learning and I recently tried to run https://github.com/matthew-zhu/MACC-RL/blob/master/DQN-loc.py and i am constantly getting the following error:
Line 141: reward_batch = torch.cat(batch.reward)
RuntimeError: Expected object of scalar type Long but got scalar type Int for sequence element 1 in sequence argument at position #1 ‘tensors’
I tried to find its solutions for half a day but still no luck
From reading different posts (i.e. https://github.com/rusty1s/pytorch_cluster/issues/19), I believe this error occurred because I am running Windows 10? Has anyone encountered this issue and how to resolve it?
Python: 3.7
Torch: 1.4.0+cu92
What type
is batch.reward
? If it’s a list
, could you check if all tensors have the same dtype
?
Based on the error message it seems that some tensors might be stored as IntTensor
s while others might be LongTensor
s.
If that’s the case, convert them to the same type via tensor.int()
or tensor.long()
.
Hi @ptrblck,
Yes. The batch.reward is an array/tuple of tensors. Some of the tensors have dtype = torch.int32 and some has dtype = torch.int64.
I just want to have all tensors in batch.rewards as Int. Am i doing this correct as shown below?
count = 0
For single_reward in batch.reward:
{if count == 0: reward_tuple = single_reward.int()
else: reward_tuple = torch.cat([reward_tuple, single_reward.int()], dim=0) #add tensor to tuple
count= count + 1 }
batch.reward = reward_tuple
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.