def soft_tversky_score(
    output: torch.Tensor,
    target: torch.Tensor,
    alpha: float,
    beta: float,
    smooth: float = 0.0,
    eps: float = 1e-7,
    dims=None,
) -> torch.Tensor:
    """Tversky loss

    References:
        https://arxiv.org/pdf/2302.05666
        https://arxiv.org/pdf/2303.16296

    """
    assert output.size() == target.size()

    if dims is not None:
        output_sum = torch.sum(output, dim=dims)
        target_sum = torch.sum(target, dim=dims)
        difference = LA.vector_norm(output - target, ord=1, dim=dims)
    else:
        output_sum = torch.sum(output)
        target_sum = torch.sum(target)
        difference = LA.vector_norm(output - target, ord=1)

    intersection = (output_sum + target_sum - difference) / 2  # TP
    fp = output_sum - intersection
    fn = target_sum - intersection

    tversky_score = (intersection + smooth) / (
        intersection + alpha * fp + beta * fn + smooth
    ).clamp_min(eps)
    return tversky_score
Tversky loss

    References:
        https://arxiv.org/pdf/2302.05666
        https://arxiv.org/pdf/2303.16296

    
segmentation_models_pytorch/losses/_functional.py