oneflow.sort

oneflow.sort()

Sorts the elements of the input tensor along a given dimension in ascending order by value.

Parameters
  • input (oneflow.Tensor) – the input Tensor.

  • dim (int, optional) – the dimension to be sorted. Defaults to the last dim (-1).

  • descending (bool, optional) – controls the sorting order (ascending or descending).

Returns

A tuple of (values, indices), where where the values are the sorted values and the indices are the indices of the elements in the original input tensor.

Return type

Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int32))

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> x = np.array([[1, 3, 8, 7, 2], [1, 9, 4, 3, 2]], dtype=np.float32)
>>> input = flow.Tensor(x)
>>> result = flow.sort(input)
>>> result.values
tensor([[1., 2., 3., 7., 8.],
        [1., 2., 3., 4., 9.]], dtype=oneflow.float32)
>>> result.indices
tensor([[0, 4, 1, 3, 2],
        [0, 4, 3, 2, 1]], dtype=oneflow.int32)
>>> result = flow.sort(input, descending=True)
>>> result.values
tensor([[8., 7., 3., 2., 1.],
        [9., 4., 3., 2., 1.]], dtype=oneflow.float32)
>>> result.indices
tensor([[2, 3, 1, 4, 0],
        [1, 2, 3, 4, 0]], dtype=oneflow.int32)
>>> result = flow.sort(input, dim=0)
>>> result.values
tensor([[1., 3., 4., 3., 2.],
        [1., 9., 8., 7., 2.]], dtype=oneflow.float32)
>>> result.indices
tensor([[0, 0, 1, 1, 0],
        [1, 1, 0, 0, 1]], dtype=oneflow.int32)