Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.mint.nn.functional.binary_cross_entropy

View Source On Gitee
mindspore.mint.nn.functional.binary_cross_entropy(logits, labels, weight=None, reduction='mean')[source]

Computes the binary cross entropy(Measure the difference information between two probability distributions) between predictive value logits and target value labels.

Set logits as x, labels as y, output as (x,y), the weight of nth batch of binary cross entropy is wn. Let,

L={l1,,lN},ln=wn[ynlogxn+(1yn)log(1xn)]

In which, L indicates the loss of all batch_size, l indicates the loss of one batch_size, and n indicates one batch_size in the 1N range. Then,

(x,y)={L,if reduction='none';mean(L),if reduction='mean';sum(L),if reduction='sum'.

Warning

  • The value of logits must range from 0 to l.

Parameters
  • logits (Tensor) – The predictive value whose data type must be float16 or float32.

  • labels (Tensor) – The target value which has the same shape and data type as logits. And the data type is float16 or float32.

  • weight (Tensor, optional) – A rescaling weight applied to the loss of each batch element. Its shape must be able to broadcast to that of logits and labels. And it must have the same shape and data type as logits. Default: None . If set to None , the loss function will not consider any sample weights, and each sample will be treated as having equal importance when calculating the loss.

  • reduction (str, optional) –

    Apply specific reduction method to the output: 'none' , 'mean' , 'sum' . Default: 'mean' .

    • 'none': no reduction will be applied.

    • 'mean': compute and return the weighted mean of elements in the output.

    • 'sum': the output elements will be summed.

Returns

Tensor or Scalar. Returns Tensor that has the same dtype and shape as logits if reduction is 'none'. Otherwise, returns a scalar Tensor.

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

  • 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).

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, mint
>>> 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 = mint.nn.functional.binary_cross_entropy(logits, labels, weight)
>>> print(output)
0.38240486