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")
我们可以通过点击节点来查看它的值和类型:
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中的操作# create a tensor of shape and all elements are zeros
tf.zeros([2, 3], tf.int32) ==> [[0, 0, 0], [0, 0, 0]]
# 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]]
# create a tensor of shape and all elements are ones
tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]]
# 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]]
# 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)
# 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]
# 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
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([a, b, b]) => equivalent to a + b + b
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》
待续、