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.ops.NLLLoss

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

Gets the negative log likelihood loss between logits and labels.

The nll loss with reduction=none can be described as:

(x,t)=L={l1,,lN},ln=wtnxn,tn,wc= weight [c]1

where x is the logits, t is the labels, w is the weight, N is the batch size, c belonging [0, C-1] is class index, where C is the number of classes.

If reduction is not ‘none’ (default ‘mean’), then

(x,t)={n=1N1n=1Nwtnln, if reduction = 'mean'; n=1Nln, if reduction = 'sum' 
Parameters

reduction (str) – Apply specific reduction method to the output: ‘none’, ‘mean’, ‘sum’, Default: “mean”.

Inputs:
  • logits (Tensor) - Input logits, with shape (N,C). Data type only support float32 or float16.

  • labels (Tensor) - Ground truth labels, with shape (N,). Data type only support int32.

  • weight (Tensor) - The rescaling weight to each class, with shape (C,) and data type only support float32 or float16.

Outputs:

Tuple of 2 tensors composed with loss and total_weight.

  • loss (Tensor) - When reduction is ‘none’ and logits is 2D tensor, the loss shape is (N,). Otherwise, the loss is a scalar. The data type is same with input’s.

  • total_weight (Tensor) - The total_weight is a scalar. The data type is same with weight’s.

Raises
  • TypeError – If dtype of logits or weight is neither float16 nor float32, labels is not int32.

  • ValueError – If logits is not a one or two dimension tensor, labels and weight not a one dimension tensor. When logits is a two dimension tensor, the first dimension of logits is not equal to labels, and second dimension of logits is not equal to weight. When logits is a one dimension tensor, the dimensions of logits, labels and weight should be equal to each other.

Supported Platforms:

Ascend GPU

Examples

>>> logits = Tensor(np.array([[0.5488135, 0.71518934],
...                           [0.60276335, 0.5448832],
...                           [0.4236548, 0.6458941]]).astype(np.float32))
>>> labels = Tensor(np.array([0, 0, 0]).astype(np.int32))
>>> weight = Tensor(np.array([0.3834415, 0.79172504]).astype(np.float32))
>>> nll_loss = ops.NLLLoss(reduction="mean")
>>> loss, weight = nll_loss(logits, labels, weight)
>>> print(loss)
-0.52507716
>>> print(weight)
1.1503246