**
一 tf.norm( )函数–求范数
**
In [30]: a = tf.ones([2,2])
In [31]: tf.norm(a)#求a的二范数
Out[31]: <tf.Tensor: id=57, shape=(), dtype=float32, numpy=2.0>
In [32]: tf.sqrt(tf.reduce_sum(tf.square(a)))#求a的二范数
Out[32]: <tf.Tensor: id=62, shape=(), dtype=float32, numpy=2.0>
In [33]: a = tf.ones([4,28,28,3])
In [34]: tf.norm(a)
Out[34]: <tf.Tensor: id=71, shape=(), dtype=float32, numpy=96.99484>
In [35]: tf.sqrt(tf.reduce_sum(tf.square(a)))#求a的二范数
Out[35]: <tf.Tensor: id=76, shape=(), dtype=float32, numpy=96.99484>
In [36]: b = tf.ones([2,2])
In [37]: tf.norm(b)
Out[37]: <tf.Tensor: id=85, shape=(), dtype=float32, numpy=2.0>
In [38]: tf.norm(b,ord=2,axis=1)#求b在轴1方向的二范数
Out[38]: <tf.Tensor: id=91, shape=(2,), dtype=float32, numpy=array([1.4142135, 1.4142135], dtype=float32)>
In [39]: tf.norm(b,ord=1)#求b的一范数
Out[39]: <tf.Tensor: id=96, shape=(), dtype=float32, numpy=4.0>
In [40]: tf.norm(b,ord=1,axis=0)#求b在轴0方向上的一范数
Out[40]: <tf.Tensor: id=101, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
In [41]: tf.norm(b,ord=1,axis=1)#求b在轴1方向上的一范数
Out[41]: <tf.Tensor: id=106, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
**
二 tf.reduce_min/max/mean( )函数–求解最小值最大值和均值函数
**
In [2]: a = tf.random.normal([4,10])
In [3]: tf.reduce_min(a)
Out[3]: <tf.Tensor: id=7, shape=(), dtype=float32, numpy=-1.8195488>
In [4]: tf.reduce_max(a)
Out[4]: <tf.Tensor: id=10, shape=(), dtype=float32, numpy=2.3925323>
In [5]: tf.reduce_mean(a)
Out[5]: <tf.Tensor: id=13, shape=(), dtype=float32, numpy=0.17027304>
In [6]: tf.reduce_min(a,axis=1)#对轴1方向取最小值
Out[6]: <tf.Tensor: id=16, shape=(4,), dtype=float32, numpy=array([-0.3108449, -1.4913915, -1.3940301, -1.8195488], dtype=float32)>
In [7]: tf.reduce_max(a,axis=1)#对轴1方向取最大值
Out[7]: <tf.Tensor: id=19, shape=(4,), dtype=float32, numpy=array([1.9342865, 0.8926946, 2.3925323, 1.7844682], dtype=float32)>
In [8]: tf.reduce_mean(a,axis=1)#对轴1方向取均值
Out[8]: <tf.Tensor: id=22, shape=(4,), dtype=float32, numpy=array([ 0.7435825 , -0.08960855, 0.25611132, -0.2289931 ], dtype=float32)>
**
三 tf.argmax( )和tf.argmin( )函数-求最大最小值位置
**
In [9]: a = tf.random.normal([4,10])
In [10]: tf.argmax(a).shape
Out[10]: TensorShape([10])
In [11]: tf.argmax(a)
Out[11]: <tf.Tensor: id=33, shape=(10,), dtype=int64, numpy=array([1, 0, 0, 1, 3, 3, 1, 1, 2, 0])>
In [12]: tf.argmin(a).shape
Out[12]: TensorShape([10])
In [13]: tf.argmin(a)
Out[13]: <tf.Tensor: id=38, shape=(10,), dtype=int64, numpy=array([3, 3, 2, 0, 2, 2, 0, 2, 0, 1])>
In [14]: tf.argmax(a,axis=1)
Out[14]: <tf.Tensor: id=41, shape=(4,), dtype=int64, numpy=array([0, 0, 8, 4])>
In [15]: tf.argmin(a,axis=1)
Out[15]: <tf.Tensor: id=44, shape=(4,), dtype=int64, numpy=array([6, 9, 4, 1])>
**
四 tf.equal( )函数
**
In [22]: a = tf.constant([0,2,3,2,4])
In [23]: b = tf.range(5)
In [24]: res = tf.equal(a,b)
In [25]: a,b,res
Out[25]:
(<tf.Tensor: id=55, shape=(5,), dtype=int32, numpy=array([0, 2, 3, 2, 4], dtype=int32)>,
<tf.Tensor: id=59, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>,
<tf.Tensor: id=60, shape=(5,), dtype=bool, numpy=array([ True, False, False, False, True])>)
In [26]: tf.reduce_sum(tf.cast(res,dtype=tf.int32))
Out[26]: <tf.Tensor: id=66, shape=(), dtype=int32, numpy=2>
求解准确度
In [34]: a = tf.constant([[0.1,0.2,0.7],[0.9,0.05,0.05],[0.1,0.8,0.1],[0.01,0.99,0]])
In [35]: pred = tf.cast(tf.argmax(a,axis=1),dtype=tf.int32)
In [36]: a,pred
Out[36]:
(<tf.Tensor: id=78, shape=(4, 3), dtype=float32, numpy=
array([[0.1 , 0.2 , 0.7 ],
[0.9 , 0.05, 0.05],
[0.1 , 0.8 , 0.1 ],
[0.01, 0.99, 0. ]], dtype=float32)>,
<tf.Tensor: id=81, shape=(4,), dtype=int32, numpy=array([2, 0, 1, 1], dtype=int32)>)
In [37]: y = tf.constant([2,0,1,2])
In [39]: correct_num = tf.reduce_sum(tf.cast(tf.equal(pred,y),dtype=tf.int32))
In [40]: accuracy = correct_num /a.shape[0]
In [41]: correct_num,accuracy
Out[41]:
(<tf.Tensor: id=92, shape=(), dtype=int32, numpy=3>,
<tf.Tensor: id=96, shape=(), dtype=float64, numpy=0.75>)
**
五 tf.unique( )函数-找不重复部分
**
In [42]: a = tf.range(5)
In [43]: Unique,idx = tf.unique(a)
In [44]: Unique,idx
Out[44]:
(<tf.Tensor: id=103, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>,
<tf.Tensor: id=104, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>)
In [45]: a = tf.constant([4,2,2,4,3])
In [46]: Unique,idx = tf.unique(a)
In [47]: Unique,idx
Out[47]:
(<tf.Tensor: id=108, shape=(3,), dtype=int32, numpy=array([4, 2, 3], dtype=int32)>,
<tf.Tensor: id=109, shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2], dtype=int32)>)
In [48]: aa = tf.gather(Unique,idx)
In [49]: aa
Out[49]: <tf.Tensor: id=113, shape=(5,), dtype=int32, numpy=array([4, 2, 2, 4, 3], dtype=int32)>