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.
Warning
After version 2.9.0, the forward inputs logits and labels will be renamed to input and target.
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, optional) – 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, optional) – 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
- Supported Platforms:
AscendGPUCPU
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)