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 27@tf_export(v1=['train.summary_iterator']) 28def summary_iterator(path): 29 # pylint: disable=line-too-long 30 """An iterator for reading `Event` protocol buffers from an event file. 31 32 You can use this function to read events written to an event file. It returns 33 a Python iterator that yields `Event` protocol buffers. 34 35 Example: Print the contents of an events file. 36 37 ```python 38 for e in tf.train.summary_iterator(path to events file): 39 print(e) 40 ``` 41 42 Example: Print selected summary values. 43 44 ```python 45 # This example supposes that the events file contains summaries with a 46 # summary value tag 'loss'. These could have been added by calling 47 # `add_summary()`, passing the output of a scalar summary op created with 48 # with: `tf.summary.scalar('loss', loss_tensor)`. 49 for e in tf.train.summary_iterator(path to events file): 50 for v in e.summary.value: 51 if v.tag == 'loss': 52 print(v.simple_value) 53 ``` 54 55 See the protocol buffer definitions of 56 [Event](https://www.tensorflow.org/code/tensorflow/core/util/event.proto) 57 and 58 [Summary](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto) 59 for more information about their attributes. 60 61 Args: 62 path: The path to an event file created by a `SummaryWriter`. 63 64 Yields: 65 `Event` protocol buffers. 66 """ 67 # pylint: enable=line-too-long 68 for r in tf_record.tf_record_iterator(path): 69 yield event_pb2.Event.FromString(r) 70