欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TensorFlor数据统计

程序员文章站 2024-03-22 12:21:04
...

学习视频
TensorFlor数据统计
1.tf.norm
Here talks about Vector Norm

a=tf.ones([2,2])
tf.norm(a)  #numpy=2.0  (1^2+1^2+1^2+1^2)^0.5=2.0
tf.sqrt(tf.reduce_sum(tf.square(a)))  #numpy=2.0

a=tf.ones([4,28,28,3])
tf.norm(a)  #numpy=96.99484
tf.sqrt(tf.reduce_sum(tf.square(a)))  #numpy=96.99484

2.L1 Norm

b=tf.ones([2,2])
tf.norm(b,ord=2,axis=1)   #numpy=array([1.4142135,1.4142135])
tf.norm(b,ord=1)   #numpy=4.0  1+1+1+1=4
tf.norm(b,ord=1,axis=0)   #numpy=array([2.,2.])
tf.norm(b,ord=1,axis=1)   #numpy=array([2.,2.])

3.reduce_min/max/mean

a=tf.random.normal([4,10])
tf.reduce_min(a),tf.reduce_max(a),tf.reduce_mean(a)  #numpy=-1.1872,numpy=2.135,numpy=0.352
tf.reduce_min(a,axis=1)    #numpy=array[-0.393,-1.423.-1.093,-1.136]

4.argmax/argmin

a=tf.random.normal([4,10])
tf.argmax(a).shape  #TensorShape([10])
tf.argmax(a)  #numpy=array([0,0,2,3,1,5,7,0,3,4])
tf.argmin(a).shape  #TensorShape([10])

5.tf.equal

a=tf.constant([1,2,2,5,4])
b=range(5)
b  #[0,1,2,3,4]
tf.equal(a,b)  #numpy = array([False,False,True,False,True])

res=tf.equal(a,b)
tf.reduce_sum(tf.cast(res,dtype=tf.int32))  #numpy=2

6.Accuracy

a=np.array([[0.1,0.2,0.7],[0.9,0.02,0.08]])
y=np.array([1,0])
pred = tf.cast(tf.argmax(a,axis=1),dtype=int32)  #numpy=array([2,0])
tf.equal(y,pred)  #numpy=array([False,True])
correct=tf.reduce_sum(tf.cast(tf.equal(y,pred),dtype=int32))  #numpy=1
accuracy=correct/len(y)  #numpy=0.5

7.tf.unique

a=tf.constant([4,2,2,4,3])
tf.unique(a)  #numpy=array([0,1,1,0,2])