mindspore.nn.probability.distribution.Normal

View Source On Gitee
class mindspore.nn.probability.distribution.Normal(mean=None, sd=None, seed=None, dtype=mstype.float32, name='Normal')[source]

Normal distribution. A Normal distribution is a continuous distribution with the range \((-\inf, \inf)\) and the probability density function:

\[f(x, \mu, \sigma) = 1 / \sigma\sqrt{2\pi} \exp(-(x - \mu)^2 / 2\sigma^2).\]

where \(\mu, \sigma\) are the mean and the standard deviation of the normal distribution respectively.

Parameters
  • mean (int, float, list, numpy.ndarray, Tensor) – The mean of the Normal distribution. Default: None .

  • sd (int, float, list, numpy.ndarray, Tensor) – The standard deviation of the Normal distribution. Default: None .

  • seed (int) – The seed used in sampling. The global seed is used if it is None. Default: None .

  • dtype (mindspore.dtype) – The type of the event samples. Default: mstype.float32 .

  • name (str) – The name of the distribution. Default: 'Normal' .

Note

sd must be greater than zero. dist_spec_args are mean and sd. dtype must be a float type because Normal distributions are continuous.

Raises
Supported Platforms:

Ascend GPU

Examples

>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.nn.probability.distribution as msd
>>> from mindspore import Tensor
>>> # To initialize a Normal distribution of the mean 3.0 and the standard deviation 4.0.
>>> n1 = msd.Normal(3.0, 4.0, dtype=mindspore.float32)
>>> # A Normal distribution can be initialized without arguments.
>>> # In this case, `mean` and `sd` must be passed in through arguments.
>>> n2 = msd.Normal(dtype=mindspore.float32)
>>> # Here are some tensors used below for testing
>>> value = Tensor([1.0, 2.0, 3.0], dtype=mindspore.float32)
>>> mean_a = Tensor([2.0], dtype=mindspore.float32)
>>> sd_a = Tensor([2.0, 2.0, 2.0], dtype=mindspore.float32)
>>> mean_b = Tensor([1.0], dtype=mindspore.float32)
>>> sd_b = Tensor([1.0, 1.5, 2.0], dtype=mindspore.float32)
>>> # Private interfaces of probability functions corresponding to public interfaces, including
>>> # `prob`, `log_prob`, `cdf`, `log_cdf`, `survival_function`, and `log_survival`,
>>> # have the same arguments as follows.
>>> # Args:
>>> #     value (Tensor): the value to be evaluated.
>>> #     mean (Tensor): the mean of the distribution. Default: self._mean_value.
>>> #     sd (Tensor): the standard deviation of the distribution. Default: self._sd_value.
>>> # Examples of `prob`.
>>> # Similar calls can be made to other probability functions
>>> # by replacing 'prob' by the name of the function
>>> ans = n1.prob(value)
>>> print(ans.shape)
(3,)
>>> # Evaluate with respect to the distribution b.
>>> ans = n1.prob(value, mean_b, sd_b)
>>> print(ans.shape)
(3,)
>>> # `mean` and `sd` must be passed in during function calls
>>> ans = n2.prob(value, mean_a, sd_a)
>>> print(ans.shape)
(3,)
>>> # Functions `mean`, `sd`, `var`, and `entropy` have the same arguments.
>>> # Args:
>>> #     mean (Tensor): the mean of the distribution. Default: self._mean_value.
>>> #     sd (Tensor): the standard deviation of the distribution. Default: self._sd_value.
>>> # Example of `mean`. `sd`, `var`, and `entropy` are similar.
>>> ans = n1.mean() # return 0.0
>>> print(ans.shape)
()
>>> ans = n1.mean(mean_b, sd_b) # return mean_b
>>> print(ans.shape)
(3,)
>>> # `mean` and `sd` must be passed in during function calls.
>>> ans = n2.mean(mean_a, sd_a)
>>> print(ans.shape)
(3,)
>>> # Interfaces of 'kl_loss' and 'cross_entropy' are the same:
>>> # Args:
>>> #     dist (str): the type of the distributions. Only "Normal" is supported.
>>> #     mean_b (Tensor): the mean of distribution b.
>>> #     sd_b (Tensor): the standard deviation of distribution b.
>>> #     mean_a (Tensor): the mean of distribution a. Default: self._mean_value.
>>> #     sd_a (Tensor): the standard deviation of distribution a. Default: self._sd_value.
>>> # Examples of `kl_loss`. `cross_entropy` is similar.
>>> ans = n1.kl_loss('Normal', mean_b, sd_b)
>>> print(ans.shape)
(3,)
>>> ans = n1.kl_loss('Normal', mean_b, sd_b, mean_a, sd_a)
>>> print(ans.shape)
(3,)
>>> # Additional `mean` and `sd` must be passed in.
>>> ans = n2.kl_loss('Normal', mean_b, sd_b, mean_a, sd_a)
>>> print(ans.shape)
(3,)
>>> # Examples of `sample`.
>>> # Args:
>>> #     shape (tuple): the shape of the sample. Default: ()
>>> #     mean (Tensor): the mean of the distribution. Default: self._mean_value.
>>> #     sd (Tensor): the standard deviation of the distribution. Default: self._sd_value.
>>> ans = n1.sample()
>>> print(ans.shape)
()
>>> ans = n1.sample((2,3))
>>> print(ans.shape)
(2, 3)
>>> ans = n1.sample((2,3), mean_b, sd_b)
>>> print(ans.shape)
(2, 3, 3)
>>> ans = n2.sample((2,3), mean_a, sd_a)
>>> print(ans.shape)
(2, 3, 3)
property mean

