import torch


def focal_loss(logit: torch.Tensor, gamma, alpha=1, eps=1e-5):
    """
    focal loss: /alpha * (1 - logit)^{gamma} * log^{logit}
    :param logit: logit value 0 ~ 1
    :param gamma:
    :param alpha:
    :param eps: a tiny value prevent log(0)
    :return:
    """
    return -alpha * torch.pow(1 - logit, gamma) * torch.log(logit + eps)