class mindspore.ops.BinaryCrossEntropy(reduction='mean')[source]

Computes the binary cross entropy between the logits and the labels.

Sets logits as \(x\), labels as \(y\), output as \(\ell(x, y)\). Let,

\[L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]\]

In which, \(L\) indicates the loss of all batch_sizes, \(l\) indicates the loss of one batch_size, and n indicates one batch_size in the 1-N range. 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}\]


  • The value of “x” must range from 0 to 1.

  • The value of “y” must be “0” or “1”.


reduction (str) – Specifies the reduction to be applied to the output. Its value must be one of ‘none’, ‘mean’ or ‘sum’. Default: ‘mean’.

  • logits (Tensor) - The input Tensor. The data type must be float16 or float32, The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • labels (Tensor) - The label Tensor which has the same shape and data type as logits.

  • weight (Tensor, optional) - A rescaling weight applied to the loss of each batch element. And it must have the same shape and data type as logits. Default: None.


Tensor or Scalar, if reduction is ‘none’, then output is a tensor and has the same shape as logits. Otherwise, the output is a scalar.

  • TypeError – If dtype of logits, labels or weight (if given) is neither float16 nor float32.

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

  • ValueError – If shape of labels is not the same as logits or weight (if given).

  • TypeError – If logits, labels or weight is not a Tensor.

Supported Platforms:

Ascend GPU CPU


>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.binary_cross_entropy = ops.BinaryCrossEntropy()
...     def construct(self, logits, labels, weight):
...         result = self.binary_cross_entropy(logits, labels, weight)
...         return result
>>> net = Net()
>>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
>>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32)
>>> weight = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = net(logits, labels, weight)
>>> print(output)