mindspore.ops.choice_with_mask

View Source On Gitee
mindspore.ops.choice_with_mask(input_x, count=256, seed=None)[source]

Generates a random sample as index tensor with a mask tensor from a given tensor.

The input_x must be a tensor whose dimension is not less than 1. If its dimension is greater than or equal to 2, the first dimension specifies the number of samples. The returned index tensor denotes the index of the nonzero sample, the mask tensor denotes which elements in the index tensor are valid.

Parameters
  • input_x (Tensor[bool]) – The input tensor. The input tensor rank must be greater than or equal to 1 and less than or equal to 5.

  • count (int, optional) – Number of items expected to get and the number must be greater than 0. Default: 256 .

  • seed (int, optional) – Seed is used as entropy source for Random number engines generating pseudo-random numbers. Default: None , which will be treated as 0.

Returns

Two tensors, the first one is the index tensor and the other one is the mask tensor.

  • index (Tensor) - The output shape is 2-D.

  • mask (Tensor) - The output shape is 1-D.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input_x = Tensor(np.ones(shape=[240000, 4]).astype(np.bool_))
>>> output_y, output_mask = ops.choice_with_mask(input_x)
>>> result = output_y.shape
>>> print(result)
(256, 2)
>>> result = output_mask.shape
>>> print(result)
(256,)