unique

paddle.compat. unique ( input, sorted=True, return_inverse=False, return_counts=False, dim=None ) [source]

Returns the unique elements of input in ascending order.

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

  • sorted (bool, optional) – Does not affect the return result, same as PyTorch.

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

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

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

Returns

tuple (output, inverse_indices, counts). output is the unique tensor for input.

inverse_indices is provided only if return_inverse is True. counts is provided only if return_counts is True.

Examples

>>> import paddle

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

>>> _, inverse_indices, counts = paddle.compat.unique(x, return_inverse=True, return_counts=True)
>>> print(inverse_indices)
Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 2, 2, 0, 3, 2])
>>> print(counts)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 1, 3, 1])

>>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3]])
>>> unique = paddle.compat.unique(x)
>>> print(unique)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 1, 2, 3])

>>> unique = paddle.compat.unique(x, dim=0)
>>> print(unique)
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[2, 1, 3],
 [3, 0, 1]])