前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据统计

数据统计

作者头像
py3study
发布2020-01-16 14:55:41
1.1K0
发布2020-01-16 14:55:41
举报
文章被收录于专栏:python3python3

目录

Outline

  • tf.norm
  • tf.reduce_min/max/mean
  • tf.argmax/argmin
  • tf.equal
  • tf.unique

Vector norm

  • Eukl. Norm \[ ||x||_2=|\sum_{k}x_k^2|^{\frac{1}{2}} \]
  • Max.norm \[ ||x||_{\infty}=max_k|x_k| \]
  • L1-Norm \[ ||x||_1=\sum_{k}|x_k| \]
  • Here talks about Vector Norm

Eukl. Norm

代码语言:javascript
复制
import tensorflow as tf
代码语言:javascript
复制
a = tf.ones([2, 2])
a
代码语言:javascript
复制
<tf.Tensor: id=11, shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
       [1., 1.]], dtype=float32)>
代码语言:javascript
复制
tf.norm(a)
代码语言:javascript
复制
<tf.Tensor: id=7, shape=(), dtype=float32, numpy=2.0>
代码语言:javascript
复制
tf.sqrt(tf.reduce_sum(tf.square(a)))
代码语言:javascript
复制
<tf.Tensor: id=16, shape=(), dtype=float32, numpy=2.0>
代码语言:javascript
复制
a = tf.ones([4, 28, 28, 3])
a.shape
代码语言:javascript
复制
TensorShape([4, 28, 28, 3])
代码语言:javascript
复制
tf.norm(a)
代码语言:javascript
复制
<tf.Tensor: id=25, shape=(), dtype=float32, numpy=96.99484>
代码语言:javascript
复制
tf.sqrt(tf.reduce_sum(tf.square(a)))
代码语言:javascript
复制
<tf.Tensor: id=30, shape=(), dtype=float32, numpy=96.99484>

L1 Norm

代码语言:javascript
复制
b = tf.ones([2, 2])
代码语言:javascript
复制
tf.norm(b)
代码语言:javascript
复制
<tf.Tensor: id=45, shape=(), dtype=float32, numpy=2.0>
代码语言:javascript
复制
tf.norm(b, ord=2, axis=1)
代码语言:javascript
复制
<tf.Tensor: id=51, shape=(2,), dtype=float32, numpy=array([1.4142135, 1.4142135], dtype=float32)>
代码语言:javascript
复制
tf.norm(b, ord=1)
代码语言:javascript
复制
<tf.Tensor: id=56, shape=(), dtype=float32, numpy=4.0>
代码语言:javascript
复制
# 列为整体
tf.norm(b, ord=1, axis=0)
代码语言:javascript
复制
<tf.Tensor: id=66, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
代码语言:javascript
复制
# 行为整体
tf.norm(b, ord=1, axis=1)
代码语言:javascript
复制
<tf.Tensor: id=71, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>

reduce_min/max/mean

  • reduce,操作可能会有减维的功能,如[2,2],对行求max,会变成[2]
代码语言:javascript
复制
a = tf.random.normal([4, 10])
代码语言:javascript
复制
tf.reduce_min(a), tf.reduce_max(a), tf.reduce_mean(a)
代码语言:javascript
复制
(<tf.Tensor: id=80, shape=(), dtype=float32, numpy=-2.215113>,
 <tf.Tensor: id=82, shape=(), dtype=float32, numpy=1.9458845>,
 <tf.Tensor: id=84, shape=(), dtype=float32, numpy=-0.045550883>)
代码语言:javascript
复制
# 对某一行求max
tf.reduce_min(a, axis=1), tf.reduce_max(a, axis=1), tf.reduce_mean(a, axis=1)
代码语言:javascript
复制
(<tf.Tensor: id=98, shape=(4,), dtype=float32, numpy=array([-2.215113 , -1.5824796, -1.4861531, -1.3477703], dtype=float32)>,
 <tf.Tensor: id=100, shape=(4,), dtype=float32, numpy=array([0.9380455, 1.1625607, 1.9458845, 1.492183 ], dtype=float32)>,
 <tf.Tensor: id=102, shape=(4,), dtype=float32, numpy=array([-0.48791748,  0.25639585,  0.07420422, -0.02488617], dtype=float32)>)

