TensorFlow Course Day 1

TensorBoard (Visualization) # day 1

TensorFlow Course Day 1

导入

新建tensorBoard.py文件

from __future__ import print_function
import tensorflow as tf
import os 

设置日志文件的默认路径

# The default path for saving event files is the same folder of this python file.
tf.app.flags.DEFINE_string(
'log_dir',
os.path.dirname(os.path.abspath(__file__)) + '/logs',
'Directory where event logs are written to.')

# Store all elements in FLAG structure!
FLAGS = tf.app.flags.FLAGS

通过tf.app.flags.FLAGS 定义全局变量

这里定义了log_dir的位置,通过tf.app.flag.FLAGS这个封装器,可以通过 python tensorBoard --log_dir <new path>来更新log_dir的值

# The user is prompted to input an absolute path.
# os.path.expanduser is leveraged to transform '~' sign to the corresponding path indicator.
#       Example: '~/logs' equals to '/home/username/logs'
if not os.path.isabs(os.path.expanduser(FLAGS.log_dir)):
    raise ValueError('You must assign absolute path for --log_dir')  

提示用户输入绝对路径。

os.path.expanduser(FLAGS.log_dir)~转化为\user\username\home

Inauguration

# Defining some sentence!
welcome = tf.constant('Welcome to TensorFlow world!')

tf.定义的输出是Tensor

运行实验

# Run the session
with tf.Session() as sess:
    writer = tf.summary.FileWriter(os.path.expanduser(FLAGS.log_dir), sess.graph)
    print("output: ", sess.run(welcome))

# Closing the writer.
writer.close()
sess.close()
  1. tf.summary.FileWriter用来从event files中总结结果
  2. 任何操作都必须用 sess.run()来执行
  3. 最后使用writer.close()关闭

课程原文