神经网络学习-tensorflow2.0-张量的排序
1.tf.sort(tensor,direction='DESCENDING'):将tensor的每一行元素按升序或倒序排列,有direction='DESCENDING'时为降序,不写时为升序
example:
input:
import tensorflow as tf
a=tf.random.shuffle(tf.range(5))
b=tf.sort(a)
c=tf.sort(a,direction='DESCENDING')
tf.print(a)
tf.print(b)
tf.print(c)
output:
[0 3 4 2 1]
[0 1 2 3 4]
[4 3 2 1 0]
2.tf.argsort(tensor,direction='DESCENDING'):将每一行元素的第n大(小)值的位置返回在相应的位置上
example:
input:
a=tf.random.shuffle(tf.range(5))
b=tf.argsort(a)
c=tf.argsort(a,direction='DESCENDING')
tf.print(a)
tf.print(b)
tf.print(c)
output:
[0 2 4 3 1]
[0 4 1 3 2]
[2 3 1 4 0]
3.tf.math.top_k(tensor,k):找出tensor中每行元素从小到大排列后的前k个元素,返回两个tensor,分别为值和位置
example:
input:
import tensorflow as tf
a=tf.constant(
[ [4,6,8],
[9,4,7],
[4,5,2]]
)
c=tf.math.top_k(a,1)
tf.print(c.values)
tf.print(c.indices)
output:
[[8]
[9]
[5]]
[[2]
[0]
[1]]

浙公网安备 33010602011771号