Hi,
I have a tensor of shape (7, 4, 1, 80, 2)
and I would like to modify some of its indices. I’ve tried to do this in two different indexing steps.
- index relevant indices at dimension 1 (of size 4)
x[torch.arange(x.size(0)).unsqueeze(1), idx]
whereidx
is of shape(7,3)
, meaning I index3
out of4
indices. - index relevant indices at dimension 4 (of size 80) which turns my code into:
x[torch.arange(x.size(0)).unsqueeze(1), idx][[
torch.arange(x.size(0))[:, None, None, None],
torch.arange(x.size(1) - 1)[:, None, None],
torch.arange(x.size(2))[:, None],
inner_idx
]]
where inner_idx
is of shape (7,3, 1, 60)
, meaning I index 60
out of 80
indices
finally I modify the tensor values:
x[torch.arange(x.size(0)).unsqueeze(1), idx][[
torch.arange(x.size(0))[:, None, None, None],
torch.arange(x.size(1) - 1)[:, None, None], # x.size(1) - 1 since 4 turned into 3
torch.arange(x.size(2))[:, None],
inner_idx
]] = new_values
where new_values
is of shape (7, 3, 1, 60, 2)
However x is not modified.
Is there anyway to solve this issue?
Thanks
Edit:
Reading more on this issue, I think it might have to do with gradients.
I will elaborate that this issue still persists regardless of x.requires_grad = True/False
. For new_values
, requires_grad
is set to True
, since I need this for training the model.