Solved by fmassa in post #3 
              
              
                Your solution should work, but instead of using the inplace operation on Variables, it might be better to use them out-of-place. 
res = Variable(torch.zeros(2, 5))
res = res.scatter(1, indices, topk)