本文介绍了张量流中二维数组从最小值到最大值的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组
x1 = tf.Variable([[0.51, 0.52, 0.53, 0.94, 0.35],
[0.32, 0.72, 0.83, 0.74, 0.55],
[0.23, 0.72, 0.63, 0.64, 0.35],
[0.11, 0.02, 0.03, 0.14, 0.15],
[0.01, 0.72, 0.73, 0.04, 0.75]],tf.float32)
我想对从最小到最大的每一行中的元素进行排序.有什么功能吗?
I want to sort the elements in each row from min to max. Is there any function for doing such ?
在这里的示例中,他们使用tf.nn.top_k
2d数组,我可以循环创建最大到最小.
In the example here they are using tf.nn.top_k
2d array,using this I can loop to create the max to min.
def sort(instance):
sorted = []
rows = tf.shape(instance)[0]
col = tf.shape(instance)[1]
for i in range(rows.eval()):
matrix.append([tf.gather(instance[i], tf.nn.top_k(instance[i], k=col.eval()).indices)])
return matrix
是否有类似的事情可以找到最小值到最大值或如何反转每一行中的数组?
Is there any thing similar for finding the min to max or how to reverse the array in each row ?
推荐答案
按照@Yaroslav的建议,您可以只使用top_k
值.
As suggested by @Yaroslav you can just use the top_k
values.
a = tf.Variable([[0.51, 0.52, 0.53, 0.94, 0.35],
[0.32, 0.72, 0.83, 0.74, 0.55],
[0.23, 0.72, 0.63, 0.64, 0.35],
[0.11, 0.02, 0.03, 0.14, 0.15],
[0.01, 0.72, 0.73, 0.04, 0.75]],tf.float32)
row_size = a.get_shape().as_list()[-1]
top_k = tf.nn.top_k(-a, k=row_size)
sess.run(-top_k.values)
这为我打印
array([[ 0.34999999, 0.50999999, 0.51999998, 0.52999997, 0.94 ],
[ 0.31999999, 0.55000001, 0.72000003, 0.74000001, 0.82999998],
[ 0.23 , 0.34999999, 0.63 , 0.63999999, 0.72000003],
[ 0.02 , 0.03 , 0.11 , 0.14 , 0.15000001],
[ 0.01 , 0.04 , 0.72000003, 0.73000002, 0.75 ]], dtype=float32)
这篇关于张量流中二维数组从最小值到最大值的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!