mel
1
I want to increase specific indices in a tensor by 10%.
I have a tensor
x = torch.randint(-2,2,[4,3])
tensor([[-2, 0, 1],
[-2, 1, -2],
[ 0, 0, -1],
[ 0, -1, 1]])
and the following indices
indices = torch.tensor([[0,0,1],[2,1,1]])
I want to increase the values by 10% on the indices above, so that I get the following tensor
tensor([[-2, 0, 1.1],
[-2, 1.1, -2],
[ 0, 0, -1],
[ 0, -1, 1]])
How can I achieve that?
albanD
(Alban D)
2
Hi,
I guess you can do something like this no?
x[indices[0], indices[1]] *= 1.1
mel
3
HI @albanD thank you for the response.
I tried your solution but I lose the full matrix, ang get only the selected vector
x[indices[0], indices[1]]
>> tensor([ 1, -2, -1])
Also, multiplying by negative numbers by 1.1 will reduce them and instead of increasing them
so I guess I need a calculation similar to
x + x.abs() * 0.1
I just don’t know how to apply it only on the specific indices.
albanD
(Alban D)
4
Ho in that case you can do:
vals = x[indices[0], indices[1]]
updated_vals = vals + vals.abs() * 1.1
x[indices[0], indices[1]] = vals
mel
5
Thank you @albanD, just what I was looking for