mindspore.ops.nll_loss

mindspore.ops.nll_loss(inputs, target, weight=None, ignore_index=- 100, reduction='mean', label_smoothing=0.0)[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' 
Parameters
  • inputs (Tensor) – (N,C) where C = number of classes or (N,C,H,W) in case of 2D Loss, or (N,C,d1,d2,...,dK). inputs is expected to be log-probabilities, data type must be float16 or float32.

  • target (Tensor) – (N) or (N,d1,d2,...,dK) for high-dimensional loss, data type must be int32.

  • weight (Tensor) – A rescaling weight applied to the loss of each batch element. If not None, the shape is (C,). The data type must be float16 or float32. Default: None .

  • ignore_index (int) – Specifies a target value that is ignored and does not contribute to the input 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.

  • label_smoothing (float) – Label smoothing values, a regularization tool used to prevent the model from overfitting when calculating Loss. The value range is [0.0, 1.0]. Default value: 0.0 .

Returns

Tensor, the computed loss value.

Supported Platforms:

Ascend GPU CPU

Examples

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