Is there anyway to add same index multiple times in one operation?
Let's say that I have two tensors value and index, which contain the data and all indexes we need. I want to add one to data in value with the corresponding index. If an index shows k times in tensor index, then this data should be added by k, instead of one.
Here's an example:
value = torch.zeros(3) # [0, 0, 0]
index = torch.zeros(10).long() #[0,0,0,0,0,0,0,0,0,0]
ret = some_func(value, index) # [10, 0, 0]

I know that using a for loop to go through all indexes in index could solve the problem, but I just wanna ask if there is more elegant way?

One way you can do this is with scatter_add:
In [54]: value = torch.zeros(3)

In [55]: index = torch.tensor([0, 0, 1, 0, 2, 2, 1, 1, 1])

In [56]: value.scatter_add(0, index, torch.ones_like(index, dtype=value.dtype))
Out[56]: tensor([3., 4., 2.])

You can use scatter_add_ to operate on value in place.

You might find it more efficient to use bincount():
In [63]: index = torch.tensor([0, 0, 1, 0, 2, 2, 1, 1, 1])

In [64]: counts = index.bincount(minlength=value.shape[0])

In [65]: counts
Out[65]: tensor([3, 4, 2])

If in your actual problem, value is initialized with 0s, then you're done--just use counts as the result.  Otherwise, add counts to value.
