【问题标题】:Sorting an Array in TensorFlow在 TensorFlow 中对数组进行排序
【发布时间】:2017-04-08 16:06:47
【问题描述】:

假设我在 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() 单独对每一列进行排序,这为我提供了排序值和相应的索引。我可以使用第三列的索引重新排序其他列,但我找不到重新排序的操作。

假设我想把东西保存在图中(没有 Python 恶作剧):

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

【问题讨论】:

  • 如果我理解您的问题,这似乎是open issue。还没有,但更糟的是最坏的情况,扁平化和top_k
  • 如果想按第三列排序,或者用索引列表重新排序,这将不起作用。
  • 您可以使用tf.gather 重新订购吗?可能吗?
  • 嘿,这是一个不错的发现。我去看看能不能把top_ktf.gather组合起来
  • tf.gather 的文档现在似乎是 here

标签: python arrays sorting tensorflow


【解决方案1】:

以下作品:

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

【讨论】:

    猜你喜欢
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2011-04-23
    • 2016-06-28
    • 2012-02-14
    • 2013-12-24
    • 2013-08-05
    相关资源
    最近更新 更多