Friday, October 10, 2025
HomeLanguagesPython – tensorflow.argsort() method

Python – tensorflow.argsort() method

TensorFlow is open-source python library designed by Google to develop Machine Learning models  and deep learning  neural networks. Tensorflow have a method argsort() which is used to find the indices of a Tensor in sorted order.

Syntax: tf.argsort(values, axis, direction, stable, name)

Parameters:

  • values: It is a numeric Tensor of any dimension.
  • axis: It defines the axis along which shorting need to be done. If no value is given, default is -1 and sorting is done based on last axis.
  • direction: Either ASCENDING or DESCENDING.
  • stable: Either  True or False. If it’s true then in case of equality original order is maintained.
  • name: It’s an optional argument which define the name for the operation.

Return: It returns a Tensor of type int32 having same shape as values.This tensor contains the indices that will give the sorted order of given values.

If axis or direction is invalid it will raise ValueError.

Example 1:

Python3




# importing the library
import tensorflow
  
# initializing value
a= [1,5,2.5,10,7,8.5]
  
# getting the indices for sorted values
b = tensorflow.argsort(a,axis=-1,direction='ASCENDING',stable=False)
  
# printing the result
print('Indices:'b)
  
print('Sorted values')
#printing the sorted value
for i in b:
  print(a[i])


Output:

Indices: tf.Tensor([0 2 1 4 5 3], shape=(6,), dtype=int32)
Sorted Values
1
2.5
5
7
8.5
10

Example 2: In this example wrong value is passes to direction. This will raise ValueError.

Python3




# importing the library
import tensorflow
  
# initializing value
a= [1,5,2.5,10,7,8.5]
  
# getting the indices for sorted values
b = tensorflow.argsort(a,axis=-1,direction='ASC',stable=False)


Output:

ValueError: ASC should be one of ASCENDING, DESCENDING

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS