mindspore.ops.random_categorical

mindspore.ops.random_categorical(logits, num_sample, seed=0, dtype=mstype.int64)[source]

Generates random samples from a given categorical distribution tensor.

Parameters
  • logits (Tensor) – The input tensor. 2-D Tensor with shape \((batch\_size, num\_classes)\).

  • num_sample (int) – Number of sample to be drawn. Only constant values is allowed.

  • seed (int) – Random seed. Only constant values is allowed. Default: 0.

  • dtype (mindspore.dtype) – The type of output. Its value must be one of mindspore.int16, mindspore.int32 and mindspore.int64. Default: mstype.int64.

Returns

Tensor, The output Tensor with shape \((batch\_size, num\_samples)\).

Raises
  • TypeError – If dtype is not one of the following: mindspore.int16, mindspore.int32, mindspore.int64.

  • TypeError – If logits is not a Tensor.

  • TypeError – If neither num_sample nor seed is an int.

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import ops
>>> from mindspore import Tensor
>>> import mindspore.common.dtype as mstype
>>> import numpy as np
>>> logits = Tensor(np.random.random((10, 5)).astype(np.float32), mstype.float32)
>>> net = ops.random_categorical(logits, 8)
>>> result = net.shape
>>> print(result)
(10, 8)