Add values to a tensor element based on its position in pytorch
In pytorch, I want to add values to elements in the tensor based on their position. For example consider, 

Input = torch.tensor([1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0])


Between several offsets of the Input array, Offsets = [0,5,10,15,20], I want to add different values, ValuesToAdd = [10,100,1000,10000]

I expect the output to be 

Output = torch.tensor([11,12,13,14,15,106,107,108,109,100,1001,1002,1003,1004,1005,10006,10007,10008,10009,10000])


Here, between indices Offsets[i] and Offsets[i+1] in Input array, ValuesToAdd[i] is added. For example, for indices 10,11,12,13 and 14 (Offsets[2] = 10 to Offsets[3]=15) in Input array, 1000 (ValuesToAdd[2]) is added.

How can I achieve this? Instead of looping over Offsets array, I am looking for a more efficient way.

You can use torch.repeat_interleave

Offsets = torch.tensor(Offsets)
shifts = Offsets[1:] - Offsets[:-1]
output = Input.clone()
output[Offsets[0]:Offsets[-1]] += torch.tensor(ValuesToAdd).repeat_interleave(shifts)
print(torch.all(output == Output))
# True

