美文网首页
TensorFlow高阶操作之数据统计

TensorFlow高阶操作之数据统计

作者: 酵母小木 | 来源:发表于2020-02-03 19:02 被阅读0次

【碎语】今天是星期一,开工大吉!已经看了5节网课,我感觉我快坚持不住了!!!
【杂记】电视剧《楚汉传奇》里面的季桃姑娘,好想发给你看

1.张量范数

分为向量范数和矩阵范数,这里只讨论向量范数,重点是1-范数(平方和开根号,即向量距离)和2-范数(各元素绝对值之和)


1.PNG
In [55]: a = tf.ones([2, 2])

In [56]: tf.norm(a)
Out[56]: <tf.Tensor: id=115, shape=(), dtype=float32, numpy=2.0>

//2-范数,等同于平方和开根号
In [57]: tf.sqrt(tf.reduce_sum(tf.square(a)))
Out[57]: <tf.Tensor: id=119, shape=(), dtype=float32, numpy=2.0>
==============================================================
In [58]: b = tf.ones([2, 2])
//指定某个轴的2-范数
In [60]: tf.norm(b, ord=2, axis=1)
Out[60]: <tf.Tensor: id=132, shape=(2,), dtype=float32, numpy=array([1.4142135, 1.4142135], dtype=float32)>

//1-范数
In [61]: tf.norm(b, ord=1)
Out[61]: <tf.Tensor: id=136, shape=(), dtype=float32, numpy=4.0>

//某个维度的1-范数
In [63]: tf.norm(b, ord=1, axis=0)
Out[63]: <tf.Tensor: id=140, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
In [64]: tf.norm(b, ord=1, axis=1)
Out[64]: <tf.Tensor: id=144, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>

2.统计操作

会对数据进行降维
【tf.reduce_min()】:求最小值
【tf.reduce_max()】:求最大值
【tf.reduce_mean()】:求均值

In [65]: a = tf.random.normal([4, 10])
//默认对所有元素进行操作
In [66]: tf.reduce_min(a), tf.reduce_max(a), tf.reduce_mean(a)
Out[66]:
(<tf.Tensor: id=152, shape=(), dtype=float32, numpy=-1.5740632>,
 <tf.Tensor: id=154, shape=(), dtype=float32, numpy=3.073718>,
 <tf.Tensor: id=156, shape=(), dtype=float32, numpy=0.099534735>)
//只对某个维度进行统计操作
In [67]: tf.reduce_min(a, axis=1),tf.reduce_max(a, axis=1),tf.reduce_mean(a, axis=1)
Out[67]:
(<tf.Tensor: id=158, shape=(4,), dtype=float32, numpy=array([-1.5740632, -1.1874818, -1.1577657, -1.3591901], dtype=float32)>,
 <tf.Tensor: id=160, shape=(4,), dtype=float32, numpy=array([1.4919865, 3.073718 , 1.4677159, 1.3543878], dtype=float32)>,
 <tf.Tensor: id=162, shape=(4,), dtype=float32, numpy=array([0.02667272, 0.25128826, 0.06651495, 0.05366297], dtype=float32)>)

求位置,会产生数据降维
【tf.argmax()】:求最小值的位置
【tf.argmin()】:求最大值的位置

In [68]: a = tf.random.normal([4, 10])

In [69]: a.shape
Out[69]: TensorShape([4, 10])

//求指定轴最大值的位置
In [70]: tf.argmax(a, axis=1)
Out[70]: <tf.Tensor: id=170, shape=(4,), dtype=int64, numpy=array([2, 9, 9, 2], dtype=int64)>

//求指定轴最小值的位置
In [71]: tf.argmin(a, axis=0)
Out[71]: <tf.Tensor: id=172, shape=(10,), dtype=int64, numpy=array([1, 1, 2, 1, 2, 1, 0, 1, 3, 0], dtype=int64)>

3.匹配验证操作:【tf.equal()】

In [72]: a = tf.constant([1, 1, 1, 2, 5])
In [73]: b = tf.range(5)      //b = [0, 1, 2, 3, 4]

//对应元素相同则为TRUE,不同则为FALSE
In [74]: tf.equal(a, b)
Out[74]: <tf.Tensor: id=178, shape=(5,), dtype=bool, numpy=array([False,  True, False, False, False])>
In [75]: res = tf.equal(a, b)
//先用tf.cast()将数据转换为整型,然后求和,便可以知道匹配个数
In [76]: tf.reduce_sum(tf.cast(res, dtype=tf.int32))
Out[76]: <tf.Tensor: id=182, shape=(), dtype=int32, numpy=1>

求解准确度(Accuracy)的实例

In [80]: a1 = tf.constant([0.1, 0.2, 0.7])
In [81]: a2 = tf.constant([0.9, 0.05, 0.05])
In [86]: a = tf.stack([a1, a2], axis=0)

In [87]: a
Out[87]: <tf.Tensor: id=188, shape=(2, 3), dtype=float32, numpy=
array([[0.1 , 0.2 , 0.7 ],
       [0.9 , 0.05, 0.05]], dtype=float32)>

// pred = [2, 0]
In [93]: pred = tf.cast(tf.argmax(a, axis=1), dtype=tf.int32)
In [94]: y = tf.constant([2,1])

In [96]: tf.equal(y, pred)
Out[96]: <tf.Tensor: id=196, shape=(2,), dtype=bool, numpy=array([ True, False])>

In [97]: correct = tf.reduce_sum(tf.cast(tf.equal(y, pred), dtype=tf.int32))
In [98]: correct
Out[98]: <tf.Tensor: id=200, shape=(), dtype=int32, numpy=1>

In [99]: correct / 2
Out[99]: <tf.Tensor: id=204, shape=(), dtype=float64, numpy=0.5>

4.去除重复元素:【tf.unique()】

In [105]: a = tf.constant([4, 2, 2, 4,3])
In [106]: newa = tf.unique(a)

//变为两部分:非重复数据和索引号
In [108]: newa
Out[108]: Unique(y=<tf.Tensor: id=208, shape=(3,), dtype=int32, numpy=array([4, 2, 3])>, idx=<tf.Tensor: id=209, shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2])>)

//通过非重复数据和索引号可以复原数据
In [107]: tf.gather(newa.y, newa.idx)
Out[107]: <tf.Tensor: id=211, shape=(5,), dtype=int32, numpy=array([4, 2, 2, 4, 3])>

参考资料

相关文章

网友评论

      本文标题:TensorFlow高阶操作之数据统计

      本文链接:https://www.haomeiwen.com/subject/ytnhxhtx.html