PyTorchClassificationLosses Module

Implements loss functions for classification tasks in PyTorch.

class HMB.PyTorchClassificationLosses.CrossEntropyLossWrapper(classWeight=None, reductionMode='mean')[source]

Bases: Module

Thin wrapper around torch.nn.CrossEntropyLoss to keep a consistent API.

Parameters:
  • weight (Tensor, optional) – a manual rescaling weight given to each class.

  • reduction (str) – “mean” (default), “sum” or “none”.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(inputTensor, targetTensor)[source]

Compute cross-entropy loss for multi-class classification.

Parameters:
  • inputTensor (Tensor) – logits of shape (N, C).

  • targetTensor (Tensor) – long tensor of shape (N,) with class indices.

Returns:

computed loss.

Return type:

torch.Tensor

class HMB.PyTorchClassificationLosses.LabelSmoothingCrossEntropy(labelSmoothing=0.1, reductionMode='mean')[source]

Bases: Module

Cross entropy with label smoothing.

The loss is computed on raw logits for numerical stability.

Parameters:
  • smoothing (float) – label smoothing factor in [0, 1). Typical values 0.0 - 0.2.

  • reduction (str) – “mean”, “sum” or “none”.

  • labelSmoothing (float)

  • reductionMode (str)

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(inputTensor, targetTensor)[source]

Compute label-smoothed cross-entropy loss.

Parameters:
  • inputTensor (Tensor) – logits of shape (N, C).

  • targetTensor (Tensor) – long tensor of shape (N,) with class indices.

Returns:

computed loss.

Return type:

torch.Tensor

class HMB.PyTorchClassificationLosses.BinaryFocalLoss(alpha=0.25, gamma=2.0, reductionMode='mean')[source]

Bases: Module

Focal loss for binary classification (uses logits for numerical stability).

\[\text{FL}(p_t) = -\alpha (1 - p_t)^{\gamma} \log(p_t)\]
Parameters:
  • alpha (float) – balancing factor for the positive class (default 0.25).

  • gamma (float) – focusing parameter (default 2.0).

  • reduction (str) – “mean”, “sum” or “none”.

  • reductionMode (str)

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(inputTensor, targetTensor)[source]

Compute binary focal loss.

Parameters:
  • inputTensor (Tensor) – logits of shape (N,).

  • targetTensor (Tensor) – float tensor of shape (N,) with binary labels (0 or 1).

Returns:

computed loss.

Return type:

torch.Tensor

class HMB.PyTorchClassificationLosses.FocalLoss(gamma=2.0, alpha=None, reductionMode='mean')[source]

Bases: Module

Multi-class focal loss (works with logits).

Parameters:
  • gamma (float) – focusing parameter.

  • alpha (None|float|list|Tensor) – balancing factor. If None no class weighting is used. If float is provided it is assumed to be the weight for the class 1 in binary case. For multi-class you can pass a list/torch.Tensor of length C with class weights.

  • reduction (str) – “mean”, “sum” or “none”.

  • reductionMode (str)

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(inputTensor, targetTensor)[source]

Compute multi-class focal loss.

Parameters:
  • inputTensor (Tensor) – logits of shape (N, C).

  • targetTensor (Tensor) – long tensor of shape (N,) with class indices.

Returns:

computed loss.

Return type:

torch.Tensor

class HMB.PyTorchClassificationLosses.FocalLossAlt(gamma=2.0, weight=None, reduction='mean')[source]

Bases: Module

Focal loss for handling class imbalance in binary/multi-class classification.

Down-weights easy examples and focuses training on hard negatives. Formula: FL(p_t) = -alpha * (1 - p_t)^gamma * log(p_t)

Parameters:
  • gamma (float) – Focusing parameter that down-weights easy examples (typical: 2.0).

  • weight (torch.Tensor or None) – Optional per-class weights for imbalance handling.

  • reduction (str) – Reduction method: “mean”, “sum”, or “none”.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(inputs, targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:
Return type:

Tensor