WeightedBCELoss

class torch_ecg.models.loss.WeightedBCELoss(pos_weight: Tensor, weight: Tensor | None = None, PosWeightIsDynamic: bool = False, WeightIsDynamic: bool = False, size_average: bool = True, reduce: bool = True)[source]

Bases: Module

Weighted Binary Cross Entropy Loss class.

This implementation is based on [1].

Parameters:
  • pos_weight (torch.Tensor) – Weight for postive sample.

  • weight (torch.Tensor, optional) – Weight for each class, of size [1, C].

  • PosWeightIsDynamic (bool, default False) – If True, the pos_weight is computed on each batch. If pos_weight is None, then it remains None.

  • WeightIsDynamic (bool, default False) – If True, the weight is computed on each batch. If weight is None, then it remains None.

  • size_average (bool, default True) – If True, the losses are averaged over each loss element in the batch. Valid only if reduce is True.

  • reduce (bool, default True) – If True, the losses are averaged or summed over observations for each minibatch.

References

forward(input: Tensor, target: Tensor) Tensor[source]

Forward pass.

Parameters:
  • input (torch.Tensor) – The predicted probability tensor, of shape (batch_size, ..., n_classes).

  • target (torch.Tensor) – The target tensor, of shape (batch_size, ..., n_classes).

Returns:

loss – The weighted binary cross entropy loss.

Return type:

torch.Tensor