Skip to content

Commit

Permalink
add "ensure_variables_initialized" to Trainer
Browse files Browse the repository at this point in the history
  • Loading branch information
haowen-xu committed Sep 10, 2019
1 parent 97b9881 commit 63adaf0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions tfsnippet/trainer/base_trainer.py
Expand Up @@ -79,14 +79,17 @@ class BaseTrainer(object):
trainer.log_after_steps(1000) # call `loop.print_logs` every 1000 steps
"""

def __init__(self, loop):
def __init__(self, loop, ensure_variables_initialized=True):
"""
Initialize the internal states of :class:`BaseTrainer`.
Args:
loop (TrainLoop): The training loop object.
ensure_variables_initialized (bool): Whether or not to ensure
the variables are initialized in :meth:`run()`?
"""
self._loop = loop
self._ensure_variables_initialized = ensure_variables_initialized
self._events = EventSource([
EventKeys.BEFORE_EXECUTION,
EventKeys.AFTER_EXECUTION,
Expand Down Expand Up @@ -134,7 +137,8 @@ def run(self):

# initialize global training status
session = get_default_session_or_error()
ensure_variables_initialized()
if self._ensure_variables_initialized:
ensure_variables_initialized()
self.loop.print_training_summary()

for _ in self.loop.iter_epochs():
Expand Down
10 changes: 8 additions & 2 deletions tfsnippet/trainer/trainer.py
Expand Up @@ -65,7 +65,8 @@ class Trainer(BaseTrainer):
"""

def __init__(self, loop, train_op, inputs, data_flow, feed_dict=None,
metrics=None, summaries=None):
metrics=None, summaries=None,
ensure_variables_initialized=True):
"""
Args:
Expand All @@ -90,13 +91,18 @@ def __init__(self, loop, train_op, inputs, data_flow, feed_dict=None,
of summaries to be run and along with `train_op`, and later
to be added to ``loop.summary_writer``.
If ``loop.summary_writer`` is None, then no summary will be run.
ensure_variables_initialized (bool): Whether or not to ensure
the variables are initialized in :meth:`run()`?
"""
if loop.max_epoch is None and loop.max_step is None:
raise ValueError('At least one of `max_epoch`, `max_step` should '
'be configured for `loop`.')
if summaries is not None and is_tensor_object(summaries):
summaries = [summaries]
super(Trainer, self).__init__(loop=loop)
super(Trainer, self).__init__(
loop=loop,
ensure_variables_initialized=ensure_variables_initialized
)

# memorize the arguments
self._inputs = tuple(inputs or ())
Expand Down

0 comments on commit 63adaf0

Please sign in to comment.