mindspore.ops.gamma

mindspore.ops.gamma(shape, alpha, beta, seed=None)[源代码]

根据伽马分布生成随机数。

参数:
  • shape (tuple) - 指定生成随机数的shape。任意维度的Tensor。

  • alpha (Tensor) - \(\alpha\) 分布的参数。应该大于0且数据类型为float32。

  • beta (Tensor) - \(\beta\) 分布的参数。应该大于0且数据类型为float32。

  • seed (int) - 随机数生成器的种子,必须是非负数,默认为None,将视为0。

返回:

Tensor。shape是输入 shapealphabeta 广播后的shape。数据类型为float32。

异常:
  • TypeError - shape 不是tuple。

  • TypeError - alphabeta 不是Tensor。

  • TypeError - seed 的数据类型不是int。

  • TypeError - alphabeta 的数据类型不是float32。

支持平台:

Ascend

样例:

>>> from mindspore import Tensor, ops
>>> import mindspore
>>> # case 1: alpha_shape is (2, 2)
>>> shape = (3, 1, 2)
>>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([1.0]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 2)
>>> # case 2: alpha_shape is (2, 3), so shape is (3, 1, 3)
>>> shape = (3, 1, 3)
>>> alpha = Tensor(np.array([[1, 3, 4], [2, 5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([1.0]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 3)
>>> # case 3: beta_shape is (1, 2), the output is different.
>>> shape = (3, 1, 2)
>>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([1.0, 2]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(output)
[[[ 2.2132034  5.8855834]]
 [ 3.3981476  7.5805717]
[[ 3.3981476  7.5805717]]
 [ 3.7190282 19.941492]
[[ 2.9512358  2.5969937]]
 [ 3.786061   5.160872 ]]]
>>> # case 4: beta_shape is (2, 1), the output is different.
>>> shape = (3, 1, 2)
>>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([[1.0], [2.0]]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(output)
[[[ 5.6085486  7.8280783]]
 [ 15.97684  16.116285]
[[ 1.8347423  1.713663]]
 [ 3.2434065 15.667398]
[[ 4.2922077  7.3365674]]
 [ 5.3876944  13.159832 ]]]