# 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 `paddle.unique()` in the sense that this function only eliminates consecutive duplicate values. This semantics is similar to std::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, inverse, counts). out is the unique consecutive tensor for x. inverse is provided only if return_inverse is True. counts is provided only if return_counts is True.

Return type

tuple

Example

```import paddle

x = paddle.to_tensor([1, 1, 2, 2, 3, 1, 1, 2])
print(output)
#        [1, 2, 3, 1, 2])

_, inverse, counts = paddle.unique_consecutive(x, return_inverse=True, return_counts=True)
print(inverse)
#        [0, 0, 1, 1, 2, 3, 3, 4])
print(counts)
#        [2, 2, 1, 2, 1])

x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]])
print(output)
# Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), 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]])