TensorFlow Course Day 1
TensorBoard (Visualization) # 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()
tf.summary.FileWriter
用来从event files
中总结结果- 任何操作都必须用
sess.run()
来执行- 最后使用
writer.close()
关闭