Focal Loss

class liqfit.losses.FocalLoss

(ignore_index: int = -100, alpha: float = 0.5, gamma: float = 2.0,
reduction: str = "mean")

Parameters:

  • ignore_index (int): Index that will be ignored while calculating the loss.

  • alpha (float, optional): Weighting factor between 0 and 1. (Defaults to 0.5).

  • gamma (float, optional): Focusing parameter gamma >= 0. (Defaults to 2.0).

  • reduction (str): Reduction method.

Using Focal Loss

from liqfit.losses import FocalLoss
import torch
# FocalLoss in liqfit supports `ignore_index`
# parameter which could be used in token classification

x = torch.randn((1, 10, 20))
y = torch.randint(0, 10, (1, 10))
focal_loss = FocalLoss()
loss = focal_loss(x.view(-1, x.shape[-1]), y.view(-1))

Last updated