I have the following tensor
inp = tensor([[[ 0.0000e+00, 5.7100e+02, -6.9846e+00],
[ 0.0000e+00, 4.4070e+03, -7.1008e+00],
[ 0.0000e+00, 3.0300e+02, -7.2226e+00],
[ 0.0000e+00, 6.8000e+01, -7.2777e+00],
[ 1.0000e+00, 5.7100e+02, -6.9846e+00],
[ 1.0000e+00, 4.4070e+03, -7.1008e+00],
[ 1.0000e+00, 3.0300e+02, -7.2226e+00],
[ 1.0000e+00, 6.8000e+01, -7.2777e+00]],
[[ 0.0000e+00, 2.1610e+03, -7.0754e+00],
[ 0.0000e+00, 6.8000e+01, -7.2259e+00],
[ 0.0000e+00, 1.0620e+03, -7.2920e+00],
[ 0.0000e+00, 2.9330e+03, -7.3009e+00],
[ 1.0000e+00, 2.1610e+03, -7.0754e+00],
[ 1.0000e+00, 6.8000e+01, -7.2259e+00],
[ 1.0000e+00, 1.0620e+03, -7.2920e+00],
[ 1.0000e+00, 2.9330e+03, -7.3009e+00]],
[[ 0.0000e+00, 4.4070e+03, -7.1947e+00],
[ 0.0000e+00, 3.5600e+02, -7.2958e+00],
[ 0.0000e+00, 3.0300e+02, -7.3232e+00],
[ 0.0000e+00, 1.2910e+03, -7.3615e+00],
[ 1.0000e+00, 4.4070e+03, -7.1947e+00],
[ 1.0000e+00, 3.5600e+02, -7.2958e+00],
[ 1.0000e+00, 3.0300e+02, -7.3232e+00],
[ 1.0000e+00, 1.2910e+03, -7.3615e+00]]])
of shape
torch.Size([3, 8, 3])
and I would like to find the topk(k=4) elements across dim1, where the value to sort by is dim2 (the negative values). The resulting tensor shape should then be:
torch.Size([3, 4, 3])
I know how to do topk for a single tensor, but how do I do this for several batches at once?