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 tensorflow.core.util import event_pb2 19from tensorflow.python.lib.io import tf_record 20from tensorflow.python.util.tf_export import tf_export 21 22 23class _SummaryIterator(object): 24 """Yields `Event` protocol buffers from a given path.""" 25 26 def __init__(self, path): 27 self._tf_record_iterator = tf_record.tf_record_iterator(path) 28 29 def __iter__(self): 30 return self 31 32 def __next__(self): 33 r = next(self._tf_record_iterator) 34 return event_pb2.Event.FromString(r) 35 36 next = __next__ 37 38 39@tf_export(v1=['train.summary_iterator']) 40def summary_iterator(path): 41 # pylint: disable=line-too-long 42 """Returns a iterator for reading `Event` protocol buffers from an event file. 43 44 You can use this function to read events written to an event file. It returns 45 a Python iterator that yields `Event` protocol buffers. 46 47 Example: Print the contents of an events file. 48 49 ```python 50 for e in tf.compat.v1.train.summary_iterator(path to events file): 51 print(e) 52 ``` 53 54 Example: Print selected summary values. 55 56 ```python 57 # This example supposes that the events file contains summaries with a 58 # summary value tag 'loss'. These could have been added by calling 59 # `add_summary()`, passing the output of a scalar summary op created with 60 # with: `tf.compat.v1.summary.scalar('loss', loss_tensor)`. 61 for e in tf.compat.v1.train.summary_iterator(path to events file): 62 for v in e.summary.value: 63 if v.tag == 'loss': 64 print(tf.make_ndarray(v.tensor)) 65 ``` 66 Example: Continuously check for new summary values. 67 68 ```python 69 summaries = tf.compat.v1.train.summary_iterator(path to events file) 70 while True: 71 for e in summaries: 72 for v in e.summary.value: 73 if v.tag == 'loss': 74 print(tf.make_ndarray(v.tensor)) 75 # Wait for a bit before checking the file for any new events 76 time.sleep(wait time) 77 ``` 78 79 See the protocol buffer definitions of 80 [Event](https://www.tensorflow.org/code/tensorflow/core/util/event.proto) 81 and 82 [Summary](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto) 83 for more information about their attributes. 84 85 Args: 86 path: The path to an event file created by a `SummaryWriter`. 87 88 Returns: 89 A iterator that yields `Event` protocol buffers 90 """ 91 return _SummaryIterator(path) 92