megengine.random.gamma

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>0.\]
参数
  • shape (Union[Tensor, float]) – 分布的形状参数(有时称为“k”)。必须是非负数。

  • scale (Union[Tensor, float]) – 分布的尺度参数(有时称为“theta”)。必须是非负数。默认值:1

  • size (Optional[Iterable[int]]) – 输出向量的大小。如果 shape 和 scale 是标量,例如给定大小为 (m, n),则输出形状为 (m, n)。如果 shape 和 scale 是一个向量,例如给定的大小是`(m, n)` ,那么输出形状是 (m, n) + broadcast(shape, scale).shape. 广播规则与 numpy.broadcast 一致。默认值:None

返回

输出张量

实际案例

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