maxframe.tensor.random.sample#

maxframe.tensor.random.sample(size=None, chunk_size=None, gpu=None, dtype=None)#

返回半开区间 [0.0, 1.0) 内的随机浮点数。

结果来自给定区间的“连续均匀”分布。要从 \(Unif[a, b), b > a\) 中采样,可以将 random_sample 的输出乘以 (b-a) 并加上 a

(b - a) * random_sample() + a
参数:
  • size (int or tuple of ints, optional) -- 输出形状。如果给定的形状是例如 (m, n, k),则抽取 m * n * k 个样本。默认为 None,此时返回单个值。

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

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

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

返回:

out -- 形状为 size 的随机浮点数数组(除非 size=None,此时返回单个浮点数)。

返回类型:

float or Tensor of floats

示例

>>> import maxframe.tensor as mt
>>> mt.random.random_sample().execute()
0.47108547995356098
>>> type(mt.random.random_sample().execute())
<type 'float'>
>>> mt.random.random_sample((5,)).execute()
array([ 0.30220482,  0.86820401,  0.1654503 ,  0.11659149,  0.54323428])

从 [-5, 0) 中抽取的 3 行 2 列的随机数数组:

>>> (5 * mt.random.random_sample((3, 2)) - 5).execute()
array([[-3.99149989, -0.52338984],
       [-2.99091858, -0.79479508],
       [-1.23204345, -1.75224494]])