RNG

class RNG(seed=None, device=None)[源代码]

RNG 公开了一些生成随机数的方法。

参数
  • seed (Optional[int]) – 用于初始化伪随机数生成器的随机种子。默认值:None

  • device (Optional[str]) – 生成向量的设备。默认值:None

实际案例

>>> import megengine.random as rand
>>> rng = rand.RNG(seed=100)
>>> x = rng.uniform(size=(2, 2))
>>> x.numpy()   
array([[0.84811664, 0.6147553 ],
       [0.59429836, 0.64727545]], dtype=float32)
beta(alpha, beta, size=None)[源代码]

服从贝塔分布 \(\operatorname{Beta}(\alpha, \beta)\) 的随机变量。

对应的概率密度函数为

\[p(x)=\frac{1}{\mathrm{~B}(\alpha, \beta)} x^{\alpha-1}(1-x)^{\beta-1} \quad \text { for } \alpha, \beta>0,\]

其中 \(\mathrm{~B}(\alpha, \beta)\) 是 Beta 函数,

\[\mathrm{~B}(\alpha, \beta)=\int_{0}^{1} t^{\alpha-1}(1-t)^{\beta-1} d t.\]
参数
  • alpha (Union[Tensor, float]) – the alpha parameter of the distribution. Must be positive.

  • beta (Union[Tensor, float]) – the beta parameter of the distribution. Must be positive.

  • size (Optional[Iterable[int]]) – the size of output tensor. If alpha and beta are scalars and given size is, e.g., (m, n), then the output shape is (m, n). If alpha or beta is a Tensor and given size is, e.g., (m, n), then the output shape is (m, n) + broadcast(alpha, beta).shape. Default: None.

返回

tensor. The random variable with Beta distribution.

返回类型

Return type

实际案例

>>> import megengine.random as rand
>>> x = rand.beta(alpha=2, beta=1, size=(2, 2))
>>> x.numpy()   
array([[0.6172312 , 0.9789006 ],
       [0.50004643, 0.9775796 ]], dtype=float32)
>>> alpha = mge.Tensor([[0.5],
...                     [  3]], dtype="float32")
>>> beta = mge.Tensor([0.5,5], dtype="float32")
>>> x = rand.beta(alpha=alpha, beta=beta)
>>> x.numpy()   
array([[0.0075407 , 0.1275094 ],
       [0.96331763, 0.22299217]], dtype=float32)
>>> x = rand.beta(alpha=alpha, beta=beta, size=2)
>>> x.numpy()   
array([[[0.46863747, 0.13819647],
        [0.8646759 , 0.16014215]],
[[0.0682759 , 0.04448463],

[0.97733796, 0.19206746]]], dtype=float32)

gamma(shape, scale=1, size=None)[源代码]

服从伽玛分布 \(\Gamma(k, \theta)\) 的随机变量。

对应的概率密度函数为

\[p(x)=x^{k-1} \frac{e^{-x / \theta}}{\theta^{k} \Gamma(k)} \quad \text { for } x>0 \quad k, \theta>0,\]

其中 \(\Gamma(k)\) 是 gamma 函数,

\[\Gamma(k)=(k-1) ! \quad \text { for } \quad k \quad \text{is positive integer}.\]
参数
  • shape (Union[Tensor, float]) – the shape parameter (sometimes designated “k”) of the distribution. Must be positive.

  • scale (Union[Tensor, float]) – the scale parameter (sometimes designated “theta”) of the distribution. Must be positive. Default: 1.

  • size (Optional[Iterable[int]]) – the size of output tensor. If shape and scale are scalars and given size is, e.g., (m, n), then the output shape is (m, n). If shape or scale is a Tensor and given size is, e.g., (m, n), then the output shape is (m, n) + broadcast(shape, scale).shape. The broadcast rules are consistent with numpy.broadcast. Default: None.

返回

tensor. The random variable with Gamma distribution.

返回类型

Return type

实际案例

>>> import megengine.random as rand
>>> x = rand.gamma(shape=2, scale=1, size=(2, 2))
>>> x.numpy()   
array([[0.97447544, 1.5668875 ],
       [1.0069491 , 0.3078318 ]], dtype=float32)
>>> shape = mge.Tensor([[ 1],
...                     [10]], dtype="float32")
>>> scale = mge.Tensor([1,5], dtype="float32")
>>> x = rand.gamma(shape=shape, scale=scale)
>>> x.numpy()   
array([[ 0.11312152,  3.0799196 ],
       [10.973469  , 29.596972  ]], dtype=float32)
>>> x = rand.gamma(shape=shape, scale=scale, size=2)
>>> x.numpy()   
array([[[4.35868073e+00, 1.22415285e+01],
        [1.02696848e+01, 4.19773598e+01]],
[[7.73875117e-02, 6.06766164e-01],

[1.22881927e+01, 8.13445740e+01]]], dtype=float32)

normal(mean=0, std=1, size=None)[源代码]

