View source on GitHub |
Implements the standard functionality of AbstractEvaluator APIs.
Inherits From: AbstractEvaluator
orbit.StandardEvaluator(
eval_dataset,
options: Optional[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
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.
Args | |
---|---|
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.
|
Attributes | |
---|---|
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).
|
trainable_variables
|
Sequence of trainable variables owned by this module and its submodules. |
variables
|
Sequence of variables owned by this module and its submodules. |
Methods
create_eval_loop_fn
create_eval_loop_fn(
has_state: bool
)
Creates an eval loop from the current step function and options.
Args | |
---|---|
has_state
|
If the step function has state, state will be kept in the loop. |
Returns | |
---|---|
The eval loop function, i.e. wrapper of multiple eval steps. |
eval_begin
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.
Returns | |
---|---|
A value to pass as the state argument to eval_reduce .
|
eval_end
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
.
Args | |
---|---|
*args
|
The outputs from eval_reduce for the last eval step, if they are
non-None (if they are None , nothing is passed).
|
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.
|
eval_reduce
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
-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()
.
Args | |
---|---|
state
|
A state being maintained throughout the evaluation. |
step_outputs
|
Outputs from the current evaluation step. |
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 .
|
eval_step
@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
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.
Args | |
---|---|
iterator
|
A tf.nest -compatible structure of tf.data.Iterator or
DistributedIterator .
|
Returns | |
---|---|
An output which is passed as step_outputs argument into eval_reduce
function.
|
evaluate
evaluate(
num_steps: tf.Tensor
) -> Optional[runner.Output]
Implements num_steps
steps of evaluation.
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 .
|
Returns | |
---|---|
The output of self.eval_end() .
|
Raises | |
---|---|
ValueError
|
If options.use_tf_while_loop is True and num_steps is
unspecified.
|
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
s and 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)>
Args | |
---|---|
method
|
The method to wrap. |
Returns | |
---|---|
The original method wrapped such that it enters the module's name scope. |