# mindspore.nn.probability.distribution.Uniform

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

$f(x, a, b) = 1 / (b - a).$

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

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

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

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

Tensor，累积分布函数的值。

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

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

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

• 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，概率分布的方差。