Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/summary/writer/event_file_writer_v2.py: 35%
40 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
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"""Writes events to disk in a logdir."""
17from tensorflow.python.framework import constant_op
18from tensorflow.python.framework import dtypes
19from tensorflow.python.framework import ops
20from tensorflow.python.ops import array_ops
21from tensorflow.python.ops import summary_ops_v2
22from tensorflow.python.platform import gfile
25class EventFileWriterV2(object):
26 """Writes `Event` protocol buffers to an event file via the graph.
28 The `EventFileWriterV2` class is backed by the summary file writer in the v2
29 summary API (currently in tf.contrib.summary), so it uses a shared summary
30 writer resource and graph ops to write events.
32 As with the original EventFileWriter, this class will asynchronously write
33 Event protocol buffers to the backing file. The Event file is encoded using
34 the tfrecord format, which is similar to RecordIO.
35 """
37 def __init__(self, session, logdir, max_queue=10, flush_secs=120,
38 filename_suffix=''):
39 """Creates an `EventFileWriterV2` and an event file to write to.
41 On construction, this calls `tf.contrib.summary.create_file_writer` within
42 the graph from `session.graph` to look up a shared summary writer resource
43 for `logdir` if one exists, and create one if not. Creating the summary
44 writer resource in turn creates a new event file in `logdir` to be filled
45 with `Event` protocol buffers passed to `add_event`. Graph ops to control
46 this writer resource are added to `session.graph` during this init call;
47 stateful methods on this class will call `session.run()` on these ops.
49 Note that because the underlying resource is shared, it is possible that
50 other parts of the code using the same session may interact independently
51 with the resource, e.g. by flushing or even closing it. It is the caller's
52 responsibility to avoid any undesirable sharing in this regard.
54 The remaining arguments to the constructor (`flush_secs`, `max_queue`, and
55 `filename_suffix`) control the construction of the shared writer resource
56 if one is created. If an existing resource is reused, these arguments have
57 no effect. See `tf.contrib.summary.create_file_writer` for details.
59 Args:
60 session: A `tf.compat.v1.Session`. Session that will hold shared writer
61 resource. The writer ops will be added to session.graph during this
62 init call.
63 logdir: A string. Directory where event file will be written.
64 max_queue: Integer. Size of the queue for pending events and summaries.
65 flush_secs: Number. How often, in seconds, to flush the
66 pending events and summaries to disk.
67 filename_suffix: A string. Every event file's name is suffixed with
68 `filename_suffix`.
69 """
70 self._session = session
71 self._logdir = logdir
72 self._closed = False
73 gfile.MakeDirs(self._logdir)
75 with self._session.graph.as_default():
76 with ops.name_scope('filewriter'):
77 file_writer = summary_ops_v2.create_file_writer(
78 logdir=self._logdir,
79 max_queue=max_queue,
80 flush_millis=flush_secs * 1000,
81 filename_suffix=filename_suffix)
82 with summary_ops_v2.always_record_summaries(), file_writer.as_default():
83 self._event_placeholder = array_ops.placeholder_with_default(
84 constant_op.constant('unused', dtypes.string),
85 shape=[])
86 self._add_event_op = summary_ops_v2.import_event(
87 self._event_placeholder)
88 self._init_op = file_writer.init() # pylint: disable=assignment-from-no-return
89 self._flush_op = file_writer.flush() # pylint: disable=assignment-from-no-return
90 self._close_op = file_writer.close() # pylint: disable=assignment-from-no-return
91 self._session.run(self._init_op)
93 def get_logdir(self):
94 """Returns the directory where event file will be written."""
95 return self._logdir
97 def reopen(self):
98 """Reopens the EventFileWriter.
100 Can be called after `close()` to add more events in the same directory.
101 The events will go into a new events file.
103 Does nothing if the EventFileWriter was not closed.
104 """
105 if self._closed:
106 self._closed = False
107 self._session.run(self._init_op)
109 def add_event(self, event):
110 """Adds an event to the event file.
112 Args:
113 event: An `Event` protocol buffer.
114 """
115 if not self._closed:
116 event_pb = event.SerializeToString()
117 self._session.run(
118 self._add_event_op, feed_dict={self._event_placeholder: event_pb})
120 def flush(self):
121 """Flushes the event file to disk.
123 Call this method to make sure that all pending events have been written to
124 disk.
125 """
126 self._session.run(self._flush_op)
128 def close(self):
129 """Flushes the event file to disk and close the file.
131 Call this method when you do not need the summary writer anymore.
132 """
133 if not self._closed:
134 self.flush()
135 self._session.run(self._close_op)
136 self._closed = True