TensorFlow学习笔记(八)——TensorBoard可视化

前言:

       训练神经网络十分复杂,有时需要几天甚至几周的时间。为了更好的管理、调试和优化神经网络的训练过程,TensorFlow提供了一个可视化工具TensorBoard。本文将介绍TensorFlow 的可视化工具 TensorBoard。TensorBoard TensorFlow 自带的工具,不需要额外的安装过程。虽然TensorBoardTensorFlow运行在不同的进程中, 但是TensorBoard会实时读取TensorFlow程序输出的日志文件从而获取最新的TensorFlow 程序运行状态。通过TensorBoard,—方面可以更好地了解TensorFlow计算图的结构以及每个TensorFlow计算节点在运行时的时间、内存消耗。另一方面也可以通过TensorBoard 可视化神经网络模型训练过程中各种指标的变化趋势,直观地了解神经网络的训练情况。

一、TensorFlow计算图可视化

        以下代码展示了一个简单的TensorFlow程序,在这个程序中完成了 TensorBoard日志输出的功能:

# -*- coding: utf-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")

#将当前的TensorFlow计算图写入日志
writer = tf.summary.FileWriter(r"C:\Users\Administrator\Desktop\code\tensorflow\TensorBoard_test\tensorboard_model",
                               tf.get_default_graph())
writer.close()

      以上程序输出了TensorFlow计算图的信息,所以运行TensorBoard时,可以看到这个向量相加程序计算图可视化之后的结果。TensorBoard不需要额外的安装过程,在 TensorFlow安装完成时,TensorBoard会被自动安装。

      注意,TensorBoard必须要在终端启动,打开cmd,输入tensorboard --logdir = 路径,如下图:


启动TensorBoard,会得到一个网址,如下图:


我们看下C:\Users\Administrator\Desktop\code\tensorflow\TensorBoard_test\tensorboard_model路径下的文件:


有两个是因为我运行了两次程序。

打开 网址http://3W34K4YR8TP6AN9:6006,如下所示:


      进入GRAPHS栏,便看到上面程序TensorFlow计算图的可视化结果。


二、命名空间与TensorBoard图上节点      

      为了更好地组织可视化效果图中的计算节点,TensorBoard支持通过TensorFlow命名空间来整理可视化效果图上的节点。在TensorBoard的默认视图中,TensorFlow计算图中同一个命名空间下的所有节点会被缩略成一个节点,只有顶层命名空间中的节点才会被显 示在TensorBoard可视化效果图上。

2.1 命名空间

我们知道,变量的命名空间可以通过 tf.variable_scope 函数来,除了tf.variable_scope 函数,tf.name_scope 函数也提供了命名空间管理的功能。这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。以下代码简单地说明了这两个函数的区别:

# -*- coding: utf-8 -*-

import tensorflow as tf


with tf.variable_scope("namespace_1"):
    #在命名空间namespace_1下获取变量“bar”,于是得到的变量名称为“namespace_1/bar”。
    a = tf.get_variable("bar",	[1])
    print(a.name) 	              # 输出:namespace_1/bar:0
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        print(sess.run(a))
        print("******************")


with tf.variable_scope("namespace_2"):
    # 在命名空间namespace_2下获取变量“bar”,于是得到的变量名称为“namespace_2/bar”。
    # 此时变量“namespace_2/bar”和变量“namespace_1/bar”并不冲突,于是可以正常运行。
    b = tf.get_variable("bar",[1])
    print (b.name)	             # 输出:namespace_2/bar: 0
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        print(sess.run(b))
        print("******************")


with tf.name_scope("namespace_3"):
    # 使用tf.Variable函数生成变量会受tf.name_scope影响,
    # 于是这个变量的名称为 “namespace_3/Variable”。
    a = tf.Variable([1])
    print(a.name) 	             # 输出:namespace_3/Variable:0
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        print(sess.run(a))
        print("******************")

# tf.get_variable函数不受tf.name_scope函数的影响,于是变量并不在a这个命名空间中。
a = tf.get_variable("b", [1])
print (a.name)         	# 输出:b:0
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    print(sess.run(a))
    print("******************")


# with tf.name_scope("b"):
#     # 因为tf.get_variable不受tf.name_scope影响,所以这里将试图获取名称为“a”的变量。
#     # 然而这个变量己经被声明了,于是这里会报重复声明的错误:
#     # ValueError: Variable bar already exists, disallowed. Did you mean
#     # to set reuse=True in VarScope? Originally defined at: ...
#     tf.get_variable("b",[1])

运行程序,输出:



2.2 通过对命名空间的管理,可视化计算图

通过对命名空间管理,改进第一节中向量相加的演示代码,使得可视化得到的效果图更加清晰。如下:

# -*- coding: utf-8 -*-

import tensorflow as tf

#将输入定义放入各自的命名空间中,从而使得TensorBoard可以根据命名空间来整理可视化效果图上的节点。
with tf.name_scope("namespace_1"):
    inputl = tf.constant ([1.0, 2.0, 3.0], name="input1")

with tf.name_scope("namespace_2"):
    input2 = tf.Variable(tf.random_uniform([3]), name="input2")

output = tf.add_n([inputl, input2], name="add")

#将当前的TensorFlow计算图写入日志
writer = tf.summary.FileWriter(r"C:\Users\Administrator\Desktop\code\tensorflow\TensorBoard_test\tensorboard_model",
                                tf.get_default_graph())
writer.close()





猜你喜欢

转载自blog.csdn.net/weixin_41695564/article/details/80246229