There is a function in NumPy called np.add.at which allows to add values to elements accessed by multi-index with repeated elements, such that for each repeated element all its corresponding values will be considered in the summation. E.g. see an example:
A = np.zeros(5)
np.add.at(A, [1, 1, 2], 1)
A
produces:
array([0., 2., 1., 0., 0.])
Right now I am in a very large need for the same thing in PyTorch (I can’t avoid repeated indices in my task), as the plain summation has different behavior:
A = torch.zeros(5)
A[[1, 1, 2]] += 1
A
produces:
tensor([ 0., 1., 1., 0., 0.])
Is there any way to simulate behavior of np.add.at by PyTorch operations?
Thank you!