Autograd node for sigmoid + binary cross-entropy fused for numerical stability.
Equivalent to BCELoss(Sigmoid(x), y) but evaluated using the
log-sum-exp identity
so that no intermediate sigmoid value is required:
When pos_weight is supplied, positive examples are re-weighted as
where is the sigmoid; the closed form above is rearranged
internally to remain numerically stable.
Math
with an additional positive-class scale applied to the
term when pos_weight is non-trivial.
Attributes
schema_v1OpSchema"bce_with_logits", AmpPolicy::ForceFP32.reduction_Reductionorig_shape_ShapeNotes
Always prefer this op over a manual Sigmoid → BCELoss chain: the
fused form preserves gradient magnitude in the saturated regions of the
sigmoid where the explicit decomposition produces vanishing gradients.
Static methods
1forward
→TensorImplPtrint forward(const int & input, const int & target, const int & weight, const int & pos_weight, Reduction reduction)Compute BCE-with-logits loss with autograd wiring.
Parameters
inputTensorImplPtrtargetTensorImplPtrinput.weightTensorImplPtrinput.shape().pos_weightTensorImplPtrinput. Pass a ones tensor if unused.reductionReductionMean, Sum, or None.Returns
TensorImplPtrReduced or per-element loss tensor.