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.

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.nn.BCEWithLogitsLoss

class mindspore.nn.BCEWithLogitsLoss(reduction='mean', weight=None, pos_weight=None)[source]

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

Sets input logits as X, input labels as Y, output as L. Then,

pij=sigmoid(Xij)=11+eXij
Lij=[Yijlog(pij)+(1Yij)log(1pij)]

Then,

(x,y)={L,if reduction='none';mean(L),if reduction='mean';sum(L),if reduction='sum'.
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 can be broadcast to a tensor with shape of logits, 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 be broadcast to a tensor with shape of logits, data type must be float16 or float32. Default: None .

Inputs:
  • logits (Tensor) - Input logits with shape (N,) where means, any number of additional dimensions. The data type must be float16 or float32.

  • labels (Tensor) - Ground truth label with shape (N,) where means, any number of additional dimensions. The same shape and data type as logits.

Outputs:

Tensor or Scalar, if reduction is ‘none’, its shape is the same as logits. Otherwise, a scalar value will be returned.

Raises
  • TypeError – If input logits or labels is not Tensor.

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

  • TypeError – If weight or pos_weight is a parameter.

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

  • TypeError – If data type of reduction is not string.

  • 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 GPU CPU

Examples

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> import numpy as np
>>> logits = ms.Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
>>> labels = ms.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