Skip to content
class

BCELossBackward

BCELossBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for binary cross-entropy on probability inputs.

Computes the element-wise loss

ℓ(y^,y)=−w [ ylog⁡(y^+ε)+(1−y)log⁡(1−y^+ε) ] \ell(\hat{y}, y) = -w \,\bigl[\, y \log(\hat{y} + \varepsilon) + (1 - y) \log(1 - \hat{y} + \varepsilon) \,\bigr]

where y^∈(0,1)\hat{y} \in (0, 1) is a probability (e.g. the output of a sigmoid) and y∈{0,1}y \in \{0, 1\} is a binary target. The clamp eps_ is added inside the logarithm to protect against log⁡0\log 0. Both input, target and the (optional) per-element weight are saved so that the backward can emit ∇y^=w⋅(−y/y^+(1−y)/(1−y^))⋅g/N\nabla_{\hat y} = w \cdot \bigl(-y / \hat y + (1 - y) / (1 - \hat y)\bigr) \cdot g / N (for Mean; analogous for the other modes).

Math

ℓ(y^,y)=−w [ylog⁡y^+(1−y)log⁡(1−y^)] \ell(\hat{y}, y) = -w \,\bigl[y \log \hat{y} + (1 - y) \log(1 - \hat{y})\bigr]

Attributes

schema_v1OpSchema
"bce_loss", AmpPolicy::ForceFP32.
reduction_Reduction
Reduction mode.
eps_double
Numerical clamp added to log arguments. Default 1e-7.
orig_shape_Shape
Element-wise shape before reduction.

Notes

Inputs must be valid probabilities; values outside (0,1)(0, 1) produce NaN / inf losses. For raw logits use BCEWithLogitsBackward, which is numerically more stable.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & input, const int & target, const int & weight, Reduction reduction, double eps)

Compute BCE loss with autograd wiring.

Parameters

inputTensorImplPtr
Probabilities in (0,1)(0, 1); arbitrary shape.
targetTensorImplPtr
Binary labels in {0,1}\{0, 1\}; same shape as input.
weightTensorImplPtr
Per-element weighting tensor broadcastable to input.shape(). Pass a ones tensor if no weighting is required.
reductionReduction
Mean, Sum, or None.
epsdouble
Clamp inside the logarithm; values around 1e-7 recommended.

Returns

TensorImplPtr

Reduced or per-element loss tensor.

Methods

1
fn

apply

int apply(int grad_out)

Backward pass: gradients w.r.t. input, target, and weight.