mindspore.nn.probability.distribution.Uniform

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

Uniform Distribution. A Uniform distributio is a continuous distribution with the range \([a, b]\) and the probability density function:

\[f(x, a, b) = 1 / (b - a),\]

where \(a, b\) are the lower and upper bound respectively.

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

  • high (int, float, list, numpy.ndarray, Tensor) – The upper bound of the distribution. Default: None .

  • seed (int) – The seed uses 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: 'Uniform' .

Note

low must be strictly less than high. dist_spec_args are high and low. dtype must be float type because Uniform distributions are continuous.

Raises
  • ValueError – When high <= low.

  • TypeError – When the input dtype is not a subclass of float.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.nn.probability.distribution as msd
>>> from mindspore import Tensor
>>> # To initialize a Uniform distribution of the lower bound 0.0 and the higher bound 1.0.
>>> u1 = msd.Uniform(0.0, 1.0, dtype=mindspore.float32)
>>> # A Uniform distribution can be initialized without arguments.
>>> # In this case, `high` and `low` must be passed in through arguments during function calls.
>>> u2 = msd.Uniform(dtype=mindspore.float32)
>>>
>>> # Here are some tensors used below for testing
>>> value = Tensor([0.5, 0.8], dtype=mindspore.float32)
>>> low_a = Tensor([0., 0.], dtype=mindspore.float32)
>>> high_a = Tensor([2.0, 4.0], dtype=mindspore.float32)
>>> low_b = Tensor([-1.5], dtype=mindspore.float32)
>>> high_b = Tensor([2.5, 5.], 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.
>>> # Args:
>>> #     value (Tensor): the value to be evaluated.
>>> #     low (Tensor): the lower bound of the distribution. Default: self.low.
>>> #     high (Tensor): the higher bound of the distribution. Default: self.high.
>>> # Examples of `prob`.
>>> # Similar calls can be made to other probability functions
>>> # by replacing 'prob' by the name of the function.
>>> ans = u1.prob(value)
>>> print(ans.shape)
(2,)
>>> # Evaluate with respect to distribution b.
>>> ans = u1.prob(value, low_b, high_b)
>>> print(ans.shape)
(2,)
>>> # `high` and `low` must be passed in during function calls.
>>> ans = u2.prob(value, low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Functions `mean`, `sd`, `var`, and `entropy` have the same arguments.
>>> # Args:
>>> #     low (Tensor): the lower bound of the distribution. Default: self.low.
>>> #     high (Tensor): the higher bound of the distribution. Default: self.high.
>>> # Examples of `mean`. `sd`, `var`, and `entropy` are similar.
>>> ans = u1.mean() # return 0.5
>>> print(ans.shape)
()
>>> ans = u1.mean(low_b, high_b) # return (low_b + high_b) / 2
>>> print(ans.shape)
(2,)
>>> # `high` and `low` must be passed in during function calls.
>>> ans = u2.mean(low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Interfaces of 'kl_loss' and 'cross_entropy' are the same.
>>> # Args:
>>> #     dist (str): the type of the distributions. Should be "Uniform" in this case.
>>> #     low_b (Tensor): the lower bound of distribution b.
>>> #     high_b (Tensor): the upper bound of distribution b.
>>> #     low_a (Tensor): the lower bound of distribution a. Default: self.low.
>>> #     high_a (Tensor): the upper bound of distribution a. Default: self.high.
>>> # Examples of `kl_loss`. `cross_entropy` is similar.
>>> ans = u1.kl_loss('Uniform', low_b, high_b)
>>> print(ans.shape)
(2,)
>>> ans = u1.kl_loss('Uniform', low_b, high_b, low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Additional `high` and `low` must be passed in.
>>> ans = u2.kl_loss('Uniform', low_b, high_b, low_a, high_a)
>>> print(ans.shape)
(2,)
>>> # Examples of `sample`.
>>> # Args:
>>> #     shape (tuple): the shape of the sample. Default: ()
>>> #     low (Tensor): the lower bound of the distribution. Default: self.low.
>>> #     high (Tensor): the upper bound of the distribution. Default: self.high.
>>> ans = u1.sample()
>>> print(ans.shape)
()
>>> ans = u1.sample((2,3))
>>> print(ans.shape)
(2, 3)
>>> ans = u1.sample((2,3), low_b, high_b)
>>> print(ans.shape)
(2, 3, 2)
>>> ans = u2.sample((2,3), low_a, high_a)
>>> print(ans.shape)
(2, 3, 2)
property high

Return the upper bound of the distribution.

Returns

Tensor, the upper bound of the distribution.

property low

Return the upper bound of the distribution.

Returns

Tensor, the lower bound of the distribution.

cdf(value, high, low)

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

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

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

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

cross_entropy(dist, high_b, low_b, high, low)

Compute the cross entropy of two distribution.

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

  • high_b (Tensor) - the upper bound of the other distribution.

  • low_b (Tensor) - the lower bound of the other distribution.

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the value of the cross entropy.

entropy(high, low)

Compute the value of the entropy.

Parameters
  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the value of the entropy.

kl_loss(dist, high_b, low_b, high, low)

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

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

  • high_b (Tensor) - the upper bound of the other distribution.

  • low_b (Tensor) - the lower bound of the other distribution.

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the value of the K-L loss.

log_cdf(value, high, low)

Compute the log value of the cumulatuve distribution function.

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

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the log value of the cumulatuve distribution function.

log_prob(value, high, low)

the log value of the probability.

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

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the log value of the probability.

log_survival(value, high, low)

Compute the log value of the survival function.

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

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the value of the K-L loss.

mean(high, low)

Compute the mean value of the distribution.

Parameters
  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the mean of the distribution.

mode(high, low)

Compute the mode value of the distribution.

Parameters
  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the mode of the distribution.

prob(value, high, low)

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

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

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the value of the probability.

sample(shape, high, low)

Generate samples.

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

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the sample following the distribution.

sd(high, low)

The standard deviation.

Parameters
  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the standard deviation of the distribution.

survival_function(value, high, low)

Compute the value of the survival function.

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

  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the value of the survival function.

var(high, low)

Compute the variance of the distribution.

Parameters
  • high (Tensor) - the upper bound of the distribution. Default: None .

  • low (Tensor) - the lower bound of the distribution. Default: None .

Returns

Tensor, the variance of the distribution.