Hey, I am trying to sum values in a Tensor based on the index of the output array.
Here is an example
import torch
from torch.autograd import Variable
import numpy as np

data = Variable(torch.from_numpy(np.array([3, 2, 1, 8, 7], dtype=np.float32)), requires_grad=True)
idx = Variable(torch.from_numpy(np.array([0, 0, 1, 1, 2], dtype=np.int64)), requires_grad=False)

So I would like to obtain a Variable with values [5, 9, 7] which I can backpropagate through.
I tried doing it with
out = Variable(torch.zeros(3), requires_grad=True)
out.scatter_add_(0, idx, data)

but this won’t work as I am doing an in-place operation on a leaf Variable.
What is the correct way of doing this?