Search code examples
pythonpytorchtensormatrix-indexing

How to convert topk data from torch.tensor.topk() to a pytorch tensor


I have a 1-d pytorch tensor and I got topk data of the tensor and indeces of this datas. How can I place each data in the corresponding position of a empty pytorch tensor?

The topk of this tensor elements and their indeces is got by: value, indeces = toech.topk(T,K) and I also have an empty tensor t of the same size as T. How can I copy each value[i] to t[indeces[i]]? I have tried to use for i in value but this way is too slow when data isn't very small.


Solution

  • This should work:

    t[indeces] = value