【问题标题】:How to Argsort in Tensorflow?如何在 Tensorflow 中进行 Argsort 排序?
【发布时间】:2017-06-18 12:16:54
【问题描述】:

如何沿第二轴对 25 x 5 x 5 矩阵(张量)进行 argsort 排序?本质上,我正在寻找与 numpy 的 argsort 等效的 tensorflow(函数或方法),例如np.argsort(matrix, 2).

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    在您的情况下,您可能会使用top_k,它返回最高的k 值。 k 可以是一维向量,定义每个维度“顶部”有多少个值。在您的情况下,如果您想要第二个轴集 k=[0, 5, 0] 可能会这样做。

    tf.nn.top_k(matrix, k=[0,5,0], sorted=True)
    

    我没有运行它。 希望这会有所帮助

    【讨论】:

      【解决方案2】:

      作为参考,Tensorflow 现在支持tf.argsort

      示例

      import tensorflow as tf
      
      
      tensor = tf.constant(
          [
              [8, 7, 11],
              [5, 3, 4],
              [17, 33, 23],
          ]
      )
      
      arg_sort_op = tf.argsort(tensor, axis=-1)
      
      with tf.Session() as sess:
          out = sess.run(arg_sort_op)
      
      print(out)
      
      #  [[1 0 2]
      #   [1 2 0]
      #   [0 2 1]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-06
        • 2021-03-14
        • 2017-04-08
        • 2015-10-12
        • 2012-01-16
        • 1970-01-01
        • 2017-07-29
        相关资源
        最近更新 更多