Help protect the Great Barrier Reef with TensorFlow on Kaggle Join Challenge


Session-like object that handles initialization, restoring, and hooks.

Migrate to TF2

This API is not compatible with eager execution and tf.function. To migrate to TF2, rewrite the code to be compatible with eager execution. Check the migration guide on replacing calls. In Keras, session hooks can be replaced by Callbacks e.g. logging hook notebook For more details please read Better performance with tf.function.


Please note that this utility is not recommended for distributed settings. For distributed settings, please use tf.compat.v1.train.MonitoredSession. The differences between MonitoredSession and SingularMonitoredSession are:

  • MonitoredSession handles AbortedError and UnavailableError for distributed settings, but SingularMonitoredSession does not.
  • MonitoredSession can be created in chief or worker modes. SingularMonitoredSession is always created as chief.
  • You can access the raw tf.compat.v1.Session object used by SingularMonitoredSession, whereas in MonitoredSession the raw session is private. This can be used:
    • To run without hooks.
    • To save and restore.
  • 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():

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 with merged fetches and feed_dict
  • calls hook.after_run()
  • returns result of 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 the SingularMonitoredSession is used as a context.

hooks An iterable of SessionRunHook' objects. </td> </tr><tr> <td>scaffold</td> <td> AScaffoldused for gathering or building supportive ops. If not specified a default one is created. It's used to finalize the graph. </td> </tr><tr> <td>master</td> <td>Stringrepresentation of the TensorFlow master to use. </td> </tr><tr> <td>config</td> <td>ConfigProtoproto used to configure the session. </td> </tr><tr> <td>checkpoint_dir</td> <td> A string. Optional path to a directory where to restore variables. </td> </tr><tr> <td>stop_grace_period_secs</td> <td> Number of seconds given to threads to stop afterclose()has been called. </td> </tr><tr> <td>checkpoint_filename_with_path` A string. Optional path to a checkpoint file from which to restore variables.

graph The graph that was launched in this session.

Child Classes

class StepContext



View source


View source

Returns underlying TensorFlow.Session object.


View source

Run ops in the monitored session.

This method is completely compatible with the method.

fetches Same as