1top and 5top errors in Pytorch

EDIT: This is likely not what you’re looking for. This is a top-k function in PyTorch to get the highest value of a Tensor (including a loss Tensor), but likely you’re looking for the meaning of “top1” and “top5” accuracy like in ImageNet. See my reply below.

Hello, have a look at @InnovArul solution to my similar question here.

You would have to slightly adapt it, but it’s the same basic approach outlined under “Ignoring top-k losses”:

import torch
loss = torch.rand(6, requires_grad=True)
k = 3
sorted_loss, indices = torch.sort(loss)
top_k_losses = sorted_loss[-k:]  # this is how you identify the top losses

Depending on your specific use case, you can also look at torch.topk.

1 Like