mindspore.ops.gamma

mindspore.ops.gamma(shape, alpha, beta, seed=None)[source]

Generates random numbers according to the Gamma random number distribution.

Support broadcasting.

Warning

  • The Ascend backend does not support the reproducibility of random numbers, so the seed parameter has no effect.

  • Starting after version 2.9.0, this API will undergo an incompatible change: this sampling interface will be changed to a distribution object form. Please use the corresponding distribution class instead.

Parameters
  • shape (tuple) – The shape specified.

  • alpha (Tensor) – The shape parameter.

  • beta (Tensor) – The inverse scale parameter.

  • seed (int, optional) – The random seed, Default None .

Returns

Tensor

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> # case 1: alpha_shape is (2, 2)
>>> shape = (3, 1, 2)
>>> alpha = mindspore.tensor([[3, 4], [5, 6]], mindspore.float32)
>>> beta = mindspore.tensor([1.0], mindspore.float32)
>>> output = mindspore.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 = mindspore.tensor([[1, 3, 4], [2, 5, 6]], mindspore.float32)
>>> beta = mindspore.tensor([1.0], mindspore.float32)
>>> output = mindspore.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 = mindspore.tensor([[3, 4], [5, 6]], mindspore.float32)
>>> beta = mindspore.tensor([1.0, 2], mindspore.float32)
>>> output = mindspore.ops.gamma(shape, alpha, beta, seed=5)
>>> print(output)
[[[ 2.2132034  5.8855834]
  [ 3.8825176  8.6066265]]
 [[ 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 = mindspore.tensor([[3, 4], [5, 6]], mindspore.float32)
>>> beta = mindspore.tensor([[1.0], [2.0]], mindspore.float32)
>>> output = mindspore.ops.gamma(shape, alpha, beta, seed=5)
>>> 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 ]]]