分享

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

 ml_Py 2021-12-08

重磅干货,第一时间送达

TensorFlow2.0(1):基本数据结构——张量

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, 1, 5, 2, 0, 4])>
tf.sort(a) # 默认是顺序排列
<tf.Tensor: id=17, shape=(6,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5])>
tf.sort(a, direction='ASCENDING') # 默认顺序排列
<tf.Tensor: id=30, shape=(6,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5])>
tf.sort(a, direction='DESCENDING') # 指定逆序排列
<tf.Tensor: id=40, shape=(6,), dtype=int32, numpy=array([5, 4, 3, 2, 1, 0])>

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

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

1.2 argsort:返回排序后的索引

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

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

b
<tf.Tensor: id=46, shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 1],
[9, 6, 3],
[6, 4, 1]])>
tf.argsort(b) # 默认对最后一维度排序,也就是以行为单位排序
<tf.Tensor: id=134, shape=(3, 3), dtype=int32, numpy=
array([[0, 2, 1],
[2, 1, 0],
[2, 1, 0]])>
tf.argsort(b,axis=0) # 指定第一维度进行排序,也就是以列为单位进行排序
<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:返回逆序排序后的前个元素组成的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, 1, 5, 2, 0, 4])>
top_2 = tf.math.top_k(a, 2) # 获取排序后前两位
top_2
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获取。

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

对于高维Tensor也是一样的:

b
<tf.Tensor: id=152, shape=(3, 3), dtype=int32, numpy=
array([[7, 9, 7],
[4, 3, 1],
[1, 1, 6]])>
tf.math.top_k(b, 2)
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():求最小值

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

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

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

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

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

(2)reduce_max():求最大值

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

(3)reduce_mean():求平均值

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

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

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

tf.reduce_mean(tf.cast(a, tf.float32), axis=0)
<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():求最小值索引

a = tf.random.uniform([3,3],minval=1, maxval=10, dtype=tf.int32)
a
<tf.Tensor: id=205, shape=(3, 3), dtype=int32, numpy=
array([[5, 6, 1],
[3, 7, 2],
[7, 1, 6]])>
b = tf.random.uniform([3,3,3],minval=1, maxval=10, dtype=tf.int32)
b
<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]]])>
tf.argmin(a) # 默认是第0维度
<tf.Tensor: id=213, shape=(3,), dtype=int64, numpy=array([1, 2, 0], dtype=int64)>
tf.argmin(b)
<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():求最大值索引

a = tf.random.uniform([3,3,3],minval=1, maxval=10, dtype=tf.int32)
a
<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]]])>
tf.argmax(a, axis=0) # 第一维度,也就是每一块
<tf.Tensor: id=233, shape=(3, 3), dtype=int64, numpy=
array([[1, 2, 0],
[0, 1, 2],
[2, 2, 0]], dtype=int64)>
tf.argmax(a, axis=2) # 第三维度,也就是每一行
<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

作者github:

https://github.com/ChenHuabin321/tensorflow2_tutorials

戳一下右下角在看小小举动,大大支持~

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多