import torch


def dice_coef_loss(pred, target, smoothing=1.0):
    """Dice coefficient loss

    This is actually -log Dice

    Args:
        pred: Array for the model predictions
        target: Array for the model target (ground truth labels)
        smoothing (float): Laplace smoothing factor (Default = 1.0)
    """
    intersection = (pred * target).sum(dim=(1, 2, 3))
    union = (pred + target).sum(dim=(1, 2, 3))

    term1 = -torch.log(2 * intersection + smoothing)
    term2 = torch.log(union + smoothing)

    return term1.mean() + term2.mean()