mindspore.nn.probability.distribution.HalfNormal

class mindspore.nn.probability.distribution.HalfNormal(mean=None, sd=None, seed=None, dtype=mstype.float32, name='HalfNormal')[source]

HalfNormal distribution. A HalfNormal distribution is a continuous distribution with the range \([\mu, \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 half normal distribution respectively.

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

  • sd (int, float, list, numpy.ndarray, Tensor) – The standard deviation of the 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: ‘HalfNormal’.

Note

  • sd must be greater than zero.

  • dist_spec_args are mean and sd.

  • dtype must be a float type because HalfNormal distributions are continuous.

Raises
Supported Platforms:

CPU

Examples

>>> import mindspore
>>> import mindspore.nn as nn
>>> from mindspore.nn.probability.distribution import HalfNormal
>>> from mindspore import Tensor
>>> # To initialize a HalfNormal distribution of the mean 3.0 and the standard deviation 4.0.
>>> n1 = HalfNormal(3.0, 4.0, dtype=mindspore.float32)
>>> # A HalfNormal distribution can be initialized without arguments.
>>> # In this case, `mean` and `sd` must be passed in through arguments.
>>> hn = HalfNormal(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.5], dtype=mindspore.float32)
>>> ans = n1.log_prob(value)
>>> print(ans.shape)
(3,)
>>> # Evaluate with respect to the distribution b.
>>> ans = n1.log_prob(value, mean_b, sd_b)
>>> print(ans.shape)
(3,)
>>> # `mean` and `sd` must be passed in during function calls
>>> ans = hn.log_prob(value, mean_a, sd_a)
>>> print(ans.shape)
(3,)
log_prob(value, mean=None, sd=None)

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.