megengine.random.gamma¶
- gamma(shape, scale=1, size=None)¶
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.\]- 参数
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: 1size (
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
- 返回
the output tensor.
实际案例
import megengine as mge import megengine.random as rand x = rand.gamma(shape=2, scale=1, size=(2, 2)) print(x.numpy()) shape = mge.Tensor([[ 1], [10]], dtype="float32") scale = mge.Tensor([1,5], dtype="float32") x = rand.gamma(shape=shape, scale=scale) print(x.numpy()) x = rand.gamma(shape=shape, scale=scale, size=2) print(x.numpy())
Outputs:
[[1.5064533 4.0689363 ] [0.71639484 1.4551026 ]] [[ 0.4352188 11.399335 ] [ 9.1888 52.009277 ]] [[[ 1.1726005 3.9654975 ] [13.656933 36.559006 ]] [[ 0.25848487 2.5540342 ] [11.960409 21.031536 ]]]