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)

 

猜你喜欢

转载自www.cnblogs.com/pengzhonglian/p/11934119.html