TensorFlow入门深度学习–06.可视化工具TensorBoard

文章列表
1.TensorFlow入门深度学习–01.基础知识. .
2.TensorFlow入门深度学习–02.基础知识. .
3.TensorFlow入门深度学习–03.softmax-regression实现MNIST数据分类. .
4.TensorFlow入门深度学习–04.自编码器(对添加高斯白噪声后的MNIST图像去噪).
5.TensorFlow入门深度学习–05.多层感知器实现MNIST数据分类.
6.TensorFlow入门深度学习–06.可视化工具TensorBoard.
7.TensorFlow入门深度学习–07.卷积神经网络概述.
8.TensorFlow入门深度学习–08.AlexNet(对MNIST数据分类).
9.TensorFlow入门深度学习–09.tf.contrib.slim用法详解.
10.TensorFlow入门深度学习–10.VGGNets16(slim实现).
11.TensorFlow入门深度学习–11.GoogLeNet(Inception V3 slim实现).

TensorFlow入门深度学习–06.可视化工具TensorBoard

TensorBoard是TensorFlow的可视化工具,可以将训练过程中的数据按一下几种形式展现出来。

这里写图片描述

为了对TensorBoard的功能有直观细致的理解,我们首先来看看常用的计算图Graphs功能。以上一小节的MLP为例,在第23行之后添加如下这3行语句,修改后为MLP_02.py文件,通过tf.summary.FileWriter将计算图sess.graph写入train_writer中,且train_writer路径位于当前路径下的train文件夹中,然后在关闭train_writer:

# Write Graphs
train_writer = tf.summary.FileWriter('./train', sess.graph)
train_writer.close()

运行之后,可以看到当前路径下生成了train文件夹。然后再启动一个命令行窗口,激活虚拟环境,键入命令tensorboard,也可通过bat文件运行,在bat文件中输入“tensorboard –logdir=./train”,其参数logdir指出log文件的存放目录,可以只给出其上级目录,TensorBoard会自动递归扫描目录:当TensorBoard服务器顺利启动后,即可打开浏览器输入地址:http://127.0.0.1:6006/查看【注意在Windows环境下输入http://0.0.0.0:6006/无效。】
这时在graphs选项下会得到如下的计算图。


这里写图片描述

上面的计算图看不出主次,我们可以通过添加with tf.name_scope限定命名空间(修改后的代码为MLP_03.py),从而使得计算图显示的具有层次感。添加的with tf.name_scope限定如下:


这里写图片描述

再次运行后得到计算图如下,对比上面的计算图,现在的计算图层次清晰多了,具体命名空间中的结构可通过点击图右上角展开图标得到:

这里写图片描述

代码应该编写简洁,尽量减少重复,因此将上面程序中的两个全连接层的定义抽象成一个函数,整理后的代码from

tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
def FC_layer(layer_name, input_tensor, in_units, out_units, activator = tf.nn.relu):
    with tf.name_scope(layer_name):
        with tf.name_scope('weights'):
            weights = tf.Variable(tf.truncated_normal([in_units, out_units], stddev=0.1))
        with tf.name_scope('bias'):
            bias = tf.Variable(tf.zeros([out_units]))
        with tf.name_scope('activations'):
            activations = activator(tf.matmul(input_tensor, weights) + bias)
        return activations
mnist = input_data.read_data_sets("./../MNISTDat", one_hot=True)
sess = tf.InteractiveSession()
# Create the model
in_units = 784
h1_units = 300
o_units = 10
batch_size = 100
with tf.name_scope('input'):
    with tf.name_scope('input_images'):
        x = tf.placeholder(tf.float32, [None, in_units])
    with tf.name_scope('input_labels'):
        y_ = tf.placeholder(tf.float32, [None, o_units])
    with tf.name_scope('input_keep_prob'):
        keep_prob = tf.placeholder(tf.float32)
hidden1 = FC_layer('layer1', x, in_units, h1_units)
with tf.name_scope('doupout'):
    hidden1_drop = tf.nn.dropout(hidden1, keep_prob)
y = FC_layer('layer2', hidden1_drop, h1_units, o_units, tf.nn.softmax)
# Define loss and optimizer
with tf.name_scope('cross_entropy'):
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
with tf.name_scope('train'):
    train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    with tf.name_scope('accuracy'):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Write Graphs
train_writer = tf.summary.FileWriter('./train', sess.graph)
train_writer.close()
# Train
tf.global_variables_initializer().run()
for i in range(3000):
  batch_xs, batch_ys = mnist.train.next_batch(batch_size)
  train_step.run({x: batch_xs, y_: batch_ys, keep_prob: 0.75})
# Test trained model
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

计算图变为:

这里写图片描述

上面介绍了命名空间在计算图层次化显示中的应用,下面继续介绍数据的存储及显示方法。数据处理方式众多,包含标量、图片、音频、数据分布、直方图和嵌入向量,对于MNIST数据分类问题,最直观的想法就是将数据以图片的形式展现出来,由于数据输入格式是向量,首先通过reshape转换成二维图像数据,通过tf.summary.image记录20组二维图像数据,每组图像数据含有10幅图像。并通过tf.summary.merge_all获得所有可视化数据的集合,这样就获得了图像可视化数据。

这里写图片描述

此外,我们还通常希望看到训练误差以及测试准确率随着迭代的变化,而他们在单个迭代步上仅是单个数值,所以用标量来记录他们,用tf.summary.scalar()即可,其中第一个参数为标量名,第二个参数为待记录的标量,改动如下图中的橘色区域所示(右边的代码对应MLP_06.py),可以看出,由于有上面的铺垫,只需要加两行话即可:

这里写图片描述

TensorFlow实现 :https://pan.baidu.com/s/14A91inmZmSC55dgDv8ZZ3Q

猜你喜欢

转载自blog.csdn.net/drilistbox/article/details/79727090
今日推荐