Hi,
I am trying to use tensor.index_copy_, but having some issues regarding tensor’s dimensions.
On the Pytorch documents, there is an example:
x = torch.zeros(5,3)
t = torch.tensor([[1,2,3], [4,5,6], [7,8,9]], dtype=torch.float)
index = torch.tensor([0, 4, 2])
x.index_copy_(0, index, t)
These codes result in x as:
x = [[1, 2, 3],
[0, 0, 0],
[7, 8, 9],
[0, 0, 0],
[4, 5, 6]]
I understood this example with the dimension of 0, but if I try to use it with dimension of 1 in similar example, I receive an runtime error that indicates some dimension issues. For the clarification, I will leave an example of this below:
x = torch.zeros(5,3)
t = torch.tensor([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]], dtype=torch.float)
index = torch.tensor([0, 2, 1])
x.index_copy_(1, index, t)
I expect to receive x as
[[1,    3,    2],
[4,    6,    3],
[7,    9,    8],
[10, 12, 11],
[13, 15, 14],]
but running these codes results in the following error:
RuntimeError: index_copy_(): Source.destination tensor must have same slice shapes. Destination slice shape: [5] at dimension 1 and source slice shape: [3] at dimension 0.
Could someone explain what is wrong with this? Thank you!