PyTorchClassificationLosses Module¶
Implements loss functions for classification tasks in PyTorch.
- class HMB.PyTorchClassificationLosses.CrossEntropyLossWrapper(classWeight=None, reductionMode='mean')[source]¶
Bases:
ModuleThin 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.
- class HMB.PyTorchClassificationLosses.LabelSmoothingCrossEntropy(labelSmoothing=0.1, reductionMode='mean')[source]¶
Bases:
ModuleCross entropy with label smoothing.
The loss is computed on raw logits for numerical stability.
- Parameters:
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class HMB.PyTorchClassificationLosses.BinaryFocalLoss(alpha=0.25, gamma=2.0, reductionMode='mean')[source]¶
Bases:
ModuleFocal loss for binary classification (uses logits for numerical stability).
\[\text{FL}(p_t) = -\alpha (1 - p_t)^{\gamma} \log(p_t)\]- Parameters:
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class HMB.PyTorchClassificationLosses.FocalLoss(gamma=2.0, alpha=None, reductionMode='mean')[source]¶
Bases:
ModuleMulti-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.
- class HMB.PyTorchClassificationLosses.FocalLossAlt(gamma=2.0, weight=None, reduction='mean')[source]¶
Bases:
ModuleFocal 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:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.