fn
binary_cross_entropy_with_logits
→Tensorbinary_cross_entropy_with_logits(x: Tensor, target: Tensor, weight: Tensor | None = None, pos_weight: Tensor | None = None, reduction: str = '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
xTensorRaw logits (un-bounded reals), any shape.
targetTensorTarget probabilities (typically binary), same shape as
x.weightTensor or None= NoneElement-wise rescaling factor.
pos_weightTensor or None= NonePer-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.reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar or full-shape per reduction.
Notes
The numerically stable base form is
equivalent to
but free of overflow. 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.3567...)