Summary

class tf.Summary.FileWriter

init

创建一个writer

1
2
3
4
5
6
7
8
9
__init__(
logdir,
graph=None,
max_queue=10,
flush_secs=120,
graph_def=None,
filename_suffix=None,
session=None
)

add_summary

将summary string添加至事件文件中

1
2
3
4
add_summary(
summary,
global_step=None
)

flush

将事件文件写到磁盘

1
flush()

close

将事件文件写到磁盘并关闭FileWriter。

1
close()

visulization Function

scalar

1
2
3
4
5
6
tf.summary.scalar(
name,
tensor,
collections=None,
family=None
)

Args:

  • name: 显示的名字

  • family: 同一族

  • Return: 一个operation

image

1
2
3
4
5
6
7
tf.summary.image(
name,
tensor,
max_outputs=3,
collections=None,
family=None
)

Args:

  • name: 显示的名字

  • tensor: shape [batch_size, height, width, channels], channels可以是1,3,4,分别对应于Grayscale,RGB,RGBA。

histogram

1
2
3
4
5
6
7

tf.summary.histogram(
name,
values,
collections=None,
family=None
)
  • name: 显示的名称

  • values: 处理的数据,可以是任意形状

text

1
2
3
4
5
6

tf.summary.text(
name,
tensor,
collections=None
)
  • tensor: a string-type Tensor to summarize.

audio

合并可视化操作

目的: 使得1次forward propagation,获取多个summary

tf.summary.merge

1
2
3
4
5
6

tf.summary.merge(
inputs,
collections=None,
name=None
)
  • inputs: A list of string Tensor objects containing serialized Summary protocol buffers.

tf.summary.merge_all

1
2
3
4
5
tf.summary.merge_all(
key=tf.GraphKeys.SUMMARIES,
scope=None,
name=None
)

Merges all summaries collected in the default graph.

Args:

  • key: GraphKey used to collect the summaries. Defaults to GraphKeys.SUMMARIES.
  • scope: Optional scope used to filter the summary ops, using re.match

Return: 返回一个ops

Conclusion

可视化主要流程:

  • 预定义Summary objects

  • 合并Summary objects

  • 启动Session

  • 定义tf.summary.FileWriter

  • 获取summary string : sess.run(summary_ops, feed_dict={})

  • 添加至事件文件

  • 写入磁盘

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import tensorflow as tf

var = tf.Variable(tf.zeros([], name='var'))

scalar_op = tf.Summary.scalar('var', var)

merge_op = tf.Summary.merge_all()

init_op = tf.global_variables_initializer()

with tf.Session() as sess:

writer = tf.Summary.FileWriter('./logs')

sess.run(init_op)

summary_string = sess.run(merge_op)

writer.add_summary(summary_string, global_step=0)

writer.flush()