import torch


def smooth_dice_loss(pred: torch.Tensor, target: torch.Tensor,
                     smooth: float=1., eps: float=1e-6) -> torch.Tensor:
    """
    Smoothed dice loss.

    :param pred: (torch.Tensor) predictions, logits
    :param target: (torch.Tensor) target, logits or binrary
    :param smooth: (float) smoothing value
    :param eps: (eps) epsilon for numerical stability

    :returns dice_loss: (torch.Tensor) the dice loss
    """

    pred = torch.sigmoid(pred)
    target = (target > 0).float()

    intersection = (pred.reshape(-1) * target.reshape(-1)).sum()

    return 1 - ((2. * intersection + smooth) / (pred.sum() + target.sum() + smooth + eps))