RNG#

class RNG(seed=None, device=None)[source]#

RNG exposes a number of methods for generating random numbers.

Parameters:
  • seed (Optional[int]) – random seed used to initialize the pseudo-random number generator. Default: None

  • device (Optional[str]) – the device of generated tensor. Default: None

Examples

>>> 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)[source]#

Random variable with Beta distribution \(\operatorname{Beta}(\alpha, \beta)\).

The corresponding probability density function is

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

where \(\mathrm{~B}(\alpha, \beta)\) is the beta function,

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

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

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

Returns:

the output tensor.

Examples

>>> 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)[source]#

Random variable with Gamma distribution \(\Gamma(k, \theta)\).

The corresponding probability density function is

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

where \(\Gamma(k)\) is the gamma function,

\[\Gamma(k)=(k-1) ! \quad \text { for } \quad k>0.\]
Parameters:
  • shape (Union[Tensor, float]) – the shape parameter (sometimes designated “k”) of the distribution. Must be non-negative.

  • scale (Union[Tensor, float]) – the scale parameter (sometimes designated “theta”) of the distribution. Must be non-negative. 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

Returns:

the output tensor.

Examples

>>> 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)[source]#

Random variable with Gaussian distribution \(N(\mu, \sigma)\).

Parameters:
  • 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

Returns:

the output tensor.

Examples

>>> 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')[source]#
Randomly permute a sequence, or return a permuted range.

If n is a multi-dimensional tensor, it is only shuffled along its first index.

Parameters:
  • n (Union[int, Tensor]) – If n is an integer, random permutation of integers from \(0\) to \(n - 1\). If n is an tensor, make a copy and shuffle the elements randomly.

  • dtype (str) – the output data type when n is an integer. int32, int16 and float32 are supported. Default: int32

Returns:

the output tensor.

Examples

>>> 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)[source]#

Random variable with poisson distribution \(\operatorname{Poisson}(\lambda)\).

The corresponding probability density function is

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

where k is the number of occurrences \(({\displaystyle k=0,1,2...})\).

Parameters:
  • lam (Union[float, Tensor]) – the lambda parameter of the distribution. Must be non-negative.

  • size (Optional[Iterable[int]]) – the size of output tensor. If lam is a scalar and given size is, e.g., (m, n), then the output shape is (m, n). If lam is a Tensor with shape (k, v) and given size is, e.g., (m, n), then the output shape is (m, n, k, v). Default: None.

Returns:

the output tensor.

Examples

>>> 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)[source]#

Modify a sequence in-place by shuffling its contents. This function only shuffles the Tensor along the first axis of a multi-dimensional Tensor. The order of sub-Tensors is changed but their contents remains the same.

Parameters:

inp (Tensor) – input tensor.

Examples

>>> 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)[source]#

Random variable with uniform distribution $U(0, 1)$.

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

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

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

Returns:

the output tensor.

Examples

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