TensorFlow2.0(3):张量排序、最大最小值

1 排序

1.1 sort:返回排序后的Tensor

import tensorflow as tf
a = tf.random.shuffle(tf.range(6))
a
<tf.Tensor: id=4, shape=(6,), dtype=int32, numpy=array([3, 2, 4, 5, 0, 1], dtype=int32)>
tf.sort(a)     # 默认是顺序排列
<tf.Tensor: id=15, shape=(6,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5], dtype=int32)>
tf.sort(a,direction='ASCENDING')     # 默认顺序排列
<tf.Tensor: id=26, shape=(6,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5], dtype=int32)>
tf.sort(a,direction='DESCENDING')     # 指定逆序排列
<tf.Tensor: id=34, shape=(6,), dtype=int32, numpy=array([5, 4, 3, 2, 1, 0], dtype=int32)>

      也可以对多维Tensor进行排序,当对多维Tensor进行排序时,可以通过axis参数指定需要排序的维度,axis的默认值为-1,也就是对最后一维进行排序。

b = tf.random.uniform([3,3],minval=1,maxval=10,dtype=tf.int32)
b
<tf.Tensor: id=39, shape=(3, 3), dtype=int32, numpy=
array([[1, 9, 8],
       [3, 4, 9],
       [5, 1, 5]], dtype=int32)>
tf.sort(b)
<tf.Tensor: id=50, shape=(3, 3), dtype=int32, numpy=
array([[1, 8, 9],
       [3, 4, 9],
       [1, 5, 5]], dtype=int32)>
tf.sort(b,axis=0)     # 通过axis参数指定第0维度,也就是对列进行排序
<tf.Tensor: id=64, shape=(3, 3), dtype=int32, numpy=
array([[1, 1, 5],
       [3, 4, 8],
       [5, 9, 9]], dtype=int32)>

1.2 argsort:返回排序后的索引

a
<tf.Tensor: id=4, shape=(6,), dtype=int32, numpy=array([3, 2, 4, 5, 0, 1], dtype=int32)>
tf.argsort(a,direction='ASCENDING') # 返回排序之后的索引组成的Tensor,默认是顺序排列
<tf.Tensor: id=74, shape=(6,), dtype=int32, numpy=array([4, 5, 1, 0, 2, 3], dtype=int32)>
tf.argsort(a,direction='DESCENDING')   # 逆序排列
<tf.Tensor: id=83, shape=(6,), dtype=int32, numpy=array([3, 2, 0, 1, 5, 4], dtype=int32)>

      可以通过axis参数指定需要排序的维度,默认获取-1维度排序后的索引。

b
<tf.Tensor: id=39, shape=(3, 3), dtype=int32, numpy=
array([[1, 9, 8],
       [3, 4, 9],
       [5, 1, 5]], dtype=int32)>
tf.argsort(b)     # 默认对最后一维度排序,也就是以行为单位排序
<tf.Tensor: id=93, shape=(3, 3), dtype=int32, numpy=
array([[0, 2, 1],
       [0, 1, 2],
       [1, 0, 2]], dtype=int32)>
tf.argsort(b,axis=0)     # 指定第0维度进行排序,也就是以列为单位进行排序
<tf.Tensor: id=106, shape=(3, 3), dtype=int32, numpy=
array([[0, 2, 2],
       [1, 1, 0],
       [2, 0, 1]], dtype=int32)>

      返回的张量中,每一个元素表示b中原来元素在该列中的索引。

1.3 top_k:返回逆序排列后的前k个元素组成的Tensor

      sort()方法和argsort()方法都是对给定Tensor的所有元素进行排序,在某些情况下如果我们只是要获取排序的前几个元素,这时候使用sort()或argsort()方法就有些浪费时间了,这时候可以使用top_k()方法。

      top_k()方法可以指定获取前k个元素。注意:top_k()方法在tf.math模块中。 

a
<tf.Tensor: id=4, shape=(6,), dtype=int32, numpy=array([3, 2, 4, 5, 0, 1], dtype=int32)>
top_2 = tf.math.top_k(a,2)     # 获取排序后前两位
top_2
TopKV2(values=<tf.Tensor: id=108, shape=(2,), dtype=int32, numpy=array([5, 4], dtype=int32)>, indices=<tf.Tensor: id=109, shape=(2,), dtype=int32, numpy=array([3, 2], dtype=int32)>)

      从上述输出可以看到,top_k()方法返回的是一个TopKV2类型对象,内部包含两部分数据:第一部分是排序后的真实数据[5,4],可以通过TopKV2对象的values属性获取;第二部分是排序后数据所在原Tensor中的索引[3,2],可以通过TokKV2对象的indices获取。

top_2.values
<tf.Tensor: id=108, shape=(2,), dtype=int32, numpy=array([5, 4], dtype=int32)>
top_2.indices
<tf.Tensor: id=109, shape=(2,), dtype=int32, numpy=array([3, 2], dtype=int32)>

      对于高维Tensor也是一样的。

b
<tf.Tensor: id=39, shape=(3, 3), dtype=int32, numpy=
array([[1, 9, 8],
       [3, 4, 9],
       [5, 1, 5]], dtype=int32)>
