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

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

Gets the negative log likelihood loss between inputs and target.

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

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

where x is the inputs, t is the target, w is the weight, N is the batch size, c belonging to [0,C1] 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' 

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. If not None, the shape is (C,), data type must be float16 or float32 or bfloat16(only supported by Atlas A2 training series products). It should have the same data type as input . Default: None .

  • ignore_index (int, optional) – Specifies a target value that is ignored and does not contribute to the input gradient. Only valid in class indices, please set it to a negative number in probabilities. Default: -100 .

  • 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) - (N) or (N,C) where C = number of classes , N = batch size , or (N,C,d1,d2,...,dK) (for high-dimensional data). input is expected to be log-probabilities, data type must be float16 or float32 or bfloat16(only supported by Atlas A2 training series products).

  • target (Tensor) - () or (N) , where the value range is [0,C1], or (N,d1,d2,...,dK) for high-dimensional loss, data type must be int32 or int64 or uint8.

Outputs:

Tensor, the data type is the same as input .

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, mint
>>> inputs = mindspore.Tensor(np.random.randn(3, 5), mindspore.float32)
>>> target = mindspore.Tensor(np.array([1, 0, 4]), mindspore.int32)
>>> op = mindspore.mint.nn.NLLLoss()
>>> output = op(inputs, target)