fn
binary_cross_entropy
→Tensorbinary_cross_entropy(x: Tensor, target: Tensor, weight: Tensor | None = None, reduction: str = 'mean')Binary cross-entropy between predicted probabilities and targets.
The standard objective for binary classification and for
multi-label classification with independent class predictions.
Operates on probabilities in — typically the
output of a lucid.nn.functional.sigmoid. When working
with raw logits, prefer binary_cross_entropy_with_logits
for numerical stability.
Inputs are clamped to
with before the logarithm to
prevent -inf gradients at the boundaries.
Parameters
xTensorPredicted probabilities in , any shape.
targetTensorTarget probabilities (typically binary) of the same shape.
weightTensor or None= NoneElement-wise rescaling factor (broadcast-compatible with
x). Use to up-weight rare classes or hard examples.reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or full-shape per reduction.
Notes
Per-element loss:
Gradient w.r.t. x is ,
which diverges as or — the reason
the logits-form (which yields a clean
gradient) is preferred when training stability is critical.
Examples
>>> import lucid
>>> from lucid.nn.functional import binary_cross_entropy
>>> p = lucid.tensor([0.9, 0.2, 0.7])
>>> y = lucid.tensor([1.0, 0.0, 1.0])
>>> binary_cross_entropy(p, y)
Tensor(0.2284...)