original_docstring: Computes the precision@k for the specified values of k
docstring: Computes the precision@k for the specified values of k
docstring_tokens: Computes the precision@k for the specified values of k
code: def topk_acc(output, target, topk=(1, 3)):
      """Computes the precision@k for the specified values of k"""
      maxk = max(topk)
  
      _, 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*100.0/len(target.view(-1)))
          # res.append(correct_k.mul_(100.0 / batch_size))
      return torch.tensor(res)
code_tokens: def topk_acc ( output , target , topk = ( 1 , 3 ) ) : maxk = max ( topk ) _ , 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 * 100.0 / len ( target . view ( - 1 ) ) ) return torch . tensor ( res )
short_docstring: Computes the precision@k for the specified values of k
short_docstring_tokens: Computes the precision@k for the specified values of k
comment: """Computes the precision@k for the specified values of k"""$SEP$# res.append(correct_k.mul_(100.0 / batch_size))