Class SingularMonitoredSession
Defined in tensorflow/python/training/monitored_session.py
.
Session-like object that handles initialization, restoring, and hooks.
Please note that this utility is not recommended for distributed settings.
For distributed settings, please use tf.train.MonitoredSession
. The
differences between MonitoredSession
and SingularMonitoredSession
are:
MonitoredSession
handlesAbortedError
andUnavailableError
for distributed settings, butSingularMonitoredSession
does not.MonitoredSession
can be created inchief
orworker
modes.SingularMonitoredSession
is always created aschief
.- You can access the raw
tf.Session
object used bySingularMonitoredSession
, whereas in MonitoredSession the raw session is private. This can be used:- To
run
without hooks. - To save and restore.
- To
- All other functionality is identical.
Example usage:
saver_hook = CheckpointSaverHook(...)
summary_hook = SummarySaverHook(...)
with SingularMonitoredSession(hooks=[saver_hook, summary_hook]) as sess:
while not sess.should_stop():
sess.run(train_op)
Initialization: At creation time the hooked session does following things in given order:
- calls
hook.begin()
for each given hook - finalizes the graph via
scaffold.finalize()
- create session
- initializes the model via initialization ops provided by
Scaffold
- restores variables if a checkpoint exists
- launches queue runners
Run: When run()
is called, the hooked session does following things:
- calls
hook.before_run()
- calls TensorFlow
session.run()
with merged fetches and feed_dict - calls
hook.after_run()
- returns result of
session.run()
asked by user
Exit: At the close()
, the hooked session does following things in order:
- calls
hook.end()
- closes the queue runners and the session
- suppresses
OutOfRange
error which indicates that all inputs have been processed if theSingularMonitoredSession
is used as a context.
__init__
__init__(
hooks=None,
scaffold=None,
master='',
config=None,
checkpoint_dir=None,
stop_grace_period_secs=120,
checkpoint_filename_with_path=None
)
Creates a SingularMonitoredSession.
Args:
hooks
: An iterable of `SessionRunHook' objects.scaffold
: AScaffold
used for gathering or building supportive ops. If not specified a default one is created. It's used to finalize the graph.master
:String
representation of the TensorFlow master to use.config
:ConfigProto
proto used to configure the session.checkpoint_dir
: A string. Optional path to a directory where to restore variables.stop_grace_period_secs
: Number of seconds given to threads to stop afterclose()
has been called.checkpoint_filename_with_path
: A string. Optional path to a checkpoint file from which to restore variables.
Child Classes
Properties
graph
The graph that was launched in this session.
Methods
tf.train.SingularMonitoredSession.__enter__
__enter__()
tf.train.SingularMonitoredSession.__exit__
__exit__(
exception_type,
exception_value,
traceback
)
tf.train.SingularMonitoredSession.close
close()
tf.train.SingularMonitoredSession.raw_session
raw_session()
Returns underlying TensorFlow.Session
object.
tf.train.SingularMonitoredSession.run
run(
fetches,
feed_dict=None,
options=None,
run_metadata=None
)
Run ops in the monitored session.
This method is completely compatible with the tf.Session.run()
method.
Args:
fetches
: Same astf.Session.run()
.feed_dict
: Same astf.Session.run()
.options
: Same astf.Session.run()
.run_metadata
: Same astf.Session.run()
.
Returns:
Same as tf.Session.run()
.
tf.train.SingularMonitoredSession.run_step_fn
run_step_fn(step_fn)
Run ops using a step function.
Args:
step_fn
: A function or a method with a single argument of typeStepContext
. The function may use methods of the argument to perform computations with access to a raw session.The returned value of the
step_fn
will be returned fromrun_step_fn
, unless a stop is requested. In that case, the nextshould_stop
call will return True.Example usage:
with tf.Graph().as_default(): c = tf.placeholder(dtypes.float32) v = tf.add(c, 4.0) w = tf.add(c, 0.5) def step_fn(step_context): a = step_context.session.run(fetches=v, feed_dict={c: 0.5}) if a <= 4.5: step_context.request_stop() return step_context.run_with_hooks(fetches=w, feed_dict={c: 0.1}) with tf.MonitoredSession() as session: while not session.should_stop(): a = session.run_step_fn(step_fn)
Hooks interact with the
run_with_hooks()
call inside thestep_fn
as they do with aMonitoredSession.run
call.
Returns:
Returns the returned value of step_fn
.
Raises:
StopIteration
: ifstep_fn
has calledrequest_stop()
. It may be caught bywith tf.MonitoredSession()
to close the session.ValueError
: ifstep_fn
doesn't have a single argument calledstep_context
. It may also optionally haveself
for cases when it belongs to an object.
tf.train.SingularMonitoredSession.should_stop
should_stop()