maxframe.tensor.random.uniform#

maxframe.tensor.random.uniform(low=0.0, high=1.0, size=None, chunk_size=None, gpu=None, dtype=None)[源代码]#

从均匀分布中抽取样本。

样本在半开区间 ``[low, high)``(包括 low,但不包括 high)上均匀分布。换句话说,给定区间内的任何值被 uniform 抽取的可能性相同。

参数:
  • low (float or array_like of floats, optional) -- 输出区间的下界。所有生成的值都将大于或等于 low。默认值为 0。

  • high (float or array_like of floats) -- 输出区间的上界。所有生成的值都将小于 high。默认值为 1.0。

  • size (int or tuple of ints, optional) -- 输出形状。如果给定的形状为,例如 (m, n, k),则抽取 m * n * k 个样本。如果 size 为 None``(默认),且 ``lowhigh 都是标量,则返回单个值。否则抽取 mt.broadcast(low, high).size 个样本。

  • chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的块大小

  • gpu (bool, optional) -- 如果为 True,则在 GPU 上分配张量,默认为 False

  • dtype (data-type, optional) -- 返回张量的数据类型。

返回:

out -- 从参数化的均匀分布中抽取的样本。

返回类型:

Tensor or scalar

参见

randint

离散均匀分布,生成整数。

random_integers

在闭区间 [low, high] 上的离散均匀分布。

random_sample

[0, 1) 上均匀分布的浮点数。

random

random_sample 的别名。

rand

便捷函数,接受维度作为输入,例如 rand(2,2) 会生成一个 2x2 的浮点数组,均匀分布在 [0, 1) 上。

备注

均匀分布的概率密度函数为

\[p(x) = \frac{1}{b - a}\]

在区间 [a, b) 内任意位置为该值,其它位置为零。

high == low 时,将返回 low 的值。如果 high < low,结果未定义,并可能最终引发错误,即不要依赖该函数在传入满足此不等式条件的参数时的行为。

示例

从分布中抽取样本:

>>> import maxframe.tensor as mt
>>> s = mt.random.uniform(-1,0,1000)

所有值都在给定区间内:

>>> mt.all(s >= -1).execute()
True
>>> mt.all(s < 0).execute()
True

显示样本的直方图以及概率密度函数:

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s.execute(), 15, normed=True)
>>> plt.plot(bins, mt.ones_like(bins).execute(), linewidth=2, color='r')
>>> plt.show()