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

class mindspore.nn.NLLLoss(weight=None, ignore_index=- 100, 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{cignore_index}

where x is the logits, t is the labels, 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 reductionnone (default 'mean' ), then

(x,t)={n=1N1n=1Nwtnln, if reduction = 'mean', n=1Nln, if reduction = 'sum' 
Parameters
  • weight (Tensor) – The rescaling weight to each class. If the value is not None, the shape is (C,). The data type only supports float32 or float16. Default: None .

  • ignore_index (int) – Specifies a target value that is ignored (typically for padding value) and does not contribute to the gradient. 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:
  • logits (Tensor) - Tensor of shape (N,C) or (N,C,d1,d2,...,dK) for K-dimensional data, where C = number of classes. Data type must be float16 or float32. inputs needs to be logarithmic probability.

  • labels (Tensor) -(N) or (N,d1,d2,...,dK) for K-dimensional data. Data type must be int32.

Returns

Tensor, the computed negative log likelihood loss value.

Raises
  • TypeError – If weight is not a Tensor.

  • TypeError – If ignore_index is not an int.

  • TypeError – If the data type of weight is not float16 or float32.

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

  • TypeError – If logits is not a Tensor.

  • TypeError – If labels is not a Tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> import numpy as np
>>> logits = ms.Tensor(np.random.randn(3, 5), ms.float32)
>>> labels = ms.Tensor(np.array([1, 0, 4]), ms.int32)
>>> loss = nn.NLLLoss()
>>> output = loss(logits, labels)