matcha.nn.losses
Loss functions for regression, classification, and multitask learning.
Attributes
Classes
Focal loss implementation using binary cross entropy with logits. |
|
Polynomial expansion of the binary cross entropy loss, which can lead to better |
|
Implementation of a customizable multitask loss. The same loss is |
|
Implementation of a customizable multi-loss function that supports |
|
Loss function for handling bounded regression |
|
Mean squared error loss (wraps |
|
Mean absolute error loss (wraps |
|
Huber loss (wraps |
|
Smooth L1 loss (wraps |
|
|
|
|
|
|
|
|
|
Binary cross-entropy with logits (wraps |
|
Cross-entropy loss (wraps |
|
Weighted binary cross entropy loss with logits. |
|
Implementation of GradNorm for adaptive loss balancing in multitask learning. |
Module Contents
- matcha.nn.losses.LossRegistry
- class matcha.nn.losses.BCEFocalLoss(gamma=2, alpha=None, reduction='mean')[source]
Bases:
torch.nn.ModuleFocal loss implementation using binary cross entropy with logits. Suitable for binary classification with class imbalance. Reference: https://arxiv.org/abs/1708.02002
- gamma = 2
- alpha = None
- reduction = 'mean'
- eps = 1e-07
- forward(inputs, targets) torch.Tensor[source]
- Parameters:
inputs (torch.Tensor) – Raw logits.
targets (torch.Tensor) – Binary target labels.
- Returns:
Focal loss value.
- Return type:
- class matcha.nn.losses.Poly1BCELoss(epsilon: float = 1.0, reduction: str = 'mean')[source]
Bases:
torch.nn.ModulePolynomial expansion of the binary cross entropy loss, which can lead to better classification performance if epsilon is tuned. Suitable for binary classification. Reference: https://arxiv.org/abs/2204.12511
- epsilon = 1.0
- reduction = 'mean'
- eps = 1e-07
- forward(inputs, targets) torch.Tensor[source]
- Parameters:
inputs (torch.Tensor) – Raw logits.
targets (torch.Tensor) – Binary target labels.
- Returns:
Poly1 BCE loss value.
- Return type:
- class matcha.nn.losses.MultitaskLoss(loss_fn: str = 'mse', loss_args: dict = {})[source]
Bases:
torch.nn.ModuleImplementation of a customizable multitask loss. The same loss is used for all tasks.
loss_fn is the name of the loss to broadcast, loss_args are its arguments.
- loss
- forward(outputs: torch.Tensor, targets: torch.Tensor) torch.Tensor[source]
- Parameters:
outputs (torch.Tensor) – Predictions of shape
(batch, num_tasks).targets (torch.Tensor) – Targets of shape
(batch, num_tasks); NaN marks missing.
- Returns:
Scalar loss averaged across valid entries and tasks.
- Return type:
- class matcha.nn.losses.MultiLoss(loss_configs: list)[source]
Bases:
torch.nn.ModuleImplementation of a customizable multi-loss function that supports dynamic weight scheduling during training.
Each loss configuration is a dictionary with: - loss_fn: name of the loss function - loss_args: arguments for the loss function - task_map: tuple/list indicating which columns this loss applies to (start, end) - init_w: initial weight at T=0 - final_w: final weight at T=end - T: total epochs to transition from init_w to final_w - warmup: epochs to keep init_w fixed before starting transition
- loss_configs
- losses
- forward(outputs: torch.Tensor, targets: torch.Tensor, T_current: int = 0) torch.Tensor[source]
Forward pass with dynamic weight scheduling.
- Parameters:
outputs (torch.Tensor) – Model predictions.
targets (torch.Tensor) – Ground truth targets; NaN marks missing entries.
T_current (int) – Current training epoch for weight scheduling.
- Returns:
Weighted total loss (scalar during training, tuple with log during eval).
- Return type:
- class matcha.nn.losses.BoundedLoss(loss_fn: str = 'mse', **kwargs)[source]
Bases:
torch.nn.ModuleLoss function for handling bounded regression
Allows the use of bound information on the readout (e.g. IC50 < x), so that the model is not penalized when it predicts e.g. y_pred < x. Implementation is based on: https://chemprop.readthedocs.io/en/latest/_modules/chemprop/nn/metrics.html#BoundedMixin
- loss
- forward(outputs: torch.Tensor, targets: torch.Tensor) torch.Tensor[source]
- Parameters:
outputs (torch.Tensor) – Predictions.
targets (torch.Tensor) – Targets with bound info in the last dimension. Shape
(batch, [num_tasks,] 2)where[..., 0]is the value and[..., 1]encodes the bound type (-1= less-than,1= greater-than).
- Returns:
Loss computed only on non-masked predictions.
- Return type:
- class matcha.nn.losses.MSELoss(size_average=None, reduce=None, reduction: str = 'mean')[source]
Bases:
torch.nn.MSELossMean squared error loss (wraps
torch.nn.MSELoss).
- class matcha.nn.losses.L1Loss(size_average=None, reduce=None, reduction: str = 'mean')[source]
Bases:
torch.nn.L1LossMean absolute error loss (wraps
torch.nn.L1Loss).
- class matcha.nn.losses.HuberLoss(reduction: str = 'mean', delta: float = 1.0)[source]
Bases:
torch.nn.HuberLossHuber loss (wraps
torch.nn.HuberLoss).
- class matcha.nn.losses.SmoothL1Loss(size_average=None, reduce=None, reduction: str = 'mean', beta: float = 1.0)[source]
Bases:
torch.nn.SmoothL1LossSmooth L1 loss (wraps
torch.nn.SmoothL1Loss).
- class matcha.nn.losses.BoundedMSELoss(**kwargs)[source]
Bases:
BoundedLossBoundedLosswith MSE as the inner loss.
- class matcha.nn.losses.BoundedMAELoss(**kwargs)[source]
Bases:
BoundedLossBoundedLosswith MAE as the inner loss.
- class matcha.nn.losses.BoundedHuberLoss(**kwargs)[source]
Bases:
BoundedLossBoundedLosswith Huber as the inner loss.
- class matcha.nn.losses.BoundedSmoothL1Loss(**kwargs)[source]
Bases:
BoundedLossBoundedLosswith Smooth L1 as the inner loss.
- class matcha.nn.losses.BCELoss(weight: torch.Tensor | None = None, size_average=None, reduce=None, reduction: str = 'mean', pos_weight: torch.Tensor | None = None)[source]
Bases:
torch.nn.BCEWithLogitsLossBinary cross-entropy with logits (wraps
torch.nn.BCEWithLogitsLoss).
- class matcha.nn.losses.CrossEntropyLoss(weight: torch.Tensor | None = None, size_average=None, ignore_index: int = -100, reduce=None, reduction: str = 'mean', label_smoothing: float = 0.0)[source]
Bases:
torch.nn.CrossEntropyLossCross-entropy loss (wraps
torch.nn.CrossEntropyLoss).
- class matcha.nn.losses.WeightedBCELoss(w1: float = 0.5, reduction: str = 'mean')[source]
Bases:
torch.nn.ModuleWeighted binary cross entropy loss with logits.
Applies per-class weights to handle class imbalance in binary classification. The user specifies the weight for the positive (minority) class; the weight for the negative class is computed so that
w0 + w1 = 1.- Parameters:
- w1 = 0.5
- w0 = 0.5
- reduction = 'mean'
- forward(inputs: torch.Tensor, targets: torch.Tensor) torch.Tensor[source]
- Parameters:
inputs (torch.Tensor) – Raw logits.
targets (torch.Tensor) – Binary target labels.
- Returns:
Weighted BCE loss value.
- Return type:
- class matcha.nn.losses.GradNormLoss(loss_fn: str = 'mse', loss_args: dict = {}, num_endpoints: int = 1, weight_lr: float = 0.025)[source]
Bases:
torch.nn.ModuleImplementation of GradNorm for adaptive loss balancing in multitask learning.
GradNorm automatically balances training by dynamically tuning gradient magnitudes. It adjusts task weights to ensure that all tasks train at similar rates. The weight updates are handled internally — no separate optimizer needed.
Reference: https://arxiv.org/abs/1711.02257
- Parameters:
Example:
loss_fn = GradNormLoss(loss_fn="mse", num_endpoints=3) # Training loop (no changes needed): loss = loss_fn(outputs, targets, shared_layer=model.backbone[-1]) optimizer.zero_grad() loss.backward() optimizer.step()
- ALPHA = 1.5
- loss
- num_endpoints = 1
- weight_lr = 0.025
- initial_losses = None
- forward(outputs: torch.Tensor, targets: torch.Tensor, shared_layer: torch.nn.Module = None) torch.Tensor[source]
Forward pass computing weighted multitask loss.
- Parameters:
outputs (torch.Tensor) – Predictions of shape
(batch_size, num_endpoints).targets (torch.Tensor) – Targets of shape
(batch_size, num_endpoints).shared_layer (torch.nn.Module or None) – The last shared layer of the network. Required during training for GradNorm weight updates.
- Returns:
Weighted sum of task losses.
- Return type: