【问题标题】:Sort a tensor based on two columns in tensorflow根据张量流中的两列对张量进行排序
【发布时间】:2018-08-30 03:38:38
【问题描述】:

是否可以根据 Tensorflow 中两列中的值对张量进行排序?

例如,假设我有以下张量。

[[1,2,3]
[2,3,5]
[1,4,6]
[2,2,1]
[0,4,2]]

我愿意 喜欢它首先根据第一列然后第二列进行排序。排序后如下所示。

[[0,4,2]
[1,2,3]
[1,4,6]
[2,2,1]
[2,3,5]]

有没有办法使用 tensorflow 来实现这一点?我能够根据单个列进行排序。但是基于两列的排序对我来说是个问题。请问有人可以帮忙吗?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    一个非常幼稚的方法,

    import tensorflow as tf
    
    a = tf.constant([[1, 2, 3],
                     [2, 3, 5],
                     [1, 4, 6],
                     [2, 2, 1],
                     [0, 4, 2]])
    
    # b = a[:0]*10 + a[:1]*1 -- > (e.g 1*10+2*1 =12)
    b = tf.add(tf.slice(a, [0, 0], [-1, 1]) * 10, tf.slice(a, [0, 1], [-1, 1]))
    
    reordered = tf.gather(a, tf.nn.top_k(b[:, 0], k=5, sorted=False).indices)
    reordered = tf.reverse(reordered, axis=[0])
    
    with tf.Session() as sess:
        result = sess.run(reordered)
        print(result)
    

    希望这会有所帮助。

    【讨论】:

    • 好黑客:)。它可以在某些情况下使用,尽管它不是理想的解决方案。还是非常感谢。
    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2021-07-08
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2018-12-28
    相关资源
    最近更新 更多