NXTfusion.NXLosses module

class NXTfusion.NXLosses.FocalLoss(alpha=1, gamma=2, logits=True, reduction='sum')[source]

Bases: torch.nn.modules.module.Module

Implementation of the FocalLoss, which is used as loss for heavily unbalanced binary predictions. It is bult as a convetional pytorch module.

__init__(alpha=1, gamma=2, logits=True, reduction='sum')[source]

Constructor for the FocalLoss.

Parameters
  • alpha (int) – Parameter of the loss

  • gamma (int) – Parameter of the loss

  • logits (bool) – Uses logits if True

class NXTfusion.NXLosses.LossWrapper(loss: torch.nn.modules.module.Module, type: str, ignore_index: int)[source]

Bases: torch.nn.modules.module.Module

Class that wraps any pytorch loss allowing for ignore index. In the Matrix Factorization context it may be useful to define a value indicating missing values even when performing a regression, for example if the goal is to predict a sparsely observed real-valued matrix.

__call__(input, target)[source]

Function defining the forward pass for this wrapper. It implements the ignore_index filtering and then it calls the actual self.loss on the remaining values.

Parameters
  • input (t.nn.Tensor) – Pytorch tensor containing the predicted values

  • target (t.nn.Tensor) – Pytorch tensor containing the target values

Returns

Return type

Loss score computed only for the target values that are not equal to self.ignore_index.

__init__(loss: torch.nn.modules.module.Module, type: str, ignore_index: int)[source]

Constructor for the wrapper.

Parameters
  • loss (t.nn.Module) – The argument can be any pytorch compatible loss functioni

  • type – Specifies wheter is a regression or a binay prediction (deprecate?)

  • ignore_index (int) – Specifies which value should be ignored while computing the loss, to allow for the presence of missing values in the matrix/relation.