import torch


def jaccard_score(outputs, labels, smooth=1e-5):
    """Compute the Jaccard/IoU score. If a division by zero occurs,
    returns a score of 0.
    Args:
        outputs (Tensor): The model's predictions
        labels (Tensor): The target labels (aka ground truth predictions)
        smooth (float/int): A smoothness factor
    Returns:
        Jaccard score (Tensor)
    """
    outputs, labels = outputs.float(), labels.float()
    intersect = torch.dot(outputs.contiguous().view(-1),
                          labels.contiguous().view(-1))
    union = torch.add(torch.sum(outputs), torch.sum(labels))
    jaccard = (intersect + smooth) / (union + smooth)
    return jaccard if not torch.isnan(jaccard) else torch.Tensor([0.0])