paddle.fluid.layers.unique_with_counts(x, dtype='int32')[source]

This OP return a unique tensor for x , and count tensor that the count of unqiue result in raw input, and an index tensor pointing to this unique tensor.

NOTICE: This op just be supported in device of CPU, and support the variable type of Tensor only.

  • x (Variable) – A 1-D input tensor with input shape of \([N]\) , the input data type is float32, float64, int32, int64.

  • dtype (np.dtype|core.VarDesc.VarType|str) – The type of count and index tensor, it could be int32, int64. Defalut value is int32.


The out is unique tensor for input x, the data shape is \([K]\), the K may be different to the N in shape of x. index is an index tensor pointing to out, the data shape is \([N]\) , the data shape is the same as input x. count is count of unqiue element in the x, the data shape is \([K]\), the data shape is the same as output out.

Return type

tuple, the variable type in tuple is Tensor, the output out data type is the same as input x, and data type of output index and count will be int32 or int64.


 import numpy as np
 import paddle.fluid as fluid
 x = fluid.layers.assign(np.array([2, 3, 3, 1, 5, 3], dtype='int32'))
 out, index, count = fluid.layers.unique_with_counts(x) # out is [2, 3, 1, 5]; index is [0, 1, 1, 2, 3, 1]
                                            # count is [1, 3, 1, 1]
# x.shape=(6,) out.shape=(4,), index.shape=(6,), count.shape=(4,)