(output, target, topk=None, ignore_index=None)
return res
Computes the precision@k for the specified values of k
Computes the precision
Computes the precision
def accuracy(output, target, topk=None, ignore_index=None):
    """Computes the precision@k for the specified values of k"""
    topk = topk or [1, 5]

    if ignore_index is not None:
        target_mask = (target != ignore_index)
        target = target[target_mask]
        output_mask = target_mask.unsqueeze(1)
        output_mask = output_mask.expand_as(output)
        output = output[output_mask]
        output = output.view(-1, output_mask.size(1))

    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, keepdim=True)
        res.append(correct_k.mul_(100.0 / batch_size)[0])
    return res
def accuracy ( output , target , topk = None , ignore_index = None ) : topk = topk or [ 1 , 5 ] if ignore_index is not None : target_mask = ( target != ignore_index ) target = target [ target_mask ] output_mask = target_mask . unsqueeze ( 1 ) output_mask = output_mask . expand_as ( output ) output = output [ output_mask ] output = output . view ( - 1 , output_mask . size ( 1 ) ) 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 , keepdim = True ) res . append ( correct_k . mul_ ( 100.0 / batch_size ) [ 0 ] ) return res