访问量: 10 次浏览
tensorflow.argsort() 方法TensorFlow 是谷歌设计的开源python库,用于开发机器学习模型和深度学习神经网络。Tensorflow 有一个方法 argsort() ,用于查找排序后的张量的索引。
语法:
tf.argsort(values, axis, direction, stable, name)
参数:
返回:
它返回一个int32类型的张量,其形状与数值相同。这个张量包含的索引将给出给定数值的排序顺序。
如果轴或方向无效,将引发ValueError._
示例 1:
# 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])
输出:
Indices: tf.Tensor([0 2 1 4 5 3], shape=(6,), dtype=int32)
Sorted Values
1
2.5
5
7
8.5
10
例子2:
在这个例子中,错误的值被传递给了方向。这将引发ValueError。
# 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)
输出:
ValueError: ASC should be one of ASCENDING, DESCENDING