1.计算图可视化
1 import tensorflow as tf 2 3 path = "D:\Path" 4 5 # 定义一个简单的计算图,实现向量加法的操作。 6 input1 = tf.constant([1.0, 2.0, 3.0], name="input1") 7 input2 = tf.Variable(tf.random_uniform([3]), name="input2") 8 output = tf.add_n([input1, input2], name="add") 9 10 # 将当前的TensorFlow计算图写入日志 11 writer = tf.summary.FileWriter(path,tf.get_default_graph()) 12 writer.close()
运行后会在指定目录创建日志文件。
通过命令行启动TensorBoard。
tensorboard --logdir=D:\Path
浏览器打开命令行返回的地址即可。
2.添加命名空间
命名空间可以将计算图的细节部分隐藏,保留关键部分。
1 import tensorflow as tf 2 3 path = "D:\Path" 4 5 # 将输入定义放入各自的命名空间中,从而使得TensorBoard可以根据命名空间来整理可视化效果图上的节点。 6 with tf.name_scope("namespace_1"): 7 inputl = tf.constant([1.0, 2.0, 3.0], name="input1") 8 9 with tf.name_scope("namespace_2"): 10 input2 = tf.Variable(tf.random_uniform([3]), name="input2") 11 12 output = tf.add_n([inputl, input2], name="add") 13 14 # 将当前的TensorFlow计算图写入日志 15 writer = tf.summary.FileWriter(path, tf.get_default_graph()) 16 writer.close()