TensorFlow2.0(三):排序及最大、最小、平均值

时间:2019-10-02
本文章向大家介绍TensorFlow2.0(三):排序及最大、最小、平均值,主要包括TensorFlow2.0(三):排序及最大、最小、平均值使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 

1 排序

 

1.1 sort:返回逆序排序后的Tensor

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

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

In [9]:
b = tf.random.uniform([3, 3], minval=1, maxval=10,dtype=tf.int32)
In [10]:
b
Out[10]:
<tf.Tensor: id=46, shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 1],
       [9, 6, 3],
       [6, 4, 1]])>
In [11]:
tf.sort(b)
Out[11]:
<tf.Tensor: id=59, shape=(3, 3), dtype=int32, numpy=
array([[1, 1, 2],
       [3, 6, 9],
       [1, 4, 6]])>
In [13]:
tf.sort(b,axis=0)  # 通过axis参数指定第一维度,也就是列进行排序
Out[13]:
<tf.Tensor: id=91, shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 1],
       [6, 4, 1],
       [9, 6, 3]])>
 

1.2 argsort:返回排序后的索引

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

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

In [14]:
b
Out[14]:
<tf.Tensor: id=46, shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 1],
       [9, 6, 3],
       [6, 4, 1]])>
In [17]:
tf.argsort(b)  # 默认对最后一维度排序,也就是以行为单位排序
Out[17]:
<tf.Tensor: id=134, shape=(3, 3), dtype=int32, numpy=
array([[0, 2, 1],
       [2, 1, 0],
       [2, 1, 0]])>
In [18]:
tf.argsort(b,axis=0)  # 指定第一维度进行排序,也就是以列为单位进行排序
Out[18]:
<tf.Tensor: id=149, shape=(3, 3), dtype=int32, numpy=
array([[0, 0, 0],
       [2, 2, 2],
       [1, 1, 1]])>
 

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

 

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

 

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

 

注意:top_k()方法在tf.math模块中。

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

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

In [22]:
top_2.values
Out[22]:
<tf.Tensor: id=153, shape=(2,), dtype=int32, numpy=array([5, 4])>
In [23]:
top_2.indices
Out[23]:
<tf.Tensor: id=154, shape=(2,), dtype=int32, numpy=array([2, 5])>
 

对于高维Tensor也是一样的:

In [37]:
b
Out[37]:
<tf.Tensor: id=152, shape=(3, 3), dtype=int32, numpy=
array([[7, 9, 7],
       [4, 3, 1],
       [1, 1, 6]])>
In [39]:
tf.math.top_k(b, 2)
Out[39]:
TopKV2(values=<tf.Tensor: id=211, shape=(3, 2), dtype=int32, numpy=
array([[9, 7],
       [4, 3],
       [6, 1]])>, indices=<tf.Tensor: id=212, shape=(3, 2), dtype=int32, numpy=
array([[1, 0],
       [0, 1],
       [2, 0]])>)
 

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

 

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

 

2.1 reduce_min、reduce_max、reduce_mean

 

(1)reduce_min():求最小值

In [24]:
a = tf.random.uniform([3, 3], minval=1, maxval=10, dtype=tf.int32)
In [28]:
a
Out[28]:
<tf.Tensor: id=162, shape=(3, 3), dtype=int32, numpy=
array([[4, 9, 5],
       [8, 6, 1],
       [8, 7, 1]])>
 

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

In [29]:
tf.reduce_min(a)  # 最小值
Out[29]:
<tf.Tensor: id=169, shape=(), dtype=int32, numpy=1>
 

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

In [30]:
 tf.reduce_min(a, axis=0)  # 求指定维度的最小值
Out[30]:
<tf.Tensor: id=172, shape=(3,), dtype=int32, numpy=array([4, 6, 1])>
 

(2)reduce_max():求最大值

In [31]:
tf.reduce_max(a)  # 最大值
Out[31]:
<tf.Tensor: id=175, shape=(), dtype=int32, numpy=9>
In [36]:
 tf.reduce_max(a, axis=-1)  # 求最后一维度的最大值
Out[36]:
<tf.Tensor: id=190, shape=(3,), dtype=int32, numpy=array([9, 8, 8])>
 

(3)reduce_mean():求平均值

 

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

In [44]:
 tf.reduce_mean(a)  # 整个Tensor所有元素的平均值
Out[44]:
<tf.Tensor: id=227, shape=(), dtype=int32, numpy=4>
In [38]:
tf.reduce_mean(a, axis=0)  # 求第一维度(行)均值
Out[38]:
<tf.Tensor: id=196, shape=(3,), dtype=int32, numpy=array([6, 7, 2])>
 

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

In [39]:
tf.reduce_mean(tf.cast(a, tf.float32), axis=0)
Out[39]:
<tf.Tensor: id=200, shape=(3,), dtype=float32, numpy=array([6.6666665, 7.3333335, 2.3333333], dtype=float32)>
 

2.2 argmin()、argmax()

 

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

 

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

In [40]:
a = tf.random.uniform([3,3],minval=1, maxval=10, dtype=tf.int32)
In [41]:
a
Out[41]:
<tf.Tensor: id=205, shape=(3, 3), dtype=int32, numpy=
array([[5, 6, 1],
       [3, 7, 2],
       [7, 1, 6]])>
In [42]:
b = tf.random.uniform([3,3,3],minval=1, maxval=10, dtype=tf.int32)
In [43]:
b
Out[43]:
<tf.Tensor: id=210, shape=(3, 3, 3), dtype=int32, numpy=
array([[[5, 4, 7],
        [4, 3, 9],
        [5, 3, 6]],

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

       [[9, 9, 5],
        [5, 4, 4],
        [7, 1, 1]]])>
In [44]:
tf.argmin(a)  # 默认是第0维度
Out[44]:
<tf.Tensor: id=213, shape=(3,), dtype=int64, numpy=array([1, 2, 0], dtype=int64)>
In [45]:
tf.argmin(b)
Out[45]:
<tf.Tensor: id=216, shape=(3, 3), dtype=int64, numpy=
array([[0, 0, 1],
       [1, 1, 2],
       [0, 2, 1]], dtype=int64)>
 

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

 

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

 

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

In [46]:
a = tf.random.uniform([3,3,3],minval=1, maxval=10, dtype=tf.int32)
In [47]:
a
Out[47]:
<tf.Tensor: id=221, shape=(3, 3, 3), dtype=int32, numpy=
array([[[1, 2, 7],
        [9, 3, 3],
        [5, 4, 8]],

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

       [[8, 9, 7],
        [3, 3, 9],
        [7, 7, 2]]])>
In [51]:
tf.argmax(a, axis=0)  # 第一维度,也就是每一块
Out[51]:
<tf.Tensor: id=233, shape=(3, 3), dtype=int64, numpy=
array([[1, 2, 0],
       [0, 1, 2],
       [2, 2, 0]], dtype=int64)>
In [52]:
tf.argmax(a, axis=2)  # 第三维度,也就是每一行
Out[52]:
<tf.Tensor: id=236, shape=(3, 3), dtype=int64, numpy=
array([[2, 0, 2],
       [0, 1, 0],
       [1, 2, 0]], dtype=int64)>

原文地址:https://www.cnblogs.com/chenhuabin/p/11617663.html