# tf.compat.v1.train.SingularMonitoredSession

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L1057-L1160) |

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

    tf.compat.v1.train.SingularMonitoredSession(
        hooks=None,
        scaffold=None,
        master='',
        config=None,
        checkpoint_dir=None,
        stop_grace_period_secs=120,
        checkpoint_filename_with_path=None
    )

<br />

## Migrate to TF2

<br />

> [!CAUTION]
> **Caution:** This API was designed for TensorFlow v1. Continue reading for details on how to migrate from this API to a native TensorFlow v2 equivalent. See the [TensorFlow v1 to TensorFlow v2 migration guide](https://www.tensorflow.org/guide/migrate) for instructions on how to migrate the rest of your code.

This API is not compatible with eager execution and [`tf.function`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/function). To migrate
to TF2, rewrite the code to be compatible with eager execution. Check the
[migration
guide](https://www.tensorflow.org/guide/migrate#1_replace_v1sessionrun_calls)
on replacing `Session.run` calls. In Keras, session hooks can be replaced by
Callbacks e.g. [logging hook notebook](https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/logging_stop_hook.ipynb)
For more details please read [Better
performance with tf.function](https://www.tensorflow.org/guide/function).

<br />

<br />

## Description

Please note that this utility is not recommended for distributed settings.
For distributed settings, please use [`tf.compat.v1.train.MonitoredSession`](https://www.tensorflow.org/versions/r2.14/api_docs/python/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`](https://www.tensorflow.org/versions/r2.14/api_docs/python/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():
        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 the `SingularMonitoredSession` is used as a context.

<br />

<br />

<br />

| ## Args ||
|---|---|---|
| `hooks` | An iterable of `SessionRunHook' objects. </td> </tr><tr> <td>`scaffold`<a id="scaffold"></a> </td> <td> A`Scaffold`used 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`<a id="master"></a> </td> <td>`String`representation of the TensorFlow master to use. </td> </tr><tr> <td>`config`<a id="config"></a> </td> <td>`ConfigProto`proto used to configure the session. </td> </tr><tr> <td>`checkpoint_dir`<a id="checkpoint_dir"></a> </td> <td> A string. Optional path to a directory where to restore variables. </td> </tr><tr> <td>`stop_grace_period_secs`<a id="stop_grace_period_secs"></a> </td> <td> Number of seconds given to threads to stop after`close()`has been called. </td> </tr><tr> <td>`checkpoint_filename_with_path\` | A string. Optional path to a checkpoint file from which to restore variables. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `graph` | The graph that was launched in this session. |

<br />

## Child Classes

[`class StepContext`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/compat/v1/train/MonitoredSession/StepContext)

## Methods

### `close`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L880-L881)

    close()

### `raw_session`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L1158-L1160)

    raw_session()

Returns underlying `TensorFlow.Session` object.

### `run`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L764-L782)

    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.

<br />

<br />

<br />

| Args ||
|---|---|
| `fetches` | Same as `tf.Session.run()`. |
| `feed_dict` | Same as `tf.Session.run()`. |
| `options` | Same as `tf.Session.run()`. |
| `run_metadata` | Same as `tf.Session.run()`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Same as `tf.Session.run()`. ||

<br />

### `run_step_fn`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L784-L838)

    run_step_fn(
        step_fn
    )

Run ops using a step function.

<br />

<br />

<br />

| Args ||
|---|---|
| `step_fn` | A function or a method with a single argument of type `StepContext`. 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 from `run_step_fn`, unless a stop is requested. In that case, the next `should_stop` call will return True. Example usage: <br /> ```python with tf.Graph().as_default(): c = tf.compat.v1.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 the `step_fn` as they do with a `MonitoredSession.run` call. <br /> |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Returns the returned value of `step_fn`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `StopIteration` | if `step_fn` has called `request_stop()`. It may be caught by `with tf.MonitoredSession()` to close the session. |
| `ValueError` | if `step_fn` doesn't have a single argument called `step_context`. It may also optionally have `self` for cases when it belongs to an object. |

<br />

### `should_stop`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L877-L878)

    should_stop()

### `__enter__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L883-L884)

    __enter__()

### `__exit__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/training/monitored_session.py#L886-L891)

    __exit__(
        exception_type, exception_value, traceback
    )