How to sum values of same index together

Hi, now I need to sum all probability with same index together for my modified pointer network. which means
prob = Variable(torch.Tensor(len, batch)) indices = Variable(torch.LongTensor(len, batch)) output = Variable(torch.zeros(len, batch,vsize)) for pos in range(len): for b in range(batch): output[pos][b][indices[pos,b]] += prob[pos][b]
but this implementation is too brutal, How can I do this without for-loop? thanks very much!

I think you can do this using index_add_ http://pytorch.org/docs/tensors.html?highlight=index_add#torch.Tensor.index_add_

output.index_add_(2, indices.view(-1), prob)
1 Like