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:
\[\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 is not ‘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) – Apply specific reduction method to the output: ‘none’, ‘mean’, or ‘sum’. Default: ‘mean’.
- 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:
Ascend
GPU
CPU
Examples
>>> logits = mindspore.Tensor(np.random.randn(3, 5), mindspore.float32) >>> labels = mindspore.Tensor(np.array([1, 0, 4]), mindspore.int32) >>> loss = nn.NLLLoss() >>> output = loss(logits, labels)