Hey Bro, Maybe your code comes from
  
      github.com
  
  
    bearpaw/pytorch-classification/blob/cc9106d598ff1fe375cc030873ceacfea0499d77/utils/eval.py 5
from __future__ import print_function, absolute_import

__all__ = ['accuracy']

def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res


  
  
    
    
  
  


See Here,
  
      stats.stackexchange.com
  
  
      
    
  

  What is the definition of Top-n accuracy? 6



  machine-learning



  
  answered by
  
    rocksyne
  
  on 05:00AM - 03 Mar 18 UTC


  
  
    
    
  
  



If we take the top-3 accuracy for this, the correct class only needs to be in the top three predicted classes to count.

Assume that you have 64 samples, it should be
output = torch.randn(64, 134)
target = torch.randn(64)