binary_cross_entropy_with_logits(x: Tensor, target: Tensor, weight: Tensor | None = None, pos_weight: Tensor | None = None, reduction: Reduction = 'mean')Binary cross-entropy from raw logits (numerically stable).
Mathematically equivalent to
binary_cross_entropy(sigmoid(x), target) but evaluated in a
log-sum-exp-style form that avoids overflow/underflow when
|x| is large. This is the preferred binary classification
loss for training — composing a separate sigmoid with BCE risks
catastrophic cancellation in for
large positive logits.
Parameters
Raw logits (un-bounded reals), any shape.
targetTensorTarget probabilities (typically binary), same shape as
x —
another shape raises ValueError rather than being broadcast.Element-wise rescaling factor that broadcasts to
x's shape;
one that does not, or that would enlarge it, raises
ValueError.Per-class weight applied to the positive term only —
useful for highly-imbalanced binary tasks, where setting
pos_weight = n_neg / n_pos recovers the prevalence-
balanced gradient. It broadcasts to x's shape, as
weight does.reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or full-shape per reduction.
Notes
The numerically stable form is
equivalent to
but free of overflow, and smooth at (the
spelling has the same value
but a kink in each piece there). With pos_weight:
Gradient w.r.t. x is the clean
(modulo weighting) — the canonical reason this form is used in
practice instead of the explicit sigmoid + BCE composition.
Examples
>>> import lucid
>>> from lucid.nn.functional import binary_cross_entropy_with_logits
>>> logits = lucid.tensor([2.0, -1.0, 0.5])
>>> target = lucid.tensor([1.0, 0.0, 1.0])
>>> binary_cross_entropy_with_logits(logits, target)
tensor(0.3048)