TensorBoard
TensorFlow自带的可视化工具,能够以直观的流程图的方式,清楚展示出整个神经网络的结构和框架,便于理解模型和发现问题。
- 可视化学习:
- 图的直观展示:
- 直方图信息中心:
启动TensorBoard
- 使用命令“tensorboard --logdir=path/to/log-directory”(或者“python -m tensorboard.main”);
- 参数logdir指向FileWriter将数据序列化的目录,建议在logdir上一级目录执行此命令;
- TensorBoard运行后,在浏览器输入“localhost:6006”即可查看TensorBoard;
帮助信息
- 使用“tensorboard --help”查看tensorboard的详细参数
示例
程序代码
1 # coding=utf-8 2 from __future__ import print_function 3 import tensorflow as tf 4 import numpy as np 5 import matplotlib.pyplot as plt 6 import os 7 8 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 9 10 11 # ### 添加神经层12 13 14 def add_layer(inputs, in_size, out_size, n_layer, activation_function=None): # 参数n_layer用来标识层数15 layer_name = 'layer{}'.format(n_layer)16 with tf.name_scope(layer_name): # 使用with tf.name_scope定义图层,并指定在可视化图层中的显示名称17 with tf.name_scope('weights'): # 定义图层并指定名称,注意这里是上一图层的子图层18 Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W') # 参数name指定名称19 tf.summary.histogram(layer_name + '/weights', Weights) # 生成直方图summary,指定图表名称和记录的变量20 with tf.name_scope('biases'): # 定义图层并指定名称21 biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b') # 参数name指定名称22 tf.summary.histogram(layer_name + '/biases', biases) # 生成直方图summary23 with tf.name_scope('Wx_plus_b'): # 定义图层并指定名称24 Wx_plus_b = tf.matmul(inputs, Weights) + biases25 if activation_function is None:26 outputs = Wx_plus_b27 else:28 outputs = activation_function(Wx_plus_b)29 tf.summary.histogram(layer_name + '/outputs', outputs) # 生成直方图summary30 return outputs31 32 33 # ### 构建数据34 x_data = np.linspace(-1, 1, 300, dtype=np.float32)[:, np.newaxis]35 noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)36 y_data = np.square(x_data) - 0.5 + noise37 38 # ### 搭建网络39 with tf.name_scope('inputs'): # 定义图层并指定名称40 xs = tf.placeholder(tf.float32, [None, 1], name='x_input') # 指定名称为x_input,也就是在可视化图层中的显示名称41 ys = tf.placeholder(tf.float32, [None, 1], name='y_input') # 指定名称为y_input42 43 h1 = add_layer(xs, 1, 10, n_layer=1, activation_function=tf.nn.relu) # 隐藏层44 prediction = add_layer(h1, 10, 1, n_layer=2, activation_function=None) # 输出层45 46 with tf.name_scope('loss'): # 定义图层并指定名称47 loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),48 reduction_indices=[1]))49 tf.summary.scalar('loss', loss) # 用于标量的summary,loss在TensorBoard的event栏50 51 with tf.name_scope('train'): # 定义图层并指定名称52 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)53 54 sess = tf.Session()55 merged = tf.summary.merge_all() # 合并之前定义的所有summary操作56 writer = tf.summary.FileWriter("logs/", sess.graph) # 创建FileWriter对象和event文件,指定event文件的存放目录57 init = tf.global_variables_initializer()58 sess.run(init)59 60 # ### 结果可视化61 fig = plt.figure()62 ax = fig.add_subplot(1, 1, 1)63 ax.scatter(x_data, y_data)64 plt.ion()65 plt.show()66 67 # ### 训练68 for i in range(1001):69 sess.run(train_step, feed_dict={xs: x_data, ys: y_data})70 if i % 50 == 0:71 result = sess.run(loss, feed_dict={xs: x_data, ys: y_data})72 print("Steps:{} Loss:{}".format(i, result))73 rs = sess.run(merged, feed_dict={xs: x_data, ys: y_data}) # 在sess.run中运行74 writer.add_summary(rs, i)75 try:76 ax.lines.remove(lines[0])77 except Exception:78 pass79 prediction_value = sess.run(prediction, feed_dict={xs: x_data})80 lines = ax.plot(x_data, prediction_value, 'r-', lw=5)81 plt.pause(0.2)82 83 # ### TensorBoard84 # TensorFlow自带的可视化工具,能够以直观的流程图的方式,清楚展示出整个神经网络的结构和框架,便于理解模型和发现问题;85 # - 可视化学习:https://www.tensorflow.org/guide/summaries_and_tensorboard86 # - 图的直观展示:https://www.tensorflow.org/guide/graph_viz;87 # - 直方图信息中心:https://www.tensorflow.org/guide/tensorboard_histograms88 #89 # ### 启动TensorBoard90 # 使用命令“tensorboard --logdir=path/to/log-directory”(或者“python -m tensorboard.main”);91 # 参数logdir指向FileWriter将数据序列化的目录,建议在logdir上一级目录执行此命令;92 # TensorBoard运行后,在浏览器输入“localhost:6006”即可查看TensorBoard;
程序运行结果
运行过程中显示的图形:
某一次运行的命令行输出:
Steps:0 Loss:0.19870562851428986Steps:50 Loss:0.006314810831099749Steps:100 Loss:0.0050856382586061954Steps:150 Loss:0.0048223137855529785Steps:200 Loss:0.004617161583155394Steps:250 Loss:0.004429362714290619Steps:300 Loss:0.004260621033608913Steps:350 Loss:0.004093690309673548Steps:400 Loss:0.003932977095246315Steps:450 Loss:0.0038178395479917526Steps:500 Loss:0.003722294932231307Steps:550 Loss:0.003660505171865225Steps:600 Loss:0.0036110866349190474Steps:650 Loss:0.0035716891288757324Steps:700 Loss:0.0035362064372748137Steps:750 Loss:0.0034975067246705294Steps:800 Loss:0.003465239657089114Steps:850 Loss:0.003431882942095399Steps:900 Loss:0.00339301535859704Steps:950 Loss:0.0033665322698652744Steps:1000 Loss:0.003349516075104475
生成的TensorBoard文件:
(mlcc) D:\Anliven\Anliven-Code\PycharmProjects\TempTest>dir logs 驱动器 D 中的卷是 Files 卷的序列号是 ACF9-2E0E D:\Anliven\Anliven-Code\PycharmProjects\TempTest\logs 的目录2019/02/24 23:41.2019/02/24 23:41 ..2019/02/24 23:41 137,221 events.out.tfevents.1551022894.DESKTOP-68OFQFP 1 个文件 137,221 字节 2 个目录 219,401,887,744 可用字节(mlcc) D:\Anliven\Anliven-Code\PycharmProjects\TempTest>
启动与TensorBoard
执行下面的启动命令,然后在浏览器中输入“http://localhost:6006/”查看。
(mlcc) D:\Anliven\Anliven-Code\PycharmProjects\TempTest>tensorboard --logdir=logsTensorBoard 1.12.0 at http://DESKTOP-68OFQFP:6006 (Press CTRL+C to quit)
栏目Scalars
栏目Graphs
- 通过鼠标滑轮可以改变显示大小和位置
- 鼠标双击“+”标识可以查看进一步的信息
- 可以将指定图层从主图层移出,单独显示
栏目Distributions
栏目histograms