首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >斯坦福tensorflow教程(二) tensorflow相关运算1.认识下TensorBoard2.常量op3. 数学运算数据类型

斯坦福tensorflow教程(二) tensorflow相关运算1.认识下TensorBoard2.常量op3. 数学运算数据类型

作者头像
致Great
发布2018-06-14 15:30:11
7180
发布2018-06-14 15:30:11
举报
文章被收录于专栏:程序生活程序生活

1.认识下TensorBoard

TensorFlow不仅是一个软件库,而是一整套包括TensorFlow、TensorBoard、Tensor Serving在内的软件包。为了更大程度地利用TensorFlow,我们应该了解如何将它们串联起来应用。在和一部分,我们来探索下TensorBoard。 TensorBoard是一个图(graph)可视化软件,在(安装TensorFlow的时候会默认安装)。下面是谷歌的介绍: The computations you'll use TensorFlow for - like training a massive deep neural network - can be complex and confusing. To make it easier to understand, debug, and optimize TensorFlow programs, we've included a suite of visualization tools called TensorBoard.

在运行一个包含一些运算的TensorFlow程序时,这些运算会导出成一个时间日志文件。TensorBoard 可以将这些日志文件可视化,以便更好观察程序的机构以及运行表现。TensorBoard和TensorFlow一并使用,会使工作更加有趣和更具生产力。

下面开始我们第一个TensorFlow程序,并使用TensorBoard可视化。

import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
with tf.Session() as sess:
    print(sess.run(x))

执行结果

为了将上面程序可视化,我们需要下面一行程序将日志写入文件:

writer = tf.summary.FileWriter([logdir], [graph])

[graph] 是运行程序所在的图,可以通过tf.get_default_graph()返回程序默认图,也可以通过sess.graph返回当前会话中运行的图,后者需要你自己先创建一个session。无论哪种方式,都要你在定义graph之后创建一个writer,否则TensorBoard不能可视化程序。

[logdir]是存储日志文件的路径

import tensorflow as tf

a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())
with tf.Session() as sess:
    # writer = tf.summary.FileWriter('./graphs', sess.graph) # if you prefer creating your writer using session's graph
    print(sess.run(x))
writer.close()

然后在cmd运行程序

$ python3 [my_program.py] 
$ tensorboard --logdir="./graphs" --port 6006

在浏览器打开

可视化效果如下

“Const”和“Const_1”指的是a和b,节点“Add”指的是x,为了更好理解运算,我们可以给ops命名。

a = tf.constant(2, name="a")
b = tf.constant(2, name="b")
x = tf.add(a, b, name="add")

我们可以通过点击节点来查看它的值和类型:

2.常量op

op:图中的节点(operation 的缩写). 下面是创建constant的操作 tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)

  • 常数 num = tf.constant(2, name="num")
  • 向量 # constant of 1d tensor (vector) a = tf.constant([2, 2], name="vector")
  • 矩阵 # constant of 2x2 tensor (matrix) b = tf.constant([[0, 1], [2, 3]], name="matrix") 可以通过填充创建tensor,类似于numpy中的操作
  • tf.zeros(shape, dtype=tf.float32, name=None)
# create a tensor of shape and all elements are zeros
tf.zeros([2, 3], tf.int32) ==> [[0, 0, 0], [0, 0, 0]]
  • tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)
# create a tensor of shape and type (unless type is specified) as the input_tensor but all elements are zeros.
# input_tensor [[0, 1], [2, 3], [4, 5]]
tf.zeros_like(input_tensor) ==> [[0, 0], [0, 0], [0, 0]]
  • tf.ones(shape, dtype=tf.float32, name=None)
# create a tensor of shape and all elements are ones
tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]]
  • tf.ones_like(input_tensor, dtype=None, name=None, optimize=True)
# create a tensor of shape and type (unless type is specified) as the input_tensor but all elements are ones.
# input_tensor is [[0, 1], [2, 3], [4, 5]]
tf.ones_like(input_tensor) ==> [[1, 1], [1, 1], [1, 1]]
  • tf.fill(dims, value, name=None)
# create a tensor filled with a scalar value.
tf.fill([2, 3], 8) ==> [[8, 8, 8], [8, 8, 8]]
  • tf.lin_space(start, stop, num, name=None)
tf.lin_space(start, stop, num, name=None)
# create a sequence of num evenly-spaced values are generated beginning at start. If num > 1, the values in the sequence increase by (stop - start) / (num - 1), so that the last one is exactly stop.
# comparable to but slightly different from numpy.linspace

tf.lin_space(10.0, 13.0, 4, name="linspace") ==> [10.0 11.0 12.0 13.0]
  • tf.range([start], limit=None, delta=1, dtype=None, name='range')
# create a sequence of numbers that begins at start and extends by increments of delta up to but not including limit
# slight different from range in Python

# 'start' is 3, 'limit' is 18, 'delta' is 3
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
# 'start' is 3, 'limit' is 1,  'delta' is -0.5
tf.range(start, limit, delta) ==> [3, 2.5, 2, 1.5]
# 'limit' is 5
tf.range(limit) ==> [0, 1, 2, 3, 4]

不像Numpy或者Python其他序列,TensorFlow序列不能迭代

for _ in np.linspace(0, 10, 4): # OK
for _ in tf.linspace(0.0, 10.0, 4): # TypeError: 'Tensor' object is not iterable.

for _ in range(4): # OK
for _ in tf.range(4): # TypeError: 'Tensor' object is not iterable.

也可以生成随机constant,具体请见API

tf.random_normal
tf.truncated_normal
tf.random_uniform
tf.random_shuffle
tf.random_crop
tf.multinomial
tf.random_gamma
tf.set_random_seed

3. 数学运算

  • division相关操作 TensorFlow 运算相当完美与标准,全部内容在这 tf.div(a/b)是TensorFlow的风格,返回a除以b的商数,比如8/3,返回2;

tf.divide(a/b)才和Python的风格一样,a除以b

a = tf.constant([2, 2], name='a')
b = tf.constant([[0, 1], [2, 3]], name='b')
with tf.Session() as sess:
    print(sess.run(tf.div(b, a)))             ⇒ [[0 0] [1 1]]
    print(sess.run(tf.divide(b, a)))          ⇒ [[0. 0.5] [1. 1.5]]
    print(sess.run(tf.truediv(b, a)))         ⇒ [[0. 0.5] [1. 1.5]]
    print(sess.run(tf.floordiv(b, a)))        ⇒ [[0 0] [1 1]]
    print(sess.run(tf.realdiv(b, a)))         ⇒ # Error: only works for real values
    print(sess.run(tf.truncatediv(b, a)))     ⇒ [[0 0] [1 1]]
    print(sess.run(tf.floor_div(b, a)))       ⇒ [[0 0] [1 1]]
  • tf.add_n tf.add_n([a, b, b]) => equivalent to a + b + b
  • 点积 Dot
a = tf.constant([10, 20], name='a')
b = tf.constant([2, 3], name='b')
with tf.Session() as sess:
    print(sess.run(tf.multiply(a, b)))           ⇒ [20 60] # element-wise multiplication
    print(sess.run(tf.tensordot(a, b, 1)))       ⇒ 80 # 按列相乘然后相加

下面是TensorFlow中运算表格,来自《Fundamentals of Deep Learning》

数据类型

待续、

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.05.09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.认识下TensorBoard
  • 2.常量op
  • 3. 数学运算
  • 数据类型
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档