作用:
在复杂的问题中,网络往往都是很复杂的,为了方便调试参数以及调整网络结构,我们需要将计算图可视化出来,以便能够更好的进行下一步的决策。tensorboard是可视化tensorflow模型的训练过程的工具,在安装tensorflow的同时就已经安装好了tensorboard。tensorboard能够有效地展示tensorflow在运行过程中的计算图、各种指标随着时间的变化趋势以及训练中使用到的数据信息
工作流程:
tensorboard是通过一些操作将数据记录到文件中,然后再读取文件来完成作图的,关键的几个步骤如下:
(1)summary:在定义计算图的时候,在适当的位置加上一些summary操作
(2)merge:在训练的时候可能加了多个summary操作,需要使用tf.summary.merge_all将这些summary操作合成一个操作,由它来产生所有的summary数据
(3)run:在没有运行的时候这些操作是不会执行任何东西的,仅仅是定义了一下,在运行(开始训练)的时候,我们需要通过tf.summary.FileWrite()指定一个目录告诉程序把产生的文件放到哪,然后在运行的时候使用 add_summary() 来将某一步的 summary 数据记录到文件中
运行:
当训练完成后,在命令行使用 tensorboard --logdir=path/to/log-directory 来启动 TensorBoard,按照提示在浏览器打开页面,注意把 path/to/log-directory 替换成你上面指定的目录。
Tensorboard可以记录与展示以下数据形式:
(1)标量Scalars 存储和显示诸如学习率和损失等单个值的变化趋势
- 图片Images 对于输入是图像的模型,显示某一步输入给模型的图像
(3)音频Audio 显示可播放的音频
(4)计算图Graph 显示代码中定义的计算图,也可以显示包括每个节点的计算时间、内存使用等情况
(5)数据分布Distribution 显示模型参数随迭代次数的变化情况
(6)直方图Histograms 显示模型参数随迭代次数的变化情况
(7)嵌入向量Embeddings 在3D或者2D图中展示高维数据
(8)文本Text 显示保存的一小段文字
例子1:
具体的代码如下所示:
with tf.name_scope('graph') as scope: a = tf.constant(1,name='a') b = tf.constant(2,name='b') add = tf.add(a,b,name='add') ##生成一个写日志的writer,并将当前的tensorflow计算图写入日志 writer = tf.summary.FileWriter('F:/logs',sess.graph) init = tf.global_variables_initializer() tf.name_scope函数是作用域名,上述代码中在graph中有3个op:a、b、add,用tf函数内部的name参数命名,这些会在tensorboard中显示出来
在pycharm中的terminal中输入:
tensorboard --logdir=F:\logs
如下所示:

在上图中的红色框中在谷歌浏览器中输入相对应的网址,则显示的结果如下图所示:


注意:
写日志的位置中不要出现中文的地址,否则很有可能报错,本人刚开始时用的中文地址,结果一直没有显示出来,稍微注意一下下
例子2:
这个例子来自:
https://www.cnblogs.com/fydeblog/p/7429344.html
具体的代码如下所示:
## prepare the original data with tf.name_scope('data'): x_data = np.random.rand(100).astype(np.float32) with tf.name_scope('parameters'): weight = tf.Variable(tf.random_uniform([1],-1.0,1.0)) bias = tf.Variable(tf.zeros([1])) with tf.name_scope('y_prediction'): y_prediction = weight*x_data+bias with tf.name_scope('loss'): loss = tf.reduce_mean(tf.square(y_data-y_prediction)) optimizer = tf.train.GradientDescentOptimizer(0.5) #creat train ,minimize the loss with tf.name_scope('train'): train = optimizer.minimize(loss) with tf.name_scope('init'): init = tf.global_variables_initializer() writer = tf.summary.FileWriter('F:/logs', sess.graph) print (step ,'weight:',sess.run(weight),'bias:',sess.run(bias))

先初始化参数,算出预测,计算损失,然后训练,更新相应的参数
例子3:
针对例子2进行了一些修改,修改之后的代码如下所示:
修改的部分在代码中相应的做了注释
## prepare the original data with tf.name_scope('data'): x_data = np.random.rand(100).astype(np.float32) with tf.name_scope('parameters'): weight = tf.Variable(tf.random_uniform([1],-1.0,1.0)) tf.summary.histogram('weights',weight) ##新增加的代码 bias = tf.Variable(tf.zeros([1])) tf.summary.histogram('bias',bias) ##新增加的代码 with tf.name_scope('y_prediction'): y_prediction = weight*x_data+bias with tf.name_scope('loss'): loss = tf.reduce_mean(tf.square(y_data-y_prediction)) tf.summary.scalar('loss',loss) ##新增加的代码 optimizer = tf.train.GradientDescentOptimizer(0.5) #creat train ,minimize the loss with tf.name_scope('train'): train = optimizer.minimize(loss) with tf.name_scope('init'): init = tf.global_variables_initializer() merged = tf.summary.merge_all() ##新增加的代码 writer = tf.summary.FileWriter('F:/logs', sess.graph) result = sess.run(merged) writer.add_summary(result,step) ##新增加的代码 将打印代码进行了去除操作
scalar中的loss训练图如下所示:

distribution中的weight和bias的训练图如下所示:

histogram中的weight和bias的训练图如下所示:

我们可以根据训练图,对模型的学习情况进行评估,比如我们看损失训练图,可以看到现在是一条慢慢变小的曲线,最后的值趋近于0(在现实情况,往往都有误差,趋近于0反而是过拟合),这符合本意,就是要最小化loss,如果loss的曲线最后没有平滑趋近一个数,则说明训练的力度还不够,还要加大次数,如果loss还很大,说明学习算法不太理想,需改变当前的算法,去实现更小的loss,另外两幅与loss类似,最后都是要趋近一个数的,没有趋近和上下浮动都是有问题的。
|