Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow中2d数组从最小值到最大值的排序_Python_Arrays_Tensorflow - Fatal编程技术网

Python tensorflow中2d数组从最小值到最大值的排序

Python tensorflow中2d数组从最小值到最大值的排序,python,arrays,tensorflow,Python,Arrays,Tensorflow,我有一个数组 x1 = tf.Variable([[0.51, 0.52, 0.53, 0.94, 0.35], [0.32, 0.72, 0.83, 0.74, 0.55], [0.23, 0.72, 0.63, 0.64, 0.35], [0.11, 0.02, 0.03, 0.14, 0.15], [0.01, 0.72, 0.73, 0.04, 0.75]],tf.float32)

我有一个数组

x1 = tf.Variable([[0.51, 0.52, 0.53, 0.94, 0.35],
             [0.32, 0.72, 0.83, 0.74, 0.55],
             [0.23, 0.72, 0.63, 0.64, 0.35],
             [0.11, 0.02, 0.03, 0.14, 0.15],
             [0.01, 0.72, 0.73, 0.04, 0.75]],tf.float32)
我想将每行中的元素从最小值排序到最大值。是否有这样的功能

在这里的示例中,他们使用
tf.nn.top_k
,使用这个我可以循环创建最大到最小值

def sort(instance):
   sorted = []
   rows = tf.shape(instance)[0]
   col = tf.shape(instance)[1]
   for i in range(rows.eval()):
       matrix.append([tf.gather(instance[i], tf.nn.top_k(instance[i], k=col.eval()).indices)])
   return matrix

在查找最小到最大值或如何反转每行中的数组时,是否有类似的方法?

正如@Yaroslav所建议的,您可以使用
top\k

a = tf.Variable([[0.51, 0.52, 0.53, 0.94, 0.35],
             [0.32, 0.72, 0.83, 0.74, 0.55],
             [0.23, 0.72, 0.63, 0.64, 0.35],
             [0.11, 0.02, 0.03, 0.14, 0.15],
             [0.01, 0.72, 0.73, 0.04, 0.75]],tf.float32)

row_size = a.get_shape().as_list()[-1]
top_k = tf.nn.top_k(-a, k=row_size)
sess.run(-top_k.values)
这是我的指纹

array([[ 0.34999999,  0.50999999,  0.51999998,  0.52999997,  0.94      ],
       [ 0.31999999,  0.55000001,  0.72000003,  0.74000001,  0.82999998],
       [ 0.23      ,  0.34999999,  0.63      ,  0.63999999,  0.72000003],
       [ 0.02      ,  0.03      ,  0.11      ,  0.14      ,  0.15000001],
       [ 0.01      ,  0.04      ,  0.72000003,  0.73000002,  0.75      ]], dtype=float32)

-tf.nn.top_k(-a)
为您提供了反转的ID u平均值tf.gather(-instance[i],tf.nn.top_k(-instance[i],k=col.eval()).index),我得到了类型错误:一元操作数类型错误,因为
top_k
返回两个输出,只需对其中一个进行求反