Obtain second-best prediction for image classification?

I’ve just finished lesson 2. Does anyone know if it’s possible to obtain the second-best prediction, as well as the first, for an image classifier? If my model is making a prediction with e.g. <60% confidence, it might also be useful to show the next-best guess. I looked at the learner.predict documentation, but couldn’t parse the source code.

Assuming a simple image classification problem, you could do something like:

_, _, probs = learn.predict(img)
_, ind = torch.topk(probs, k=2)
cat = dls.vocab[ind[1]]

The first line calculates the probabilities of img belonging to each class, torch.topk returns the k largest values in probs (in a descending order) alongside their indices, and we finally extract the name of the category.

Good luck!