Write summaries to TensorFlow event file.
| 225 | |
| 226 | |
| 227 | class TFEventWriter(MonitorBase): |
| 228 | """ |
| 229 | Write summaries to TensorFlow event file. |
| 230 | """ |
| 231 | def __init__(self, logdir=None, max_queue=10, flush_secs=120, split_files=False): |
| 232 | """ |
| 233 | Args: |
| 234 | logdir: ``logger.get_logger_dir()`` by default. |
| 235 | max_queue, flush_secs: Same as in :class:`tf.summary.FileWriter`. |
| 236 | split_files: if True, split events to multiple files rather than |
| 237 | append to a single file. Useful on certain filesystems where append is expensive. |
| 238 | """ |
| 239 | if logdir is None: |
| 240 | logdir = logger.get_logger_dir() |
| 241 | assert tf.gfile.IsDirectory(logdir), logdir |
| 242 | self._logdir = fs.normpath(logdir) |
| 243 | self._max_queue = max_queue |
| 244 | self._flush_secs = flush_secs |
| 245 | self._split_files = split_files |
| 246 | |
| 247 | def __new__(cls, logdir=None, max_queue=10, flush_secs=120, **kwargs): |
| 248 | if logdir is None: |
| 249 | logdir = logger.get_logger_dir() |
| 250 | |
| 251 | if logdir is not None: |
| 252 | return super(TFEventWriter, cls).__new__(cls) |
| 253 | else: |
| 254 | logger.warn("logger directory was not set. Ignore TFEventWriter.") |
| 255 | return NoOpMonitor("TFEventWriter") |
| 256 | |
| 257 | def _setup_graph(self): |
| 258 | self._writer = tf.summary.FileWriter( |
| 259 | self._logdir, max_queue=self._max_queue, flush_secs=self._flush_secs) |
| 260 | |
| 261 | def _write_graph(self): |
| 262 | self._writer.add_graph(self.graph) |
| 263 | |
| 264 | def _before_train(self): |
| 265 | # Writing the graph is expensive (takes ~2min) when the graph is large. |
| 266 | # Therefore use a separate thread. It will then run in the |
| 267 | # background while TF is warming up in the first several iterations. |
| 268 | self._write_graph_thread = threading.Thread(target=self._write_graph) |
| 269 | self._write_graph_thread.daemon = True |
| 270 | self._write_graph_thread.start() |
| 271 | |
| 272 | @HIDE_DOC |
| 273 | def process_summary(self, summary): |
| 274 | self._writer.add_summary(summary, self.global_step) |
| 275 | |
| 276 | @HIDE_DOC |
| 277 | def process_event(self, evt): |
| 278 | self._writer.add_event(evt) |
| 279 | |
| 280 | def _trigger(self): # flush every epoch |
| 281 | self._writer.flush() |
| 282 | if self._split_files: |
| 283 | self._writer.close() |
| 284 | self._writer.reopen() # open new file |