Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily create summary writer for TF2 logger. #5631

Merged
merged 3 commits into from
Sep 4, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions python/ray/tune/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ 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)

def on_result(self, result):
if not hasattr(self, "_file_writer"):
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 +180,12 @@ def on_result(self, result):
self._file_writer.flush()

def flush(self):
self._file_writer.flush()
if hasattr(self, "_file_writer"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we not use hasattr and instead set a class variable _file_writer = None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks.

self._file_writer.flush()

def close(self):
self._file_writer.close()
if hasattr(self, "_file_writer"):
self._file_writer.close()


def to_tf_values(result, path):
Expand Down