unique_consecutive

paddle. unique_consecutive ( x, return_inverse=False, return_counts=False, axis=None, dtype='int64', name=None ) [source]

Eliminates all but the first element from every consecutive group of equivalent elements.

Note

This function is different from unique in the sense that this function only eliminates consecutive duplicate values. This semantics is similar to unique in C++.

Parameters
  • x (Tensor) – the input tensor, it’s data type should be float32, float64, int32, int64.

  • return_inverse (bool, optional) – If True, also return the indices for where elements in the original input ended up in the returned unique consecutive tensor. Default is False.

  • return_counts (bool, optional) – If True, also return the counts for each unique consecutive element. Default is False.

  • axis (int, optional) – The axis to apply unique consecutive. If None, the input will be flattened. Default is None.

  • dtype (np.dtype|str, optional) – The data type inverse tensor: int32 or int64. Default: int64.

  • name (str, optional) – Name for the operation. For more information, please refer to Name. Default is None.

Returns

  • out (Tensor), the unique consecutive tensor for x.

  • inverse (Tensor), the element of the input tensor corresponds to

    the index of the elements in the unique consecutive tensor for x. inverse is provided only if return_inverse is True.

  • counts (Tensor), the counts of the every unique consecutive element in the input tensor.

    counts is provided only if return_counts is True.

Examples

>>> import paddle

>>> x = paddle.to_tensor([1, 1, 2, 2, 3, 1, 1, 2])
>>> output = paddle.unique_consecutive(x) #
>>> print(output)
Tensor(shape=[5], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 2, 3, 1, 2])

>>> _, inverse, counts = paddle.unique_consecutive(x, return_inverse=True, return_counts=True)
>>> print(inverse)
Tensor(shape=[8], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 0, 1, 1, 2, 3, 3, 4])
>>> print(counts)
Tensor(shape=[5], dtype=int64, place=Place(cpu), stop_gradient=True,
 [2, 2, 1, 2, 1])

>>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]])
>>> output = paddle.unique_consecutive(x, axis=0) #
>>> print(output)
Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[2, 1, 3],
 [3, 0, 1],
 [2, 1, 3]])

>>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]])
>>> output = paddle.unique_consecutive(x, axis=0) #
>>> print(output)
Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[2, 1, 3],
 [3, 0, 1],
 [2, 1, 3]])