TensorBoard是TensorFlow下的一个可视化的工具,能够帮助我们在训练大规模神经网络过程中出现的复杂且不好理解的运算。TensorBoard能展示你训练过程中绘制的图像、网络结构等。
图1 神将网络结构图
首先我们给出一个创建神经网络的python代码作为一个案例:
# -*- coding:utf-8 -*-
import tensorflow as tf
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
return outputs
# define placeholder for inputs to network
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
ys = tf.placeholder(tf.float32, [None, 1], name='y_input')
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# the error between prediciton and real data
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.Session()
writer = tf.train.SummaryWriter("logs/", sess.graph)
# important step
sess.run(tf.initialize_all_variables())
在linux终端输入:python 文件名.py 执行以后就会在这个python文件的一个目录下生成一个logs文件夹,在其中就会生成以下箭头所指向的这个文件。
下面介绍一下官方文档上启动TensorBoard的方法:
第一步:定位到你训练后log文件保存的位置;
我的文件目录如上图。
第二步:键入命令行,启动TensorBoard;
命令行是: tensorboard --logdir='logs/' 回车,就自动加载该文件。
然后你复制网址http://0.0.0.0:6006到firefox浏览器中就可以打开TensorBoard,这里的链接视生成的而定。
最后附上结果图:
图中的各层都是可以展开,查看里面具体的内容。
当tensorflow环境在windows系统上的时候,上面的操作大体都是相同的,唯一的区别是路径的写法。
例如,如果用相对路径写法,那么我需要提前进入到D:\repository\tensorflowbook\chapters这个目录下
在windows下输入命令:
然后,我们就可以通过简短的相对路径访问tensorboard图了,注意路径没有引号。
使用绝对路径(记住,只有这一种路径斜杠方法):
tensorboard --logdir=D://repository//tensorflowbook//chapters//my_graph
运行效果: