8、数据统计
-
tf.norm
-
tf.reduce_min/max
-
tf.argmax/argmin
-
tf.equal
-
tf.unique
1、norm,向量的范数
1 #向量的范数,默认为2范数,平方和开根号
2 a = tf.ones([2,2])
3 print(tf.norm(a)) # tf.Tensor(2.0, shape=(), dtype=float32)
4
5 #自己实现
6 a1 = tf.sqrt(tf.reduce_sum(tf.square(a)))
7 print(a1) # tf.Tensor(2.0, shape=(), dtype=float32)
8
9 b = tf.ones([4,28,28,3])
10 b1 = tf.norm(b)
11 print(b1) #tf.Tensor(96.99484, shape=(), dtype=float32)
12
13 #自己实现
14 b2 = tf.sqrt(tf.reduce_sum(tf.square(b)))
15 print(b2) #tf.Tensor(96.99484, shape=(), dtype=float32)
1 # L1 norm
2 b = tf.ones([2,2])
3 n1 = tf.norm(b)
4 print(n1) #tf.Tensor(2.0, shape=(), dtype=float32)
5
6 n2 = tf.norm(b,ord=2,axis=1) #压缩列,求每一行的2范数
7 print(n2) # tf.Tensor([1.4142135 1.4142135], shape=(2,), dtype=float32)
8
9 n3 = tf.norm(b,ord=1) #1范数,默认求所有的值
10 print(n3) # tf.Tensor(4.0, shape=(), dtype=float32)
11
12 n3 = tf.norm(b,ord=1,axis=0) #压缩行,求每一列的1范数
13 print(n3) # tf.Tensor([2. 2.], shape=(2,), dtype=float32)
14
15 n4 = tf.norm(b,ord=1,axis=1) #压缩列,求每一行的1范数
16 print(n4) # tf.Tensor([2. 2.], shape=(2,), dtype=float32)
2、reduce_min/max/mean 加一个reduce是因为在求最值的过程中会出现维度减少
1 #reduce_min/max/mean 加一个reduce是因为在求最值的过程中会出现维度的减少
2 a = tf.random.normal([4,10]) #0-1分布的数据
3
4 # ①默认是所有维度的统计
5 max_a = tf.reduce_max(a)
6 min_a = tf.reduce_min(a)
7 mean_a = tf.reduce_mean(a)
8 print(max_a,"\n",min_a,"\n",mean_a)
9
10 # ②指定维度
11 max_a = tf.reduce_max(a,axis=1)
12 min_a = tf.reduce_min(a,axis=1)
13 mean_a = tf.reduce_mean(a,axis=1)
14 print(max_a,"\n",min_a,"\n",mean_a)
输出:
tf.Tensor(1.866617, shape=(), dtype=float32) tf.Tensor(-2.5952716, shape=(), dtype=float32) tf.Tensor(-0.25601098, shape=(), dtype=float32) tf.Tensor([ 1.866617 1.0276536 1.6409966 -0.11667389], shape=(4,), dtype=float32) tf.Tensor([-2.5952716 -2.0198066 -0.93489224 -1.7569251 ], shape=(4,), dtype=float32) tf.Tensor([-0.43045864 -0.26896027 0.43675566 -0.76138073], shape=(4,), dtype=float32)
3、argmax/argmin 返回最大值/最小值的index
a = tf.random.normal([4,10]) #0-1分布的数据 print(a) b = tf.argmax(a) #默认是求得每一列的最大值的索引 print(b)
输出:
tf.Tensor( [[-0.7919774 -2.972948 -3.1243117 1.2346513 -0.652347 0.35998186 1.4628253 -0.12529439 0.18237525 -0.29699537] [-1.2740427 -0.13558723 0.5396547 -0.46394014 0.27174982 0.06232562 -0.73767865 1.03348 -0.36170274 1.170251 ] [-0.1372166 0.3829013 -0.42844287 0.7059885 0.78422594 -0.11698396 0.08661779 -0.22002225 -1.4231229 2.1049361 ] [ 0.19104291 -1.4988437 0.47450095 -0.04199754 -0.14426158 0.6865424 0.85595787 1.442973 -1.9023395 1.2549413 ]], shape=(4, 10), dtype=float32)
tf.Tensor([3 2 1 0 2 3 0 3 0 2], shape=(10,), dtype=int64)
4、equal,求解准确度
(1)cast(a, dtype = tf.int32 ),类型转换函数
(2)equal(a,b)返回一个bool型的张量
(3)reduce_sum(c) 返回张量c中所有元素的和
通过equal(a,b)得到一个bool张量,然后用cast将张量转换为整型0,1,最后用reduce_sum得到正确分类的个数,再除以总数就可以得到准确度
1 a = tf.constant([1,2,3,2,5]) # tf.Tensor([1 2 3 2 5], shape=(5,), dtype=int32)
2 b = tf.range(5) # tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int32)
3 c = tf.equal(a,b) # tf.Tensor([False False False False False], shape=(5,), dtype=bool)
4 sum_Ture = tf.reduce_sum(tf.cast(c,dtype=tf.int32))
5 print(sum_Ture) #tf.Tensor(0, shape=(), dtype=int32)
5、unique 去除重复元素
1 #unique 去除重复元素 2 a = tf.range(5) 3 b = tf.unique(a) 4 print(b) 5 a = tf.constant([1,2,3,3,3,24,5,4,5,5,4,3,1]) 6 b = tf.unique(a) 7 print(b) 8 print("unique_value: ",b.y.numpy()) 9 print("idx: ",b.idx.numpy())
输出:
Unique(y=<tf.Tensor: id=4, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>, idx=<tf.Tensor: id=5, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>) Unique(y=<tf.Tensor: id=7, shape=(6,), dtype=int32, numpy=array([ 1, 2, 3, 24, 5, 4])>, idx=<tf.Tensor: id=8, shape=(13,), dtype=int32, numpy=array([0, 1, 2, 2, 2, 3, 4, 5, 4, 4, 5, 2, 0])>) unique_value: [ 1 2 3 24 5 4] idx: [0 1 2 2 2 3 4 5 4 4 5 2 0]
idx的list相当于一个模板,里面的值是unique_value的索引,通过索引可以将原来的tensor进行还原