WeightedBCELoss

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

Bases: torch.nn.modules.module.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

1

https://github.com/pytorch/pytorch/issues/5660#issuecomment-403770305

forward(input: torch.Tensor, target: torch.Tensor) torch.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