|
|
|
@ -612,6 +612,7 @@ def unique(x,
|
|
|
|
|
return_inverse=False,
|
|
|
|
|
return_counts=False,
|
|
|
|
|
axis=None,
|
|
|
|
|
dtype="int64",
|
|
|
|
|
name=None):
|
|
|
|
|
"""
|
|
|
|
|
Returns the unique elements of `x` in ascending order.
|
|
|
|
@ -625,6 +626,8 @@ def unique(x,
|
|
|
|
|
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
|
|
|
|
|
:ref:`api_guide_Name`. Default: None.
|
|
|
|
|
|
|
|
|
@ -650,6 +653,7 @@ def unique(x,
|
|
|
|
|
np_counts = counts.numpy() # [1 1 3 1]
|
|
|
|
|
|
|
|
|
|
x_data = np.array([[2, 1, 3], [3, 0, 1], [2, 1, 3]])
|
|
|
|
|
x = paddle.to_tensor(x_data)
|
|
|
|
|
unique = paddle.unique(x)
|
|
|
|
|
np_unique = unique.numpy() # [0 1 2 3]
|
|
|
|
|
|
|
|
|
@ -662,11 +666,10 @@ def unique(x,
|
|
|
|
|
axis = []
|
|
|
|
|
else:
|
|
|
|
|
axis = [axis]
|
|
|
|
|
|
|
|
|
|
attr_dtype = convert_np_dtype_to_dtype_(dtype)
|
|
|
|
|
if in_dygraph_mode():
|
|
|
|
|
out, inverse, indices, counts = core.ops.unique(
|
|
|
|
|
x, 'dtype',
|
|
|
|
|
convert_np_dtype_to_dtype_('int32'), 'return_index', return_index,
|
|
|
|
|
x, 'dtype', attr_dtype, 'return_index', return_index,
|
|
|
|
|
'return_inverse', return_inverse, 'return_counts', return_counts,
|
|
|
|
|
'axis', axis, "is_sorted", True)
|
|
|
|
|
outs = [out]
|
|
|
|
@ -687,12 +690,13 @@ def unique(x,
|
|
|
|
|
check_type(return_index, 'return_index', bool, 'unique')
|
|
|
|
|
check_type(return_inverse, 'return_inverse', bool, 'unique')
|
|
|
|
|
check_type(return_counts, 'return_counts', bool, 'unique')
|
|
|
|
|
check_dtype(dtype, 'dtype', ['int32', 'int64'], 'unique')
|
|
|
|
|
if len(axis) != 0:
|
|
|
|
|
check_type(axis[0], 'axis', int, 'unique')
|
|
|
|
|
|
|
|
|
|
helper = LayerHelper('unique', **locals())
|
|
|
|
|
attrs = {
|
|
|
|
|
'dtype': int(core.VarDesc.VarType.INT32),
|
|
|
|
|
'dtype': attr_dtype,
|
|
|
|
|
"return_index": return_index,
|
|
|
|
|
"return_inverse": return_inverse,
|
|
|
|
|
"return_counts": return_counts,
|
|
|
|
@ -702,19 +706,19 @@ def unique(x,
|
|
|
|
|
out = helper.create_variable_for_type_inference(
|
|
|
|
|
dtype=x.dtype, stop_gradient=True)
|
|
|
|
|
inverse = helper.create_variable_for_type_inference(
|
|
|
|
|
dtype=core.VarDesc.VarType.INT64, stop_gradient=True)
|
|
|
|
|
dtype=attr_dtype, stop_gradient=True)
|
|
|
|
|
outputs = {"Out": out, "Index": inverse}
|
|
|
|
|
outs = [out]
|
|
|
|
|
if return_index:
|
|
|
|
|
indices = helper.create_variable_for_type_inference(
|
|
|
|
|
dtype=core.VarDesc.VarType.INT64, stop_gradient=True)
|
|
|
|
|
dtype=attr_dtype, stop_gradient=True)
|
|
|
|
|
outputs["Indices"] = indices
|
|
|
|
|
outs.append(indices)
|
|
|
|
|
if return_inverse:
|
|
|
|
|
outs.append(inverse)
|
|
|
|
|
if return_counts:
|
|
|
|
|
counts = helper.create_variable_for_type_inference(
|
|
|
|
|
dtype=core.VarDesc.VarType.INT64, stop_gradient=True)
|
|
|
|
|
dtype=attr_dtype, stop_gradient=True)
|
|
|
|
|
outputs["Counts"] = counts
|
|
|
|
|
outs.append(counts)
|
|
|
|
|
|
|
|
|
|