mindspore.nn.NLLLoss

View Source On Gitee
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:

\[\ell(x, t)=L=\left\{l_{1}, \ldots, l_{N}\right\}^{\top}, \quad l_{n}=-w_{t_{n}} x_{n, t_{n}}, \quad w_{c}=\text { weight }[c] \cdot \mathbb{1}\{c \not= \text{ignore_index}\}\]

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

If \(reduction \neq none\) (default 'mean' ), then

\[\begin{split}\ell(x, t)=\left\{\begin{array}{ll} \sum_{n=1}^{N} \frac{1}{\sum_{n=1}^{N} w_{t n}} l_{n}, & \text { if reduction }=\text { 'mean', } \\ \sum_{n=1}^{N} l_{n}, & \text { if reduction }=\text { 'sum' } \end{array}\right.\end{split}\]
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, d_1, d_2, ..., d_K)\) 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, d_1, d_2, ..., d_K)\) 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)