unique_with_counts¶
- 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.
- Parameters
-
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.
- Returns
-
tuple, the variable type in tuple is Tensor, the output
out
data type is the same as inputx
, and data type of outputindex
andcount
will be int32 or int64.: Theout
is unique tensor for inputx
,the data shape is \([K]\), the K may be different to the N in shape ofx
.index
is an index tensor pointingtoout
, the data shape is \([N]\) , the data shape is the same as inputx
.count
is count of unique element inthex
, the data shape is \([K]\), the data shape is the same as outputout
.
Examples
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,)