fn

binary_cross_entropy

Tensor
binary_cross_entropy(x: Tensor, target: Tensor, weight: Tensor | None = None, reduction: str = 'mean')
source

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 (0,1)(0, 1) — 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 [ε,1ε][\varepsilon, 1 - \varepsilon] with ε=1012\varepsilon = 10^{-12} before the logarithm to prevent -inf gradients at the boundaries.

Parameters

xTensor
Predicted probabilities in (0,1)(0, 1), any shape.
targetTensor
Target probabilities (typically binary) of the same shape.
weightTensor or None= None
Element-wise rescaling factor (broadcast-compatible with x). Use to up-weight rare classes or hard examples.
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or full-shape per reduction.

Notes

Per-element loss:

Li=(yilogpi+(1yi)log(1pi))L_i = -\big(y_i\,\log p_i + (1 - y_i)\,\log(1 - p_i)\big)

Gradient w.r.t. x is (piyi)/(pi(1pi))(p_i - y_i) / (p_i(1 - p_i)), which diverges as pi0p_i \to 0 or 11 — the reason the logits-form (which yields a clean σ(x)y\sigma(x) - y 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...)