I have a pytorch tensor as output language model (GTP2) and I use argmax to get index with the highest probability.

predicted_index = torch.argmax(predictions_2[0, -1, :]).item()

But I need all the indexes sorted by probability.

How do I do this in pytorch?

#####################################################

print (“predicted_index:”,predicted_index)

print (“predictions_2[0, -1, :]:”,predictions_2[0, -1, :])

I get:

predicted_index: 484

predictions_2[0, -1, :] field is: tensor([-122.9283, -124.4627, -128.4069, …, -131.5974, -128.7110,

-125.7269], device=‘cuda:0’)