Return the mean of the distribution.

Returns

Tensor, the mean of the distribution.

property sd

Return the standard deviation of the distribution.

Returns

Tensor, the standard deviation of the distribution.

cdf(value, mean, sd)

Compute the cumulatuve distribution function(CDF) of the given value.

Parameters
  • value (Tensor) - the value to compute.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the value of the cumulatuve distribution function for the given input.

cross_entropy(dist, mean_b, sd_b, mean, sd)

Compute the cross entropy of two distribution.

Parameters
  • dist (str) - the type of the other distribution.

  • mean_b (Tensor) - the mean of the other distribution.

  • sd_b (Tensor) - the standard deviation of the other distribution.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the value of the cross entropy.

entropy(mean, sd)

Compute the value of the entropy.

Parameters
  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the value of the entropy.

kl_loss(dist, mean_b, sd_b, mean, sd)

Compute the value of the K-L loss between two distribution, namely KL(a||b).

Parameters
  • dist (str) - the type of the other distribution.

  • mean_b (Tensor) - the mean of the other distribution.

  • sd_b (Tensor) - the standard deviation of the other distribution.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the value of the K-L loss.

log_cdf(value, mean, sd)

Compute the log value of the cumulatuve distribution function.

Parameters
  • value (Tensor) - the value to compute.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value, mean, sd)

the log value of the probability.

Parameters
  • value (Tensor) - the value to compute.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the log value of the probability.

log_survival(value, mean, sd)

Compute the log value of the survival function.

Parameters
  • value (Tensor) - the value to compute.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the value of the K-L loss.

mode(mean, sd)

Compute the mode value of the distribution.

Parameters
  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the mode of the distribution.

prob(value, mean, sd)

The probability of the given value. For the continuous distribution, it is the probability density function.

Parameters
  • value (Tensor) - the value to compute.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the value of the probability.

sample(shape, mean, sd)

Generate samples.

Parameters
  • shape (tuple) - the shape of the sample.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the sample following the distribution.

survival_function(value, mean, sd)

Compute the value of the survival function.

Parameters
  • value (Tensor) - the value to compute.

  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the value of the survival function.

var(mean, sd)

Compute the variance of the distribution.

Parameters
  • mean (Tensor) - the mean of the distribution. Default: None .

  • sd (Tensor) - the standard deviation of the distribution. Default: None .

Returns

Tensor, the variance of the distribution.