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.$

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

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

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

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 !},$

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