class

BCELoss

extendsModule
BCELoss(weight: Tensor | None = None, reduction: str = 'mean')
source

Binary cross-entropy loss.

Measures the element-wise binary cross-entropy between predictions x (probabilities in (0,1)(0, 1)) and binary targets y (values in {0,1}\{0, 1\}):

(x,y)=[ylogx+(1y)log(1x)]\ell(x, y) = -\bigl[y \log x + (1 - y) \log(1 - x)\bigr]

The input must be valid probabilities, i.e. produced by a Sigmoid activation. Passing raw logits directly leads to undefined behaviour; use BCEWithLogitsLoss instead for better numerical stability when starting from logits.

Parameters

weightTensor= None
Element-wise weight tensor that is broadcast over the input and target tensors. Must be broadcastable to their shape.
reductionstr= 'mean'
'none' | 'mean' (default) | 'sum'.

Attributes

weightTensor or None
Optional element-wise weighting.
reductionstr
The reduction mode.

Notes

  • Input x : ()(*) — probabilities in (0,1)(0, 1).
  • Target y : ()(*) — binary labels in {0,1}\{0, 1\}.
  • Output : scalar for 'mean' / 'sum'; ()(*) for 'none'.
  • Values of x outside (0,1)(0, 1) will produce NaN or inf losses due to the logarithm.
  • For logits (pre-sigmoid values), prefer BCEWithLogitsLoss which uses the identity log(1+ex)=max(x,0)+log(1+ex)\log(1 + e^{-x}) = \max(x,0) + \log(1 + e^{-|x|}).

Examples

Basic binary classification:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.BCELoss()
>>> probs   = lucid.tensor([0.9, 0.1, 0.8, 0.3])
>>> targets = lucid.tensor([1.0, 0.0, 1.0, 0.0])
>>> loss = criterion(probs, targets)
With element-wise sample weighting:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.BCELoss(weight=lucid.tensor([2.0, 1.0, 2.0, 1.0]))
>>> probs   = lucid.tensor([0.7, 0.2, 0.6, 0.4])
>>> targets = lucid.tensor([1.0, 0.0, 1.0, 0.0])
>>> loss = criterion(probs, targets)

Methods (3)

dunder

__init__

None
__init__(weight: Tensor | None = None, reduction: str = 'mean')
source

Initialise the BCELoss module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor, target: Tensor)
source

Compute the loss between predictions and targets.

Parameters

xTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.