tensorflow系列(5)tensorboard可视化

Posted by grt1stnull on 2017-08-11

tensorboard可视化使用

0x00.前言

看书的笔记,水一篇。

0x01.tensorboard

1.限定命名空间

with tf.name_scope('')

1
2
3
4
with tf.name_scope('input'):
# 设置输入
X = tf.placeholder(tf.float32, shape=[None, 6])
y = tf.placeholder(tf.float32, shape=[None, 2])

2.记录与汇总

tf.summary.scalar('',)

1
2
3
4
5
6
7
8
9
10
11
with tf.name_scope('cost'):
# 交叉熵 与 损失函数
# softmax 手写专用
#cross_entropy = -tf.reduce_sum(y * tf.log(y_ + 1e-10), reduction_indices=1)
# 长函数
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_, labels=y)
cost = tf.reduce_mean(cross_entropy)
tf.summary.scalar('loss', cost)

scalar

3.直方图数据记录

tf.summary.histogram('',)

1
2
3
4
5
6
7
8
9
with tf.name_scope('classifier'):
# 设置参数
w = tf.Variable(tf.random_normal([6, 2]), name='weights')
b = tf.Variable(tf.zeros([2]), name='bias')
y_ = tf.matmul(X, w) + b
tf.summary.histogram('weights', w)
tf.summary.histogram('bias', b)

histogram0

histogram

4.日志记录

tf.summary.FileWriter()

1
2
3
4
# 训练数据
train_writer = tf.summary.FileWriter('./logs/train', sess.graph)
# 测试数据
test_writer = tf.summary.FileWriter('./logs/test')

5.初始化

tf.summary.merge_all()

1
2
3
4
merged = tf.summary.merge_all()
# 之后,初始化变量
tf.global_variables_initializer().run()

6.记录

writer.add_summary(summary, i)

1
2
3
for i in range(FLAGS.max_steps):
summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False))
test_writer.add_summary(summary, i)

7.元信息定义

1
2
3
4
5
6
7
8
9
10
for i in range(max_steps):
if i % 100 == 99:
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
summary, _ = sess.run([merged, train_step],
feed_dict=feed_dict(True),
options=run_options,
run_metadata=run_metadata)
train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
train_writer.add_summary(summary, i)

8.命令行启动

1
2
# 在命令行中输入,路径指定为日志写的路径
tensorboard --logdir=/home/grt1st/logs/train

手头没有程序的可以运行tensorflow的官方示例,摸这里

我一个程序的整个图:
graph

0x03.参考