argsort

paddle. argsort ( x: Tensor, axis: int = -1, descending: bool = False, stable: bool = False, name: str | None = None ) Tensor [source]

Sorts the input along the given axis, and returns the corresponding index tensor for the sorted output values. The default sort algorithm is ascending, if you want the sort algorithm to be descending, you must set the descending as True.

Note

Alias Support: The parameter name input can be used as an alias for x, and the parameter name dim can be used as an alias for axis. For example, argsort(input=tensor_x, dim=1) is equivalent to (x=tensor_x, axis=1).

Parameters
  • x (Tensor) – An input N-D Tensor with type bfloat16, float16, float32, float64, int16, int32, int64, uint8. alias: input.

  • axis (int, optional) – Axis to compute indices along. The effective range is [-R, R), where R is Rank(x). when axis<0, it works the same way as axis+R. Default is -1. alias: dim.

  • descending (bool, optional) – Descending is a flag, if set to true, algorithm will sort by descending order, else sort by ascending order. Default is false.

  • stable (bool, optional) – Whether to use stable sorting algorithm or not. When using stable sorting algorithm, the order of equivalent elements will be preserved. Default is False.

  • name (str|None, optional) – For details, please refer to api_guide_Name. Generally, no setting is required. Default: None.

Returns

Tensor, sorted indices(with the same shape as x and with data type int64).

Examples

>>> import paddle

>>> x = paddle.to_tensor([[[5,8,9,5],
...                        [0,0,1,7],
...                        [6,9,2,4]],
...                       [[5,2,4,2],
...                        [4,7,7,9],
...                        [1,7,0,6]]],
...                      dtype='float32')
>>> out1 = paddle.argsort(x, axis=-1)
>>> out2 = paddle.argsort(x, axis=0)
>>> out3 = paddle.argsort(x, axis=1)

>>> print(out1)
Tensor(shape=[2, 3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[[0, 3, 1, 2],
  [0, 1, 2, 3],
  [2, 3, 0, 1]],
 [[1, 3, 2, 0],
  [0, 1, 2, 3],
  [2, 0, 3, 1]]])

>>> print(out2)
Tensor(shape=[2, 3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[[0, 1, 1, 1],
  [0, 0, 0, 0],
  [1, 1, 1, 0]],
 [[1, 0, 0, 0],
  [1, 1, 1, 1],
  [0, 0, 0, 1]]])

>>> print(out3)
Tensor(shape=[2, 3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[[1, 1, 1, 2],
  [0, 0, 2, 0],
  [2, 2, 0, 1]],
 [[2, 0, 2, 0],
  [1, 1, 0, 2],
  [0, 2, 1, 1]]])

>>> x = paddle.to_tensor([1, 0]*40, dtype='float32')
>>> out1 = paddle.argsort(x, stable=False)
>>> out2 = paddle.argsort(x, stable=True)

>>> print(out1)
Tensor(shape=[80], dtype=int64, place=Place(cpu), stop_gradient=True,
[55, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 1 , 57, 59, 61,
 63, 65, 67, 69, 71, 73, 75, 77, 79, 17, 11, 13, 25, 7 , 3 , 27, 23, 19,
 15, 5 , 21, 9 , 10, 64, 62, 68, 60, 58, 8 , 66, 14, 6 , 70, 72, 4 , 74,
 76, 2 , 78, 0 , 20, 28, 26, 30, 32, 24, 34, 36, 22, 38, 40, 12, 42, 44,
 18, 46, 48, 16, 50, 52, 54, 56])

>>> print(out2)
Tensor(shape=[80], dtype=int64, place=Place(cpu), stop_gradient=True,
[1 , 3 , 5 , 7 , 9 , 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35,
 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71,
 73, 75, 77, 79, 0 , 2 , 4 , 6 , 8 , 10, 12, 14, 16, 18, 20, 22, 24, 26,
 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62,
 64, 66, 68, 70, 72, 74, 76, 78])