neuro_morpho.model.loss ======================= .. py:module:: neuro_morpho.model.loss .. autoapi-nested-parse:: Loss functions for training models. Attributes ---------- .. autoapisummary:: neuro_morpho.model.loss.NAME_LOSS neuro_morpho.model.loss.PRED neuro_morpho.model.loss.TARGET neuro_morpho.model.loss.LOSS_FN Classes ------- .. autoapisummary:: neuro_morpho.model.loss.WeightedFocalLoss neuro_morpho.model.loss.SigmoidDiceLoss neuro_morpho.model.loss.WeightedMap neuro_morpho.model.loss.CombinedLoss Module Contents --------------- .. py:data:: NAME_LOSS .. py:data:: PRED .. py:data:: TARGET .. py:data:: LOSS_FN .. py:class:: WeightedFocalLoss(alpha: float = 0.25, gamma: float = 2, reduction: str = 'mean') Bases: :py:obj:`torch.nn.Module` Weighted version of Focal Loss. This loss is designed to address class imbalance by down-weighting easy examples and focusing on hard examples. See: https://arxiv.org/pdf/1708.02002 :param alpha: Weighting factor in range (0, 1) to balance positive vs negative examples. :type alpha: float :param gamma: Focusing parameter to reduce the relative loss for well-classified examples. :type gamma: float :param reduction: Specifies the reduction to apply to the output: 'none', 'mean', 'sum'. :type reduction: str .. py:attribute:: alpha :value: 0.25 .. py:attribute:: gamma :value: 2 .. py:attribute:: reduction :value: 'mean' .. py:method:: forward(inputs: torch.Tensor, targets: torch.Tensor) -> tuple[str, torch.Tensor] Calculate the weighted focal loss. .. py:class:: SigmoidDiceLoss(smooth=1.0) Bases: :py:obj:`torch.nn.Module` Dice Loss for image segmentation for binary classification. This loss is commonly used for image segmentation tasks. It measures the overlap between the predicted and target segmentation. .. py:attribute:: smooth :value: 1.0 .. py:method:: forward(preds: torch.Tensor | list[torch.Tensor], targets: torch.Tensor | list[torch.Tensor]) -> tuple[str, torch.Tensor] Calculate the dice loss. .. py:class:: WeightedMap(loss_fn: torch.nn.Module, coefs: list[float]) Bases: :py:obj:`torch.nn.Module` Weighted Map Loss. This loss applies a weighted sum of a given loss function to a list of predictions and targets. .. py:attribute:: coefs .. py:attribute:: loss_fn .. py:method:: forward(pred: list[torch.Tensor], lbl: list[torch.Tensor]) -> tuple[str, torch.Tensor] Calculate the weighted map loss. .. py:class:: CombinedLoss(weights: list[float], losses: list[torch.nn.Module]) Bases: :py:obj:`torch.nn.Module` Combined Loss Function. This loss function combines multiple loss functions with given weights. .. py:attribute:: weights .. py:attribute:: losses .. py:method:: forward(pred: torch.Tensor, lbl: torch.Tensor) -> list[tuple[str, torch.Tensor]] Forward pass to compute the combined loss. :param pred: The predicted tensor. :param lbl: The target/label tensor. :returns: A list of tuples, where each tuple contains the name of the loss and the weighted loss value. :rtype: list[tuple[str, torch.Tensor]]