一.合并与分割(MergeAndSplit)
这里的合并于分割是基于张量提出的,所以我们要先理解一下张量是什么意思:
张量:几何代数中的张量是基于向量和矩阵的推广,通俗一点理解,我们可以将标量视为零阶张量,矢量视为一阶张量,矩阵就是二阶张量。
也就是说,我们可以将张量理解为n维数组。在Tensorflow中,张量其实就是tensor。
- 0维张量/标量:标量是一个数字
1维张量/向量:1维张量称为“向量”。
2维张量:2维张量称为矩阵
3维张量:公用数据存储在张量,如:文本数据、彩色图片(RGB)
张量合并与分割的方法:
tf.concat([a,b] , axis = n):拼接,将a,b合并维度下标为n的维度,但a,b的其他维度必须相同;
tf.split(a,axis=n,num_or_size_splits=x):将a在n维上分割为x份,或者将x改为[x,y,z],则就分割为3份,每份分别为x,y,z;比unstack灵活性更强;
tf.stack([a,b] , axis = n):堆叠,在n维上增加了一个维度,但a、b必须shape相同;
tf.unstack():分割
练习代码如下:
import tensorflow as tf
a = tf.ones([4,35,8])
b = tf.ones([2,35,8])
#tf.concat([a,b] , axis = n):拼接,
print(tf.concat([a,b],axis=0).shape) #合并维度下标为 axis的维度,但其他维度必须相同
#out:(6, 35, 8)
a = tf.ones([4,35,8])
b = tf.ones([4,35,8])
##tf.stack([a,b] , axis = n),堆叠
print(tf.stack([a,b],axis=0).shape) #在axis上增加了一个维度,但a、b必须shape相同
#out:(2, 4, 35, 8)
cArray = tf.split(a,axis=0,num_or_size_splits=2) #在0维上被分割为2份
print(cArray[0].shape) #out:(2, 35, 8)
dArray = tf.split(a,axis=2,num_or_size_splits=[2,2,4]) #在0维上被分割为3份,比例为[2,2,4]
print(dArray[0].shape,dArray[1].shape,dArray[2].shape) #out:(4, 35, 2) (4, 35, 2) (4, 35, 4)
二.数据统计(DataStatistic)
tf.norm():求范数
a = tf.ones([2,2])
#求二范数
print(tf.norm(a)) #out:tf.Tensor(2.0, shape=(), dtype=float32)
print(tf.sqrt(tf.reduce_sum(tf.square(a)))) #这个式子跟上面求范数是一个意思,就是从算法上实现norm():先求平方,在求和,再开根号
a = tf.ones([4,28,28,3])
print(tf.norm(a)) #out:tf.Tensor(96.99484, shape=(), dtype=float32)
a = tf.ones([2,2])
#求一范数
print(tf.norm(a,ord=2,axis=1)) #out:tf.Tensor([1.4142135 1.4142135], shape=(2,), dtype=float32)
tf.reduce_min/max/mean():最大/小/均值
#tf.reduce_min/max/mean():最大/小/均值
a = tf.random.normal([4,10])
print(tf.reduce_min(a),tf.reduce_max(a),tf.reduce_mean(a))#tf.Tensor(-3.5173497, shape=(), dtype=float32) tf.Tensor(2.455712, shape=(), dtype=float32) tf.Tensor(0.11045538, shape=(), dtype=float32)
print(tf.reduce_max(a,axis=1)) #可以专门求某一维度进行求
tf.argmax/argmin():最大/小值的位置,分别输出每个维度中分别的最大/小值
print(tf.argmax(a))
#out:tf.Tensor([1 1 2 2 2 2 2 2 1 0], shape=(10,), dtype=int64)
#每个维度都求
tf.equal():张量比较
a = tf.constant([0,2,3,2,5])
b = tf.range(5) #[0,1,2,3,4]
print(tf.equal(a,b)) #每个维度都比较,只有0维能相等
#out:tf.Tensor([ True False False False False], shape=(5,), dtype=bool)
res = tf.equal(a,b)
print(tf.reduce_sum(tf.cast(res,dtype=tf.int32))) #计算相等的个数
#out:tf.Tensor(1, shape=(), dtype=int32)
tf.unique():独特值,即去除所有的重复值再返回,就是每个元素只出现一次
a = tf.range(5) #[0,1,2,3,4]
print(tf.unique(a))#全都不同
#Unique(y=<tf.Tensor: id=149, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>, idx=<tf.Tensor: id=150, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>)
a = tf.constant([3,2,3,2,5])
print(tf.unique(a))#3,2,5不同,后面的array就是每个数字第一次出现的下标
#Unique(y=<tf.Tensor: id=152, shape=(3,), dtype=int32, numpy=array([3, 2, 5])>, idx=<tf.Tensor: id=153, shape=(5,), dtype=int32, numpy=array([0, 1, 0, 1, 2])>)
三.张量排序(TensorSort)
tf.sort(a,direction=’DESCENDING/ASCENDING’):排序,将a中的元素降序/升序输出,在二维中只在每一维中分别排序;
argsort(a,direction=’DESCENDING/ASCENDING’):排序输出index,将a中的元素降序/升序排列后,按顺序输出其在a中原本的index;
tf.math.top_k(a,k):最大的k个元素,二维中得到每一维的前k个值
a = tf.random.shuffle(tf.range(5)) #将[0,1,2,3,4]随机打乱
print(a)
#out:tf.Tensor([1 3 0 4 2], shape=(5,), dtype=int32)
#tf.sort(a,direction=’DESCENDING/ASCENDING’):排序,将a中的元素降序/升序输出;
print(tf.sort(a,direction='DESCENDING')) #降序排序,
# out:tf.Tensor([4 3 2 1 0], shape=(5,), dtype=int32)
#argsort(a,direction=’DESCENDING/ASCENDING’):排序输出index,将a中的元素降序/升序排列后,按顺序输出其在a中原本的index;
print(tf.argsort(a,direction='DESCENDING')) #降序输出每个元素在原本张量中的index
#tf.Tensor([3 1 4 0 2], shape=(5,), dtype=int32)
#二维测试
a = tf.random.uniform([3,3],maxval=10,dtype=tf.int32)
print(a)
# tf.Tensor(
# [[4 1 0]
# [0 8 6]
# [9 1 8]], shape=(3, 3), dtype=int32)
print(tf.sort(a)) #在二维中只在每一维中分别排序
# tf.Tensor(
# [[0 1 4]
# [0 6 8]
# [1 8 9]], shape=(3, 3), dtype=int32)
print(tf.argsort(a))
# tf.Tensor(
# [[2 1 0]
# [0 2 1]
# [1 2 0]], shape=(3, 3), dtype=int32)
#tf.math.top_k(a,k):最大的k个元素,二维中得到每一维的前k个值
res = tf.math.top_k(a,2) #二维中,找出每一维最大的两个
print(res.values) #输出元素值
# tf.Tensor(
# [[4 1]
# [8 6]
# [9 8]], shape=(3, 2), dtype=int32)
print(res.indices) #输出元素原本的Index
#tf.Tensor(
# [[0 1]
# [1 2]
# [0 2]], shape=(3, 2), dtype=int32)