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.BCELoss

View Source On Gitee
class mindspore.mint.nn.BCELoss(weight=None, reduction='mean')[source]

Compute the binary cross entropy between the true labels and predicted labels.

Set the predicted labels as x, true labels as y, the output loss as (x,y). The formula is as follow:

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

where N is the batch size. Then,

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

Note

Note that the predicted labels should always be the output of sigmoid. Because it is a two-class classification, the true labels should be numbers between 0 and 1. And if xn is either 0 or 1, one of the log terms would be mathematically undefined in the above loss equation.

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • 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 inputs. Default: None .

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

Inputs:
  • input (Tensor) - The input tensor with shape (N,) where means, any number of additional dimensions. The data type must be float16 or float32 or bfloat16(only supported by Atlas A2 training series products).

  • target (Tensor) - The label tensor with shape (N,) where means, any number of additional dimensions. The same shape and data type as input.

Outputs:

Tensor, has the same dtype as input. if reduction is 'none', then it has the same shape as input. Otherwise, it is a scalar Tensor.

Raises
  • TypeError – If dtype of input, target or weight (if given) is not float16, float32 or bfloat16.

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

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

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> from mindspore import mint
>>> import numpy as np
>>> weight = ms.Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 3.3, 2.2]]), ms.float32)
>>> loss = mint.nn.BCELoss(weight=weight, reduction='mean')
>>> input = ms.Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]), ms.float32)
>>> target = ms.Tensor(np.array([[0, 1, 0], [0, 0, 1]]), ms.float32)
>>> output = loss(input, target)
>>> print(output)
1.8952923