1# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15 16"""Provides a method for reading events from an event file via an iterator.""" 17 18from __future__ import absolute_import 19from __future__ import division 20from __future__ import print_function 21 22from tensorflow.core.util import event_pb2 23from tensorflow.python.lib.io import tf_record 24from tensorflow.python.util.tf_export import tf_export 25 26 27class _SummaryIterator(object): 28 """Yields `Event` protocol buffers from a given path.""" 29 30 def __init__(self, path): 31 self._tf_record_iterator = tf_record.tf_record_iterator(path) 32 33 def __iter__(self): 34 return self 35 36 def __next__(self): 37 r = next(self._tf_record_iterator) 38 return event_pb2.Event.FromString(r) 39 40 next = __next__ 41 42 43@tf_export(v1=['train.summary_iterator']) 44def summary_iterator(path): 45 # pylint: disable=line-too-long 46 """Returns a iterator for reading `Event` protocol buffers from an event file. 47 48 You can use this function to read events written to an event file. It returns 49 a Python iterator that yields `Event` protocol buffers. 50 51 Example: Print the contents of an events file. 52 53 ```python 54 for e in tf.compat.v1.train.summary_iterator(path to events file): 55 print(e) 56 ``` 57 58 Example: Print selected summary values. 59 60 ```python 61 # This example supposes that the events file contains summaries with a 62 # summary value tag 'loss'. These could have been added by calling 63 # `add_summary()`, passing the output of a scalar summary op created with 64 # with: `tf.compat.v1.summary.scalar('loss', loss_tensor)`. 65 for e in tf.compat.v1.train.summary_iterator(path to events file): 66 for v in e.summary.value: 67 if v.tag == 'loss': 68 print(v.simple_value) 69 ``` 70 Example: Continuously check for new summary values. 71 72 ```python 73 summaries = tf.compat.v1.train.summary_iterator(path to events file) 74 while True: 75 for e in summaries: 76 for v in e.summary.value: 77 if v.tag == 'loss': 78 print(v.simple_value) 79 # Wait for a bit before checking the file for any new events 80 time.sleep(wait time) 81 ``` 82 83 See the protocol buffer definitions of 84 [Event](https://www.tensorflow.org/code/tensorflow/core/util/event.proto) 85 and 86 [Summary](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto) 87 for more information about their attributes. 88 89 Args: 90 path: The path to an event file created by a `SummaryWriter`. 91 92 Returns: 93 A iterator that yields `Event` protocol buffers 94 """ 95 return _SummaryIterator(path) 96