import torch


def accuracy(output, y, k=1):
    """Computes the precision@k for the specified values of k"""
    # Rehape to [N, 1]
    target = y.view(-1, 1)

    _, pred = torch.topk(output, k, dim=1, largest=True, sorted=True)
    correct = torch.eq(pred, target)

    return torch.sum(correct).float() / y.size(0)