[Tensorflow] Correct way to write summary when using multiple graphs

https://www.tensorflow.org/programmers_guide/summaries_and_tensorboard
This official sample illustrates how to write summary of tensorflow.
But when you're using more then two graphs, you should provide graph key to specify which tensor goes into which graph, like:

import tensorflow as tf
class Net:
def __init__(self, session, name, multiplier):
self.session = session
self.name = name
with tf.variable_scope(name):
self.x1 = tf.placeholder(tf.int32)
# Don't do this
# tf.summary.histogram('in', self.x1)
# Do this
# Add summary to collection
tf.summary.histogram('in', self.x1, collections=[self.name])
a1 = tf.constant(multiplier)
self.y1 = tf.multiply(self.x1, a1)
tf.summary.histogram('out', self.y1)
def __enter__(self):
self._prepare_log_dir()
# Don't do this
# self.merged_summery = tf.summary.merge_all()
# Do this
# Collect necessary summaries only by specifying collection key
self.merged_summery = tf.summary.merge_all(key=self.name)
self.train_writer = tf.summary.FileWriter(self.name, self.session.graph)
def __exit__(self, exception_type, exception_value, traceback):
self.train_writer.close()
def predict(self, x):
result, _ = sess.run([self.y1, self.merged_summery], feed_dict={self.x1: x})
return result
def _prepare_log_dir(self):
if tf.gfile.Exists(self.name):
tf.gfile.DeleteRecursively(self.name)
tf.gfile.MakeDirs(self.name)
with tf.Session() as sess:
net1 = Net(sess, 'graph-1', 2)
net2 = Net(sess, 'graph-2', 3)
with net1, net2:
tf.global_variables_initializer().run()
print(net1.predict(3))
print(net2.predict(3))

댓글

이 블로그의 인기 게시물

Unity에서 모델의 폴리곤의 반대편이 랜더링 되지 않는 문제 해결 방법

Docker 컨테이너 네트워크 설정시 ifconfig로 docker0를 찾을 수 없는 문제에 대해

Use FFMPEG in XCode(for MacOS)