def get_accuracy(loss_string, output, targets, batch_size, threshold=0.5):
    """
    Helper function to calculate the accuracy of a given batch
    :param loss_string: loss function currently used
    :param output: the model prediction
    :param targets: target output
    :param batch_size: batch size
    :param threshold: decision threshold for binary classification
    :return: the accuracy of current batch
    """
    if loss_string == "crossentropy":
        max_index = output.max(dim=1)[1]
        correct = (max_index == targets).sum()
        accuracy = int(correct.data) / len(targets)

    elif loss_string == "binarycrossentropy":
        binary_output = output > threshold
        correct = sum((binary_output.float() == targets).data.cpu().numpy()[0])
        accuracy = correct / batch_size

    else:
        raise ValueError("Accuracy metrics not supported to current network")

    return accuracy