RuntimeError: Index tensor must have the same number of dimensions as self tensor

155 Views Asked by At

I am running a deep reinforcement learning code and I got this error:

RuntimeError: Index tensor must have the same number of dimensions as self tensor

The code snippet that is related to this error is:

def update_mask(mask, dynamic, chosen_idx,num_couriers= None):

print("Mask shape:", mask.shape)
print("Chosen_idx shape:", chosen_idx.shape)

chosen_idx = chosen_idx.to(torch.int64)
mask.scatter_(1, chosen_idx.unsqueeze(1), 0)

return mask

The output of the print statements is:

Mask shape: torch.Size([256, 20, 1]) Chosen_idx shape: torch.Size([256, 1, 20])

I tried to use unsqueeze(1) to both mask and chosen_idx, and also tried to specify the dim parameter in torch.scatter_() function, but none of them worked. I still got the same error or a different error.

I am not sure what is wrong with my index tensor and how to fix it. Can anyone help me with this? Thank you very much.

I expect the update_mask function to mark the visited city in the mask tensor, so that it can't be selected again in the next step. The mask tensor should have the same shape as the input tensor, and the value at the visited city index should be zero.

0

There are 0 best solutions below