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

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

NOTICE: This op 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.


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.: 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 pointingto out, the data shape is \([N]\) , the data shape is the same as input x. count is count of unique element inthe x, the data shape is \([K]\), the data shape is the same as output out.


 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,)