服从高斯分布的随机变量 \(N(\mu, \sigma)\)

参数
  • mean (float) – the mean or expectation of the distribution. Default: 0.

  • std (float) – the standard deviation of the distribution (variance = \(\sigma ^ 2\)). Default: 1.

  • size (Optional[Iterable[int]]) – the size of output tensor. Default: None.

返回

tensor. The random variable with Gaussian distribution.

返回类型

Return type

实际案例

>>> import megengine.random as rand
>>> x = rand.normal(mean=0, std=1, size=(2, 2))
>>> x.numpy()   
array([[ 1.5534291 , -0.28356555],
       [ 2.2230418 , -0.92425716]], dtype=float32)
permutation(n, *, dtype='int32')[源代码]
打乱一个张量,或者返回给定范围的随机张量

如果 n 是一个张量,只会沿着第一个轴打乱

参数
  • n (Union[int, Tensor]) – 如果 n 是一个整数,会返回 0 到 n-1 的一个随机张量。如果 n 是一个张量会返回一个新的打乱后的张量

  • dtype (str) – 当 n 为整数时返回张量设置的类型。支持 int32,int16 和 float32,默认为 int32

返回

The output tensor.

实际案例

>>> import numpy as np
>>> import megengine.random as rand
>>> x = rand.permutation(10, dtype="int32")
>>> x.numpy()   
array([8, 4, 0, 3, 5, 6, 2, 1, 7, 9], dtype=int32)
>>> x = rand.permutation(10, dtype="float32")
>>> x.numpy()   
array([1., 3., 0., 2., 4., 8., 7., 9., 6., 5.], dtype=float32)
>>> x = mge.tensor(np.arange(18)).reshape(6,3)
>>> x = rand.permutation(x)
>>> x.numpy()   
array([[15, 16, 17],
       [ 6,  7,  8],
       [ 0,  1,  2],
       [ 3,  4,  5],
       [12, 13, 14],
       [ 9, 10, 11]], dtype=int32)
poisson(lam, size=None)[源代码]

服从泊松分布 \(\operatorname{Poisson}(\lambda)\) 的随机变量。

对应的概率密度函数为

\[f(k ; \lambda)=\frac{\lambda^{k} e^{-\lambda}}{k !},\]

其中 k 是出现的次数 \(({\displaystyle k=0,1,2...})\)

参数
  • lam (Union[float, Tensor]) – the lambda parameter of the distribution. Must be positive.

  • size (Optional[Iterable[int]]) – 输出向量的大小。如果 lam 是标量,例如给定大小为 (m, n),则输出形状为 (m, n)。如果 lam 是一个向量,例如给定的大小是 (m, n) ,那么输出形状是 (m, n, k, v). 默认值:None

返回

tensor. The random variable with Poisson distribution.

返回类型

Return type

实际案例

>>> import megengine.random as rand
>>> x = rand.poisson(lam=2., size=(1, 3))
>>> x.numpy()   
array([[1., 2., 2.]], dtype=float32)
>>> lam = mge.Tensor([[1.,1.],
...                 [10,10]], dtype="float32")
>>> x = rand.poisson(lam=lam)
>>> x.numpy()   
array([[ 1.,  2.],
       [11., 11.]], dtype=float32)
>>> x = rand.poisson(lam=lam, size=(1,3))
>>> x.numpy()   
array([[[[ 2.,  1.],
         [10.,  8.]],
[[ 5., 2.],

[10., 10.]],

[[ 1., 2.],

[ 8., 10.]]]], dtype=float32)

shuffle(inp)[源代码]

打乱张量的内容原地修改张量。这个函数只是沿着第一个轴打乱张量。张量顺序改变但不改变值

参数

inp (Tensor) – 输入张量。

返回

None.

实际案例

>>> import numpy as np
>>> import megengine.random as rand
>>> x = mge.tensor(np.arange(10))
>>> rand.shuffle(x)
>>> x.numpy()   
array([4, 5, 9, 6, 2, 8, 1, 0, 3, 7], dtype=int32)
>>> y = mge.tensor(np.arange(18)).reshape(6,3)
>>> rand.shuffle(y)
>>> y.numpy()   
array([[ 3,  4,  5],
       [ 6,  7,  8],
       [15, 16, 17],
       [ 0,  1,  2],
       [12, 13, 14],
       [ 9, 10, 11]], dtype=int32)
uniform(low=0, high=1, size=None)[源代码]

Random variable with uniform distribution \(U(low, high)\).

参数
  • low (float) – lower range. Default: 0.

  • high (float) – upper range. Default: 1.

  • size (Optional[Iterable[int]]) – the size of output tensor. Default: None.

返回

tensor. The random variable with uniform distribution.

返回类型

Return type

实际案例

>>> import megengine.random as rand
>>> x = rand.uniform(size=(2, 2))
>>> x.numpy()   
array([[0.28603864, 0.3156649 ],
       [0.42066026, 0.9805052 ]], dtype=float32)