tf.argsort

View source on GitHub

Returns the indices of a tensor that give its sorted order along an axis.

tf.argsort(
    values, axis=-1, direction='ASCENDING', stable=False, name=None
)

For a 1D tensor, tf.gather(values, tf.argsort(values)) is equivalent to tf.sort(values). For higher dimensions, the output has the same shape as values, but along the given axis, values represent the index of the sorted element in that slice of the tensor at the given position.

Usage:

import tensorflow as tf
a = [1, 10, 26.9, 2.8, 166.32, 62.3]
b = tf.argsort(a,axis=-1,direction='ASCENDING',stable=False,name=None)
c = tf.keras.backend.eval(b)
# Here, c = [0 3 1 2 5 4]

Args:

Returns:

An int32 Tensor with the same shape as values. The indices that would sort each slice of the given values along the given axis.

Raises: