mindspore.nn.probability.distribution.Uniform

查看源文件
class mindspore.nn.probability.distribution.Uniform(low=None, high=None, seed=None, dtype=mstype.float32, name='Uniform')[源代码]

均匀分布(Uniform Distribution)。 连续随机分布,取值范围为 \([a, b]\) ,概率密度函数为

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

其中 \(a, b\) 为分别为均匀分布的下界和上界。

参数:
  • low (int, float, list, numpy.ndarray, Tensor) - 分布的下限。默认值: None

  • high (int, float, list, numpy.ndarray, Tensor) - 分布的上限。默认值: None

  • seed (int) - 采样时使用的种子。如果为None,则使用全局种子。默认值: None

  • dtype (mindspore.dtype) - 事件样例的类型。默认值: mstype.float32

  • name (str) - 分布的名称。默认值: 'Uniform'

说明

  • low 必须小于 high

  • dtype 必须是float类型,因为均匀分布是连续的。

异常:
  • ValueError - low 大于等于 high

  • TypeError - dtype 不是float的子类。

支持平台:

Ascend GPU CPU

样例:

>>> 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

返回分布的上限。

返回:

Tensor,分布的上限值。

property low

返回分布的下限。

返回:

Tensor,分布的下限值。

cdf(value, high, low)

在给定值下计算累积分布函数(Cumulatuve Distribution Function, CDF)。

参数:
  • value (Tensor) - 要计算的值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,累积分布函数的值。

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

计算分布a和b之间的交叉熵。

参数:
  • dist (str) - 分布的类型。

  • high_b (Tensor) - 对比分布的上限值。

  • low_b (Tensor) - 对比分布的下限值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,交叉熵的值。

entropy(high, low)

计算熵。

参数:
  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,熵的值。

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

计算KL散度,即KL(a||b)。

参数:
  • dist (str) - 分布的类型。

  • high_b (Tensor) - 对比分布的上限值。

  • low_b (Tensor) - 对比分布的下限值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,KL散度。

log_cdf(value, high, low)

计算给定值对于的累积分布函数的对数。

参数:
  • value (Tensor) - 要计算的值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,累积分布函数的对数。

log_prob(value, high, low)

计算给定值对应的概率的对数。

参数:
  • value (Tensor) - 要计算的值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,累积分布函数的对数。

log_survival(value, high, low)

计算给定值对应的生存函数的对数。

参数:
  • value (Tensor) - 要计算的值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,生存函数的对数。

mean(high, low)

计算期望。

参数:
  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,概率分布的期望。

mode(high, low)

计算众数。

参数:
  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,概率分布的众数。

prob(value, high, low)

计算给定值下的概率。对于连续是计算概率密度函数(Probability Density Function)。

参数:
  • value (Tensor) - 要计算的值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,概率值。

sample(shape, high, low)

采样函数。

参数:
  • shape (tuple) - 样本的shape。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,根据概率分布采样的样本。

sd(high, low)

计算标准差。

参数:
  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,概率分布的标准差。

survival_function(value, high, low)

计算给定值对应的生存函数。

参数:
  • value (Tensor) - 要计算的值。

  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,生存函数的值。

var(high, low)

计算方差。

参数:
  • high (Tensor) - 分布的上限值。默认值: None

  • low (Tensor) - 分布的下限值。默认值: None

返回:

Tensor,概率分布的方差。