# RNG#

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

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

>>> 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)[源代码]#

$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)=\int_{0}^{1} t^{\alpha-1}(1-t)^{\beta-1} d t.$

the output tensor.

>>> 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)[源代码]#

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

$\Gamma(k)=(k-1) ! \quad \text { for } \quad k>0.$

the output tensor.

>>> 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)[源代码]#

the output tensor.

>>> 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')[源代码]#

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)[源代码]#

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

the output tensor.

>>> 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) – 输入张量。

>>> 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(0, 1)$.

the output tensor.

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