def accuracy(output, labels):
    """ Calculate accuracy.
    
    Args:
        output: output of a model.
        labels: ground truth labels.
        
    Returns:
        float: accuracy score.
    """
    if any([s > 1 for s in labels.shape]):
        s = output.round() + labels
        numerator = (s == 2).sum().double() + (s == 0).sum().double()
        denominator = (s == 1).sum()
        # print("output + labels: {}".format(s),
        #       "numerator: {}".format(numerator.item()),
        #       "denominator: {}".format(denominator.item()))
        if denominator == 0:
            return 0.0
        return (numerator / denominator).item()
    else:
        return (output.round() == labels).sum().item() / output.numel()