1top and 5top errors in Pytorch

Hi all, How can I calculate the k-top error in Pytorch? any help would be appreciated.

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

Hi, thank you for your response. So, is the top1 error, the largest error value?

Hi, thank you for your response. So, is the top1 error, the largest error value?

Yes, should be.

1 Like

What about top5? For it there are 5 values but in the technical report or research paper one value as top5 error is reported. Is it the avarage of the five values?

Hmm, I think your original question was motivated by something different than the highest loss.

Likely what you’re looking for with top1 / top5 is this instead:

  • top 1 means whether the highest probability model prediction matches the label
  • top 5 means that one of the 5 highest probability predictions match the label

See more here.

1 Like