在TensorFlow中对数组排序 [英] Sorting an Array in TensorFlow

查看:263
本文介绍了在TensorFlow中对数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在TensorFlow中有一个数组:

Let's assume I have an array in TensorFlow:

[ 0.12300211,  0.51767069,  0.13886075,  0.55363625],
[ 0.47279349,  0.50432992,  0.48080254,  0.51576483],
[ 0.84347934,  0.44505221,  0.88839239,  0.48857492],
[ 0.93650454,  0.43652734,  0.96464157,  0.47236174], ..

我想按第三列对该数组进行排序.我该怎么做呢?我可以使用tf.nn.top_k()分别对每一列进行排序,这为我提供了排序后的值和相应的索引.我可以使用第三列的索引对其他列进行重新排序,但是找不到重新排序的操作.

I would like to sort this array by the third column. How do I do this? I am able to sort each column individually using tf.nn.top_k(), which gives me the sorted values and the respective indices. I could use the indices of this third column to reorder the others, but I cannot find a reordering Op.

假设我想将事情保持在图表中(没有Python恶作剧):

Assuming I want to keep things in-graph (no Python shenanigans):

  • 如何在TensorFlow中排序(上述数组)?
  • 当我有要重新排序的索引时,如何在TensorFlow中重新排序?

推荐答案

以下作品:

a = tf.constant(...) # the array
reordered = tf.gather(a, tf.nn.top_k(a[:, 2], k=4).indices)

这篇关于在TensorFlow中对数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