unique

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

Returns the unique elements of x in ascending order.

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

  • return_index (bool, optional) – If True, also return the indices of the input tensor that result in the unique Tensor.

  • 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.

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

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

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

Returns

tuple (out, indices, inverse, counts). out is the unique tensor for x. indices is

provided only if return_index is True. inverse 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.unique(x)
>>> print(unique)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 2, 3, 5])

>>> _, indices, inverse, counts = paddle.unique(x, return_index=True, return_inverse=True, return_counts=True)
>>> print(indices)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[3, 0, 1, 4])
>>> print(inverse)
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.unique(x)
>>> print(unique)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 1, 2, 3])

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