mindspore.ops.RandomCategorical

View Source On Gitee
class mindspore.ops.RandomCategorical(dtype=mstype.int64)[source]

Generates random samples from a given categorical distribution tensor.

Parameters

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

Inputs:
  • 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. Default: 0 . Only constant values is allowed.

Outputs:
  • output (Tensor) - The output Tensor with shape \((batch\_size, num\_samples)\).

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

  • TypeError – If logits is not a Tensor.

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

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import nn, ops, Tensor
>>> class Net(nn.Cell):
...   def __init__(self, num_sample):
...     super(Net, self).__init__()
...     self.random_categorical = ops.RandomCategorical(mindspore.int64)
...     self.num_sample = num_sample
...   def construct(self, logits, seed=0):
...     return self.random_categorical(logits, self.num_sample, seed)
...
>>> x = np.random.random((10, 5)).astype(np.float32)
>>> net = Net(8)
>>> output = net(Tensor(x))
>>> result = output.shape
>>> print(result)
(10, 8)