import torch


def all_or_none_accuracy(preds, targets, dim=-1):
    """ Gets the accuracy of the predicted sequence.

    :param preds: model predictions
    :param targets: the true targets
    :param dim: dimension to operate over
    :returns: scalar value for all-or-none accuracy
    :rtype: float32

    """
    preds_max = preds.data.max(dim=dim)[1]  # get the index of the max log-probability
    assert targets.shape == preds_max.shape, \
        "target[{}] shape does not match preds[{}]".format(targets.shape, preds_max.shape)
    targ = targets.data
    return torch.mean(preds_max.eq(targ).cpu().all(dim=dim).type(torch.float32))