mindspore.nn.GaussianNLLLoss

class mindspore.nn.GaussianNLLLoss(*, full=False, eps=1e-06, reduction='mean')[source]

Gaussian negative log likelihood loss.

The target values are considered to be samples from a Gaussian distribution, where the expectation and variance are predicted by a neural network. For labels modeled on a Gaussian distribution, logits to record expectations, and the variance var (elements are all positive), the calculated loss is:

\[\text{loss} = \frac{1}{2}\left(\log\left(\text{max}\left(\text{var}, \ \text{eps}\right)\right) + \frac{\left(\text{logits} - \text{labels}\right)^2} {\text{max}\left(\text{var}, \ \text{eps}\right)}\right) + \text{const.}\]

where \(eps\) is used for stability of \(log\). When \(full=True\), a constant will be added to the loss. If the shape of \(var\) and \(logits\) are not the same (due to a homoscedastic assumption), their shapes must allow correct broadcasting.

Keyword Arguments
  • full (bool, optional) – Whether include the constant term in the loss calculation. When \(full=True\), the constant term const. will be \(0.5 * log(2\pi)\). Default: False .

  • eps (float, optional) – Used to improve the stability of log function. Default: 1e-6 .

  • 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 mean of elements in the output.

    • 'sum': the output elements will be summed.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) or \((*)\) where \(*\) means any number of additional dimensions.

  • labels (Tensor) - Tensor of shape \((N, *)\) or \((*)\), same shape as the logits, or same shape as the logits but with one dimension equal to 1 (to allow for broadcasting).

  • var - Tensor of shape \((N, *)\) or \((*)\), same shape as logits, or same shape as the logits but with one dimension equal to 1, or same shape as the logits but with one fewer dimension (to allow for broadcasting).

Returns

Tensor or Tensor scalar, the computed loss depending on \(reduction\).

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> import numpy as np
>>> arr1 = np.arange(8).reshape((4, 2))
>>> arr2 = np.array([2, 3, 1, 4, 6, 4, 4, 9]).reshape((4, 2))
>>> logits = ms.Tensor(arr1, ms.float32)
>>> labels = ms.Tensor(arr2, ms.float32)
>>> loss = nn.GaussianNLLLoss(reduction='mean')
>>> var = ms.Tensor(np.ones((4, 1)), ms.float32)
>>> output = loss(logits, labels, var)
>>> print(output)
1.4374993
Reference:

Nix, D. A. and Weigend, A. S., "Estimating the mean and variance of the target probability distribution", Proceedings of 1994 IEEE International Conference on Neural Networks (ICNN'94), Orlando, FL, USA, 1994, pp. 55-60 vol.1, doi: 10.1109/ICNN.1994.374138.