# orbit.StandardEvaluator

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/models/blob/v2.20.0/orbit/standard_runner.py#L238-L450) |

Implements the standard functionality of AbstractEvaluator APIs.

Inherits From: [`AbstractEvaluator`](https://www.tensorflow.org/api_docs/python/orbit/AbstractEvaluator)

    orbit.StandardEvaluator(
        eval_dataset,
        options: Optional[https://www.tensorflow.org/api_docs/python/orbit/StandardEvaluatorOptions] = None
    )

This class structures evaluation roughly as follows:

    state = eval_begin()
    for _ in range(num_steps):
      step_outputs = eval_step(eval_iterator)
      state = eval_reduce(state, step_outputs)
    return eval_end(state)

Calls to `eval_begin` and `eval_end` are always done in eager
mode, while `eval_step` may be compiled with [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) as determined by
the `options` passed to `__init__`. `eval_reduce` is in eager mode if
`use_tf_while_loop=False` in `StandardEvaluatorOptions`, but in graph mode if
`use_tf_while_loop=True`.

This class does not support completely evaluating multiple different datasets
(i.e., where every example of each dataset should be processed, as opposed to
running for a fixed number of evaluation steps). A custom `AbstractEvaluator`
is recommended in this case.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `eval_dataset` | A [`tf.nest`](https://www.tensorflow.org/api_docs/python/tf/nest)-compatible structure of [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) or `DistributedDataset`. On TPUs, if users want to exaust the dataset without specifying number of eval steps, it is recommended to set `drop_remainder=False` when batching the dataset, so the infrastructure can handle the last partial batch properly. |
| `options` | An [`orbit.StandardEvaluatorOptions`](https://www.tensorflow.org/api_docs/python/orbit/StandardEvaluatorOptions) instance. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `eval_dataset` | The current evaluation dataset. |
| `name` | Returns the name of this module as passed or determined in the ctor. <br /> > [!NOTE] > **Note:** This is not the same as the `self.name_scope.name` which includes parent module names. |
| `name_scope` | Returns a [`tf.name_scope`](https://www.tensorflow.org/api_docs/python/tf/name_scope) instance for this class. |
| `non_trainable_variables` | Sequence of non-trainable variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `submodules` | Sequence of all sub-modules. Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on). a = tf.Module() b = tf.Module() c = tf.Module() a.b = b b.c = c list(a.submodules) == [b, c] True list(b.submodules) == [c] True list(c.submodules) == [] True <br /> |
| `trainable_variables` | Sequence of trainable variables owned by this module and its submodules. <br /> > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `variables` | Sequence of variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |

## Methods

### `create_eval_loop_fn`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/orbit/standard_runner.py#L284-L307)

    create_eval_loop_fn(
        has_state: bool
    )

Creates an eval loop from the current step function and options.

<br />

<br />

<br />

| Args ||
|---|---|
| `has_state` | If the step function has state, state will be kept in the loop. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The eval loop function, i.e. wrapper of multiple eval steps. ||

<br />

### `eval_begin`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/orbit/standard_runner.py#L354-L365)

    eval_begin() -> Any

Called once at the beginning of the evaluation.

This method is always called in eager mode, and is a good place to reset
metrics that accumulate values over the course of evaluation.

Note that this method is called before dataset iterator creation.

<br />

<br />

<br />

| Returns ||
|---|---|
| A value to pass as the `state` argument to `eval_reduce`. ||

<br />

### `eval_end`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/orbit/standard_runner.py#L392-L410)

    eval_end(
        *args
    ) -> Optional[runner.Output]

Called at the end of the evaluation.

Called once at the end of evaluation.

This method is always called in eager mode, and is a good place to get
metric results. The value returned from this function will be returned as-is
from the `evaluate` method implementation provided by `StandardEvaluator`.

<br />

<br />

<br />

| Args ||
|---|---|
| `*args` | The outputs from `eval_reduce` for the last eval step, if they are non-`None` (if they are `None`, nothing is passed). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The function may return a dictionary of `Tensors`, which will be written to logs and as TensorBoard summaries. It can also be a nested dictionary, yielding a hierarchy of summary directories. ||

<br />

### `eval_reduce`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/orbit/standard_runner.py#L412-L432)

    eval_reduce(
        state: Optional[Any] = None, step_outputs: Optional[runner.Output] = None
    ) -> Any

A function to perform per-step reduction on the evaluation outputs.

This is useful for passing state throughout evaluation, especially in cases
where maintaining or accumulating state is hard to accomplish using
`tf.metrics.Metric` or other [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable)-based approaches. For instance,
it can be used to easily accumulate all per-example losses from the full
evaluation for subsequent processing in `eval_end()`.

<br />

<br />

<br />

| Args ||
|---|---|
| `state` | A state being maintained throughout the evaluation. |
| `step_outputs` | Outputs from the current evaluation step. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An output which is passed as the `state` argument to this function for the next step. After evaluation is finished, the output from last step will be passed to `eval_end`. ||

<br />

### `eval_step`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/orbit/standard_runner.py#L367-L390)

    @abc.abstractmethod
    eval_step(
        iterator
    ) -> Any

Implements one step of evaluation.

What a "step" consists of is up to the implementer. When using distribution
strategies, the call to this method takes place in the "cross-replica
context" for generality, to allow e.g. multiple iterator dequeues and calls
to `strategy.run`.

Note that if `use_tf_function=True`, all the code inside `eval_step` should
be compatible with [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) tracing (and in particular, any state
modifications involving `self` should be avoided). In some cases, non-
[`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) compatible code can be moved to `eval_loop_begin`,
`eval_reduce`, or `eval_loop_end`, which always execute eagerly.

<br />

<br />

<br />

| Args ||
|---|---|
| `iterator` | A [`tf.nest`](https://www.tensorflow.org/api_docs/python/tf/nest)-compatible structure of [`tf.data.Iterator`](https://www.tensorflow.org/api_docs/python/tf/data/Iterator) or `DistributedIterator`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An output which is passed as `step_outputs` argument into `eval_reduce` function. ||

<br />

### `evaluate`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/orbit/standard_runner.py#L309-L352)

    evaluate(
        num_steps: tf.Tensor
    ) -> Optional[runner.Output]

Implements `num_steps` steps of evaluation.

<br />

<br />

<br />

| Args ||
|---|---|
| `num_steps` | The number of evaluation steps to run. When this is -1, evaluation proceeds until a call to `eval_step` raises a `StopIteration` or [`tf.errors.OutOfRangeError`](https://www.tensorflow.org/api_docs/python/tf/errors/OutOfRangeError). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The output of `self.eval_end()`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If `options.use_tf_while_loop` is `True` and `num_steps` is unspecified. |

<br />

### `with_name_scope`

    @classmethod
    with_name_scope(
        method
    )

Decorator to automatically enter the module name scope.

    class MyModule(tf.Module):
      @tf.Module.with_name_scope
      def __call__(self, x):
        if not hasattr(self, 'w'):
          self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))
        return tf.matmul(x, self.w)

Using the above module would produce [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable)s and [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s whose
names included the module name:

    mod = MyModule()
    mod(tf.ones([1, 2]))
    <tf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)>
    mod.w
    <tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,
    numpy=..., dtype=float32)>

<br />

<br />

<br />

| Args ||
|---|---|
| `method` | The method to wrap. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The original method wrapped such that it enters the module's name scope. ||

<br />