Executes training steps, recovers and checkpoints. Note that this class is particularly preliminary, experimental, and expected to change.
| 24 | |
| 25 | |
| 26 | class Monitor(object): |
| 27 | """Executes training steps, recovers and checkpoints. |
| 28 | |
| 29 | Note that this class is particularly preliminary, experimental, and |
| 30 | expected to change. |
| 31 | """ |
| 32 | # TODO(isaprykin): Support step functions that need multiple session calls. |
| 33 | # TODO(isaprykin): Support extra arguments to the step function. |
| 34 | # TODO(isaprykin): Support recovery, checkpointing and summaries. |
| 35 | |
| 36 | def __init__(self, step_callable, session=None): |
| 37 | """Initialize the Monitor with components for executing training steps. |
| 38 | |
| 39 | Args: |
| 40 | step_callable: a training `Step` that's capable of signaling when done. |
| 41 | session: a `Session` instance that's needed for graph mode. |
| 42 | |
| 43 | Raises: |
| 44 | ValueError: if `session` was provided for eager mode or not provided for |
| 45 | graph mode. |
| 46 | """ |
| 47 | if context.executing_eagerly(): |
| 48 | if session is not None: |
| 49 | raise ValueError("Should not provide a `session` in Eager mode.") |
| 50 | self._run_step = step_callable |
| 51 | else: |
| 52 | if session is None: |
| 53 | raise ValueError("Should provide a `session` in Graph mode.") |
| 54 | session.run(step_callable.initialize()) |
| 55 | self._run_step = session.make_callable(step_callable()) |
| 56 | session.run(variables.global_variables_initializer()) |
| 57 | |
| 58 | def run_steps(self, num_steps=None): |
| 59 | step = 0 |
| 60 | while num_steps is None or step < num_steps: |
| 61 | try: |
| 62 | self._run_step() |
| 63 | step += 1 |
| 64 | except errors.OutOfRangeError: |
| 65 | break |
no outgoing calls
no test coverage detected