mindspore.ops.unique_consecutive

View Source On Gitee
mindspore.ops.unique_consecutive(input, return_inverse=False, return_counts=False, dim=None)[source]

Remove consecutive duplicate elements in the input tensor, retaining only the first occurrence from each repeated group.

Parameters
  • input (Tensor) – The input tensor.

  • return_inverse (bool, optional) – Whether to also return the indices for where elements in the original input ended up in the returned unique list. Default False .

  • return_counts (bool, optional) – Whether to also return the counts for each unique element. Default False .

  • dim (int, optional) – Specify the dimension for unique. Default None , the input tensor will be flattened.

Returns

Tensor or tuple(output, inverse_indices, counts) of tensors.

  • output (Tensor) - The deduplicated output tensor.

  • inverse_indices (Tensor, optional) - The indices of the elements of the input tensor in the output .

  • counts (Tensor, optional) - The counts for each unique element.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> x = mindspore.tensor([1, 1, 2, 2, 3, 1, 1, 2], mindspore.int32)
>>> output, inverse_indices, counts = mindspore.ops.unique_consecutive(x, True, True, None)
>>> print(output)
[1 2 3 1 2]
>>> print(inverse_indices)
[0 0 1 1 2 3 3 4]
>>> print(counts)
[2 2 1 2 1]