Implements the standard functionality of AbstractEvaluator APIs.

Inherits From: AbstractEvaluator

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 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.

eval_dataset A tf.nest-compatible structure of 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 instance.

eval_dataset The current evaluation dataset.
name Returns the name of this module as passed or determined in the ctor.

name_scope Returns a tf.name_scope instance for this class.
non_trainable_variables Sequence of non-trainable variables owned by this module and its submodules.
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]
list(b.submodules) == [c]
list(c.submodules) == []

trainable_variables Sequence of trainable variables owned by this module and its submodules.

variables Sequence of variables owned by this module and its submodules.



View source

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

has_state If the step function has state, state will be kept in the loop.

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


View source

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.

An value to pass as the state argument to eval_reduce.


View source

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.

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

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.


View source

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-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().

state A state being mainted throughout the evaluation.
step_outputs Outputs from the current evaluation step.

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.


View source

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 tracing (and in particular, any state modifications involving self should be avoided). In some cases, non- tf.function compatible code can be moved to eval_loop_begin, eval_reduce, or eval_loop_end, which always execute eagerly.

iterator A tf.nest-compatible structure of tf.data.Iterator or DistributedIterator.

An output which is passed as step_outputs argument into eval_reduce function.


View source

Implements num_steps steps of evaluation.

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.

The output of self.eval_end().

ValueError If options.use_tf_while_loop is True and num_steps is unspecified.


Decorator to automatically enter the module name scope.

class MyModule(tf.Module):
  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.Variables and tf.Tensors whose names included the module name:

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

method The method to wrap.

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