Tensorflow Summary

Submitted by Lizhe on Thu, 08/17/2017 - 17:27

直接上例子

import tensorflow as tf

data = tf.Variable(0, trainable=False)
increment_data = tf.assign_add(data, tf.constant(1))
tf.scalar_summary('increment_data', data)

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)

summary_writer = tf.train.SummaryWriter('./my_graph', sess.graph)

sum_ops = tf.merge_all_summaries()
for step in range(0, 10):
    sumed_ops = sess.run(sum_ops)
    summary_writer.add_summary(sumed_ops, global_step=step)
    sess.run(increment_data)

这个例子中首先我们创建了一个变量,初始值是0

data = tf.Variable(0, trainable=False)

然后定义一个对其+1的函数
increment_data = tf.assign_add(data, tf.constant(1))

给可视化传递数据,将这个data变量列入统计对象
tf.scalar_summary('increment_data', data)

创建session对象,并且初始化默认值

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)

写入图

summary_writer = tf.train.SummaryWriter('./my_graph', sess.graph)

创建一个用于收集当前图的所有信息的对象

sum_ops = tf.merge_all_summaries()

开始迭代调用
for step in range(0, 10):
    sumed_ops = sess.run(sum_ops)
    summary_writer.add_summary(sumed_ops, global_step=step)
    sess.run(increment_data)

460

 

461

 

import tensorflow as tf

global_step = tf.Variable(0, trainable=False)
increment_op = tf.assign_add(global_step, tf.constant(1))

lr = tf.train.exponential_decay(0.1, global_step, decay_steps=1, decay_rate=0.9, staircase=False)


tf.scalar_summary('learning_rate', lr)


sum_ops = tf.merge_all_summaries()


sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)


summary_writer = tf.train.SummaryWriter('./my_graph', sess.graph)


for step in range(0, 10):
    s_val = sess.run(sum_ops)
    summary_writer.add_summary(s_val, global_step=step)
    sess.run(increment_op)

 

458

459