mindspore.ops.BCEWithLogitsLoss

class mindspore.ops.BCEWithLogitsLoss(*args, **kwargs)[source]

Adds sigmoid activation function to input predict, and uses the given logits to compute binary cross entropy between the target and the output.

Sets input predict as X, input target as Y, output as L. Then,

\[p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}}\]
\[L_{ij} = -[Y_{ij} * log(p_{ij}) + (1 - Y_{ij})log(1 - p_{ij})]\]

Then,

\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]
Parameters

reduction (str) – Type of reduction to be applied to loss. The optional values are ‘mean’, ‘sum’, and ‘none’. If ‘none’, do not perform reduction. Default:’mean’.

Inputs:
  • predict (Tensor) - Input logits. Data type must be float16 or float32.

  • target (Tensor) - Ground truth label. Has the same shape with predict. Data type must be float16 or float32.

  • weight (Tensor) - A rescaling weight applied to the loss of each batch element. It must can be broadcast to a tensor with shape of predict. Data type must be float16 or float32.

  • pos_weight (Tensor) - A weight of positive examples. Must be a vector with length equal to the number of classes. It must can be broadcast to a tensor with shape of predict. Data type must be float16 or float32.

Outputs:

Scalar. If reduction is ‘none’, it’s a tensor with the same shape and type as input predict.

Raises
  • TypeError – If data type of any input is neither float16 nor float32.

  • ValueError – If weight or pos_weight can not be broadcast to a tensor with shape of predict.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend

Examples

>>> predict = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
>>> target = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32))
>>> weight = Tensor(np.array([1.0, 1.0, 1.0]).astype(np.float32))
>>> pos_weight = Tensor(np.array([1.0, 1.0, 1.0]).astype(np.float32))
>>> loss = ops.BCEWithLogitsLoss()
>>> output = loss(predict, target, weight, pos_weight)
>>> print(output)
0.3463612