Skip to content

Commit

Permalink
Lazily create summary writer for TF2 logger. (#5631)
Browse files Browse the repository at this point in the history
  • Loading branch information
llan-ml authored and pcmoritz committed Sep 4, 2019
1 parent 4ab5704 commit 1823ea7
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions python/ray/tune/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ def tf2_compat_logger(config, logdir):

class TF2Logger(Logger):
def _init(self):
from tensorflow.python.eager import context
self._context = context
self._file_writer = tf.summary.create_file_writer(self.logdir)
self._file_writer = None

def on_result(self, result):
if self._file_writer is None:
from tensorflow.python.eager import context
self._context = context
self._file_writer = tf.summary.create_file_writer(self.logdir)
with tf.device("/CPU:0"), self._context.eager_mode():
with tf.summary.record_if(True), self._file_writer.as_default():
step = result.get(
Expand All @@ -181,10 +183,12 @@ def on_result(self, result):
self._file_writer.flush()

def flush(self):
self._file_writer.flush()
if self._file_writer is not None:
self._file_writer.flush()

def close(self):
self._file_writer.close()
if self._file_writer is not None:
self._file_writer.close()


def to_tf_values(result, path):
Expand Down

0 comments on commit 1823ea7

Please sign in to comment.