我想对一维张量进行排序,这样张量就会根据张量中项目的频率排序,比如给定这个张量[6,0,1,0,2,6,6],我想这样排序[6,6,6,0,0,1,2],目前为止我已经做到了:
ks = tf.constant([6,0,1,0,2,6,6])
unique_s, idx, cnts = tf.unique_with_counts(ks)
r = tf.gather(unique_s,tf.nn.top_k(cnts, k=(1+tf.reduce_max(idx))).indices)
s = tf.gather(cnts,tf.nn.top_k(cnts, k=(1+tf.reduce_max(idx))).indices)
其中r包含值[6,0,1,2],s包含[3,2,1,1]。现在,我想根据s中的计数展开r,所以在Python中,我们可以将上面的列表设置为:
^{pr2}$但由于张量流中不允许迭代张量,我现在有点卡住了。在
我相当肯定有一个更优雅的解决方案,但这里是:
这需要运行两个会话:
代码:
输出
[6 6 6 0 0 1 2]
相关问题 更多 >
编程相关推荐