Amanda-Zhang
追梦女一枚

大话Tensorboard

2020-06-03 tensorflow
Word count: 778 | Reading time: 3min

TensorBoard概念

​ 它是TensorFlow提供的一个可视化工具,它可以有效地展示TensorFlow在运算过程中的计算图,各种指标随着时间的变化趋势以及训练中使用到的图像等信息。

TensorBoard原理

​ 通过读取相应的日志数据,对齐进行可视化展示。

运行TensorBoard

1.打开电脑的命令窗口,进入到项目所在目录;
2.输入命令”tensorboard –logdir=/path/to/log”并运行
3.通过浏览器打开localhost:6006

TensorBoard监控范围

1.计算图结构

2.计算节点状态

3.变量(标量、张量、输入)

功能:主要通过调用tf.summary()类下各方法

计算图可视化

1
2
3
4
5
6
7
8
import tensorflow as tf 
#定义一个简单的计算图,实现向量加法的操作
input1 = tf.constant([1.0,2.0,3.0],name = 'input1')
input2 = tf.Variable(tf.random_uniform([3]),name = 'input2')
output = tf.add_n([input1,input2],name = 'add')
#生成一个写日志的writer,并且将当前的tensorflow计算图写入日志
writer = tf.summary.FileWriter("/path/to/log", tf.get_default_graph())
writet.close()

​ 以上代码输出的结构图在Tensorboard中显示如下:

1591151402995

命名空间简化计算图

​ TensorBoard支持通过TensorFlow命名空间来整理可视化效果图上的节点。将同一个命名空间下的所有节点会被缩略成一个节点,只有顶层命名空间中的节点才会被显示。

1
2
3
4
5
6
7
8
9
10
11
import tensorflow as tf 
#定义一个简单的计算图,实现向量加法的操作
#将输入定义放在各自的命名空间中
with tf.name_scope("input1")
input1 = tf.constant([1.0,2.0,3.0],name = 'input1')
with tf.name_scope("input2")
input2 = tf.Variable(tf.random_uniform([3]),name = 'input2')
output = tf.add_n([input1,input2],name = 'add')
#生成一个写日志的writer,并且将当前的tensorflow计算图写入日志
writer = tf.summary.FileWriter("/path/to/log", tf.get_default_graph())
writet.close()

​ 我们来看一下区别:(总的节点可以点开查看具体变量)

1591151427818

变量监控

1591151462187

图像监控

​ 将输入向量还原成图片的像素矩阵,并通过tf.summary.image将当前图片信息写入日志。

1
2
3
with tf.name_scope(' input_ reshape' ):
image_shaped_input = tf.reshape(x,[-1,28,28,1] )
tf.summary.image('input',image_shaped_input, 10)

标量监控

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#计算交叉熵并定义生成交叉熵监控日志的操作
with tf.name_scope(cross_ entropy):
cross_entropy = tf . reduce mean(\
tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y))
tf.summary.scalar('cross entropy' ,cross_entropy)

#计算模型在当前给定数据的正确率,并定义生成正确率监控日志的操作
with tf.name_scope('accuracy'):
with tf.name_scope('correct_prediction'):
correct_prediction = tf.equal(tf.arg_max(y,1),tf.argmax(y_,1))
with tf.name scope('accuracy'):
accuracy = tf.reduce_mean(
tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy',accuracy)

​ 监控情况如下所示:

1591151486750

张量监控

1
2
3
4
5
6
7
8
9
with tf.name_scope('Wx_plus_b'):
preactivate = tf.matmul(input_tensor,weights)+biases
#记录神经网络输出节点在经过激活函数之前的分布
tf.summary.histogram(layer_name+'/pre_activations',preactivate)
activations = act(preactivate,name='activation')
#记录神经网络输出节点在经过激活函数之后的分布
tf.summary.histogram(layer_name+'/activations',activations)

return activations

​ 监控情况如下所示:

1591151505416

小结

TensorBoard可视化分析工具

分析内容

​ 计算图状态

​ 标量趋势

​ 张量统计信息

​ 输入文件(图像、文本、音频)

Author: Amanda-Zhang

Link: http://chunchunya.github.io/2020/06/03/tensorflow/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
词向量(NLP入门)
NextPost >
个人博客注意事项
CATALOG
  1. 1. TensorBoard概念
  2. 2. TensorBoard原理
  3. 3. 运行TensorBoard
  4. 4. TensorBoard监控范围
  5. 5. 计算图可视化
  6. 6. 命名空间简化计算图
  7. 7. 变量监控
    1. 7.1. 图像监控
    2. 7.2. 标量监控
    3. 7.3. 张量监控
  8. 8. 小结