argmax/argmin

代码语言:javascript
复制
a.shape
代码语言:javascript
复制
TensorShape([4, 10])
代码语言:javascript
复制
tf.argmax(a).shape
代码语言:javascript
复制
TensorShape([10])
代码语言:javascript
复制
# 返回index
tf.argmax(a)
代码语言:javascript
复制
<tf.Tensor: id=112, shape=(10,), dtype=int64, numpy=array([1, 1, 2, 3, 2, 1, 3, 1, 2, 1])>
代码语言:javascript
复制
# 对第1维作用
tf.argmin(a).shape
代码语言:javascript
复制
TensorShape([10])
代码语言:javascript
复制
# 对第2维作用
tf.argmin(a, axis=1).shape
代码语言:javascript
复制
TensorShape([4])

tf.equal

代码语言:javascript
复制
a = tf.constant([1, 2, 3, 2, 5])
代码语言:javascript
复制
b = tf.range(5)
代码语言:javascript
复制
tf.equal(a, b)
代码语言:javascript
复制
<tf.Tensor: id=186, shape=(5,), dtype=bool, numpy=array([False, False, False, False, False])>
代码语言:javascript
复制
res = tf.equal(a, b)
代码语言:javascript
复制
# 对True和False转换为1和0
tf.reduce_sum(tf.cast(res, dtype=tf.int32))
代码语言:javascript
复制
<tf.Tensor: id=191, shape=(), dtype=int32, numpy=0>

Accuracy

代码语言:javascript
复制
a = tf.random.normal([2, 3])
a
代码语言:javascript
复制
<tf.Tensor: id=198, shape=(2, 3), dtype=float32, numpy=
array([[ 0.25201225, -1.3897187 ,  0.29240564],
       [-1.0671712 ,  2.1487093 ,  0.690736  ]], dtype=float32)>
代码语言:javascript
复制
pred = tf.cast(tf.argmax(a, axis=1), dtype=tf.int32)
pred.shape
代码语言:javascript
复制
TensorShape([2])
代码语言:javascript
复制
y = tf.constant([2, 1])
y
代码语言:javascript
复制
<tf.Tensor: id=163, shape=(2,), dtype=int32, numpy=array([2, 1], dtype=int32)>
代码语言:javascript
复制
tf.equal(y, pred)
代码语言:javascript
复制
<tf.Tensor: id=165, shape=(2,), dtype=bool, numpy=array([ True,  True])>
代码语言:javascript
复制
correct = tf.reduce_sum(tf.cast(tf.equal(y, pred), dtype=tf.int32))
代码语言:javascript
复制
correct
代码语言:javascript
复制
<tf.Tensor: id=170, shape=(), dtype=int32, numpy=2>
代码语言:javascript
复制
correct / 2
代码语言:javascript
复制
<tf.Tensor: id=175, shape=(), dtype=float64, numpy=1.0>

tf.unique

  • 用于去重
代码语言:javascript
复制
a = tf.range(5)
a
代码语言:javascript
复制
<tf.Tensor: id=235, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
代码语言:javascript
复制
# 返回索引
tf.unique(a)
代码语言:javascript
复制
Unique(y=<tf.Tensor: id=237, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>, idx=<tf.Tensor: id=238, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>)
代码语言:javascript
复制
a = tf.constant([4, 2, 2, 4, 3])
a
代码语言:javascript
复制
<tf.Tensor: id=226, shape=(5,), dtype=int32, numpy=array([4, 2, 2, 4, 3], dtype=int32)>
代码语言:javascript
复制
res = tf.unique(a)
代码语言:javascript
复制
Unique(y=<tf.Tensor: id=228, shape=(3,), dtype=int32, numpy=array([4, 2, 3], dtype=int32)>, idx=<tf.Tensor: id=229, shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2], dtype=int32)>)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Outline
  • Vector norm
    • Eukl. Norm
      • L1 Norm
      • reduce_min/max/mean
      • argmax/argmin
      • tf.equal
      • Accuracy
      • tf.unique
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档