tf.math.top_k(b,2)
TopKV2(values=<tf.Tensor: id=111, shape=(3, 2), dtype=int32, numpy=
array([[9, 8],
       [9, 4],
       [5, 5]], dtype=int32)>, indices=<tf.Tensor: id=112, shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
       [2, 1],
       [0, 2]], dtype=int32)>)

      注意:top_k()方法只能对最后一维度进行排序。

2 最大值、最小值、平均值、和

2.1 reduce_min、reduce_max、reduce_mean、reduce_sum

(1)reduce_min():求最小值

a = tf.random.uniform([3,3],minval=1,maxval=10,dtype=tf.int32)
a
<tf.Tensor: id=116, shape=(3, 3), dtype=int32, numpy=
array([[2, 3, 9],
       [6, 5, 9],
       [7, 3, 8]], dtype=int32)>

      不指定维度时,获取整个Tensor的最小值。

tf.reduce_min(a)
<tf.Tensor: id=118, shape=(), dtype=int32, numpy=2>

      通过axis参数可以对指定维度求最小值。

tf.reduce_min(a,axis=0)
<tf.Tensor: id=120, shape=(3,), dtype=int32, numpy=array([2, 3, 8], dtype=int32)>

(2)reduce_max():求最大值

tf.reduce_max(a)
<tf.Tensor: id=122, shape=(), dtype=int32, numpy=9>
tf.reduce_max(a,axis=-1)
<tf.Tensor: id=124, shape=(3,), dtype=int32, numpy=array([9, 9, 8], dtype=int32)>

(3)reduce_mean():求平均值

      不指定维度时,求整个Tensor所有元素的平均值。

tf.reduce_mean(a)     # 求整个Tensor所有元素的平均值
<tf.Tensor: id=126, shape=(), dtype=int32, numpy=5>
tf.reduce_mean(a,axis=0)
<tf.Tensor: id=128, shape=(3,), dtype=int32, numpy=array([5, 3, 8], dtype=int32)>

      在上面求均值的例子中,因为Tensor的dtype为int32,所以求出来的均值也是int32,而不是浮点型。如果需要求浮点型的均值,就需要将a的类型先转换为float32。

tf.reduce_mean(tf.cast(a,tf.float32),axis=0)
<tf.Tensor: id=131, shape=(3,), dtype=float32, numpy=array([5.       , 3.6666667, 8.666667 ], dtype=float32)>

2.2 argmin()、agrmax()

      argmin()、agrmax()返回最大值最小值的索引组成的Tensor。

(1)argmin():求最小值索引

a = tf.random.uniform([3,3],minval=1,maxval=10,dtype=tf.int32)
a
<tf.Tensor: id=135, shape=(3, 3), dtype=int32, numpy=
array([[1, 5, 4],
       [1, 2, 7],
       [2, 6, 9]], dtype=int32)>
b = tf.random.uniform([3,3,3],minval=1,maxval=10,dtype=tf.int32)
b
<tf.Tensor: id=139, shape=(3, 3, 3), dtype=int32, numpy=
array([[[5, 2, 1],
        [5, 6, 3],
        [1, 7, 5]],

       [[1, 9, 2],
        [8, 5, 2],
        [1, 6, 9]],

       [[6, 6, 7],
        [2, 8, 7],
        [2, 2, 9]]], dtype=int32)>
tf.argmin(a)     # 默认是对列操作
<tf.Tensor: id=141, shape=(3,), dtype=int64, numpy=array([0, 1, 0])>
tf.argmin(b)
<tf.Tensor: id=143, shape=(3, 3), dtype=int64, numpy=
array([[1, 0, 0],
       [2, 1, 1],
       [0, 2, 0]])>

      对于shape为(3,3)的Tensor,argmin(a)返回的是shape为(3,)的Tensor,因为没有指定比较的维度,默认比较的是第0维度的元素,也就是每一列数据;对于shape为(3,3,3)的Tensor,argmin(a)返回的是shape为(3,3)的Tensor,默认比较的是第0维度的元素,也就是每一块对应位置的元素,例如第一块的5,第二块的1,第三块的6比较, 第二块的1最小,索引为1,所以返回的Tensor中第一个元素是1。

      注意:argmin()方法在没有指定维度时,默认返回的是第0维度最小值的索引,这与reduce_min()方法不同,reduce_min()方法在没有指定维度时,返回的是整个Tensor中所有元素的最小值。

(2)argmax():求最大值索引

a = tf.random.uniform([3,3,3],minval=1,maxval=10,dtype=tf.int32)
a
<tf.Tensor: id=147, shape=(3, 3, 3), dtype=int32, numpy=
array([[[7, 4, 5],
        [1, 1, 4],
        [9, 1, 4]],

       [[9, 4, 2],
        [3, 7, 9],
        [6, 5, 1]],

       [[8, 5, 2],
        [3, 4, 2],
        [5, 1, 4]]], dtype=int32)>
tf.argmax(a,axis=0)     # 第一维度,也就是每一块
<tf.Tensor: id=149, shape=(3, 3), dtype=int64, numpy=
array([[1, 2, 0],
       [1, 1, 1],
       [0, 1, 0]])>
tf.argmax(a,axis=2)     # 第三维度,也就是每一行
<tf.Tensor: id=151, shape=(3, 3), dtype=int64, numpy=
array([[0, 2, 0],
       [0, 2, 0],
       [0, 1, 0]])>
原文地址:https://www.cnblogs.com/chenjin2018/p/13756462.html