mindspore.nn.BCEWithLogitsLoss

class mindspore.nn.BCEWithLogitsLoss(reduction='mean', weight=None, pos_weight=None)[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} * ln(p_{ij}) + (1 - Y_{ij})ln(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’.

  • weight (Tensor, optional) – A rescaling weight applied to the loss of each batch element. If not None, it must can be broadcast to a tensor with shape of predict, data type must be float16 or float32. Default: None.

  • pos_weight (Tensor, optional) – A weight of positive examples. Must be a vector with length equal to the number of classes. If not None, it must can be broadcast to a tensor with shape of predict, data type must be float16 or float32. Default: None.

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

  • labels (Tensor) - Ground truth label. Has the same data type and shape with logits.

Outputs:

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

Raises
  • TypeError – If data type of logits or labels is neither float16 nor float32.

  • TypeError – If weight or pos_weight is Parameter.

  • TypeError – If data type of weight or pos_weight is neither float16 nor float32.

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

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

Supported Platforms:

Ascend

Examples

>>> logits = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
>>> labels = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32))
>>> loss = nn.BCEWithLogitsLoss()
>>> output = loss(logits, labels)
>>> print(output)
0.3463612