How to get top-k elements of each row in a 2D tensor?
How to get the top-k elements of each row in a 2D tensor in an elegant way instead of using for-loop as below?

import torch

elements = torch.rand(5,10)
topk_list = [2,3,1,2,0] # means top2 for 1st row, top3 for 2nd row, top1 for 3rd row,....
index_list = [] # record the topk index in elements

for i in range(5):
    index_list.append(elements[i].topk(topk_list[i]))


If your k's don't vary too much and you want to vectorize your code you can first take the maximum top k per row and then gather the desired results.

# Code from OP
import torch

elements = torch.rand(5,10)
topk_list = [2,3,1,2,0] # means top2 for 1st row, top3 for 2nd row, top1 for 3rd row,....
index_list = [] # record the topk index in elements

for i in range(5):
    index_list.append(elements[i].topk(topk_list[i]))

# Print the result
print(index_list)

# Get topk for max_k
max_k = max(topk_list)
topk_vals, topk_inds = elements.topk(max_k, dim=-1)

# Select desired topk using mask
mask = torch.arange(max_k)[None, :] &lt; torch.tensor(topk_list)[:, None]
vals, inds = topk_vals[mask], topk_inds[mask]
rows, _ = mask.nonzero().T
print("-" * 10)
print("rows", rows)
print("inds", inds)
print("vals", vals)

# Or split
vals_per_row = vals.split(topk_list)
inds_per_row = inds.split(topk_list)
print("-" * 10)
print("vals_per_row", vals_per_row)
print("inds_per_row", inds_per_row)

# Or zip (for loop but should be cheap)
index_list = zip(vals_per_row, inds_per_row)
print("-" * 10)
print("zipped results", list(index_list))


This gives the following output:

[torch.return_types.topk(
values=tensor([0.8148, 0.7443]),
indices=tensor([8, 4])), torch.return_types.topk(
values=tensor([0.7529, 0.7352, 0.6354]),
indices=tensor([8, 1, 9])), torch.return_types.topk(
values=tensor([0.8792]),
indices=tensor([7])), torch.return_types.topk(
values=tensor([0.9626, 0.8728]),
indices=tensor([6, 2])), torch.return_types.topk(
values=tensor([]),
indices=tensor([], dtype=torch.int64))]
----------
rows tensor([0, 0, 1, 1, 1, 2, 3, 3])
inds tensor([8, 4, 8, 1, 9, 7, 6, 2])
vals tensor([0.8148, 0.7443, 0.7529, 0.7352, 0.6354, 0.8792, 0.9626, 0.8728])
----------
vals_per_row (tensor([0.8148, 0.7443]), tensor([0.7529, 0.7352, 0.6354]), tensor([0.8792]), tensor([0.9626, 0.8728]), tensor([]))
inds_per_row (tensor([8, 4]), tensor([8, 1, 9]), tensor([7]), tensor([6, 2]), tensor([], dtype=torch.int64))
----------
zipped results [(tensor([0.8148, 0.7443]), tensor([8, 4])), (tensor([0.7529, 0.7352, 0.6354]), tensor([8, 1, 9])), (tensor([0.8792]), tensor([7])), (tensor([0.9626, 0.8728]), tensor([6, 2])), (tensor([]), tensor([], dtype=torch.int64))]

