mindspore.ops.multinomial_with_replacement

mindspore.ops.multinomial_with_replacement(x, seed, offset, numsamples, replacement=False)[source]

Returns a tensor where each row contains numsamples indices sampled from the multinomial distribution with replacement. It is different from multinomial in that it allows the same outcome to be chosen multiple times.

Note

The rows of input do not need to sum to one (in which case we use the values as weights), but must be non-negative, finite and have a non-zero sum.

Parameters
  • x (Tensor) – the input tensor containing the cumsum of probabilities, must be 1 or 2 dimensions. Must be one of the following types: float16, float32, float64.

  • seed (int) – If seed is set to be -1, and offset is set to be 0, the random number generator is seeded by a random seed. Otherwise, it is seeded by the given seed.

  • offset (int) – Offset used to avoid seed collision.

  • numsamples (int) – the number of samples to draw.

  • replacement (bool, optional) – Whether to draw with replacement or not. Default: False .

Returns

Tensor with the same rows as x, each row has numsamples sampled indices.

Raises
  • TypeError – If x is not a 1D or 2D Tensor.

  • TypeError – If dtype of x is not float16, float32 or float64.

  • TypeError – If numsamples is not an int.

  • TypeError – If replacement is not a bool.

  • ValueError – If the value of numsamples is not greater than x_shape[-1] when replacement is False.

  • ValueError – If the sum of one row of x less than 0.

  • ValueError – If one of the element of each row of x less than 0.

  • ValueError – If numsamples equal or less than 0.

Supported Platforms:

CPU

Examples

>>> from mindspore import Tensor, ops
>>> from mindspore import dtype as mstype
>>> x = Tensor([[0., 9., 4., 0.]], mstype.float32)
>>> output = ops.multinomial_with_replacement(x, 2, 5, 2, True)
>>> print(output)
[[1 1]]