pytorch_pfn_extras.training.extensions.Evaluator

class pytorch_pfn_extras.training.extensions.Evaluator(self, iterator, target, eval_func=None, *, progress_bar=False)

An extension to evaluate models on a validation set.

This extension evaluates the current models by a given evaluation function. It creates a Reporter object to store values observed in the evaluation function on each iteration. The report for all iterations are aggregated to DictSummary. The collected mean values are further reported to the reporter object of the manager, where the name of each observation is prefixed by the evaluator name. See Reporter for details in naming rules of the reports.

Evaluator has a structure to customize similar to that of StandardUpdater. The main differences are:

  • There are no optimizers in an evaluator. Instead, it holds links to evaluate.

  • An evaluation loop function is used instead of an update function.

  • Preparation routine can be customized, which is called before each evaluation. It can be used, e.g., to initialize the state of stateful recurrent networks.

There are two ways to modify the evaluation behavior besides setting a custom evaluation function. One is by setting a custom evaluation loop via the eval_func argument. The other is by inheriting this class and overriding the evaluate() method. In latter case, users have to create and handle a reporter object manually. Users also have to copy the iterators before using them, in order to reuse them at the next time of evaluation. In both cases, the functions are called in testing mode

This extension is called at the end of each epoch by default.

Parameters
  • iterator (Union[torch.utils.data.dataloader.DataLoader[Any], Dict[str, torch.utils.data.dataloader.DataLoader[Any]]]) – Dataset iterator for the validation dataset. It can also be a dictionary of iterators. If this is just an iterator, the iterator is registered by the name 'main'.

  • target (Union[torch.nn.modules.module.Module, Dict[str, torch.nn.modules.module.Module]]) – torch.nn.Module object or a dictionary of links to evaluate. If this is just a layer object, the link is registered by the name 'main'.

  • eval_func (Optional[Callable[[...], Any]]) – Evaluation function called at each iteration. The target link to evaluate as a callable is used by default.

  • progress_bar – Boolean flag to show a progress bar while training, which is similar to ProgressBar. (default: False)

  • metrics – List of callables that are called every batch to calculate metrics such as accuracy, roc_auc or others The signature of the callable is: def metric_fn(batch, output, last_iteration) (default: [])

  • eval_hook (Optional[Callable[[Evaluator], None]]) –

  • kwargs (Any) –

Return type

None

Warning

The argument progress_bar is experimental. The interface can change in the future.

eval_hook

Function to prepare for each evaluation process.

eval_func

Evaluation function called at each iteration.

__init__(iterator, target, eval_hook=None, eval_func=None, **kwargs)
Parameters
  • iterator (Union[torch.utils.data.dataloader.DataLoader[Any], Dict[str, torch.utils.data.dataloader.DataLoader[Any]]]) –

  • target (Union[torch.nn.modules.module.Module, Dict[str, torch.nn.modules.module.Module]]) –

  • eval_hook (Optional[Callable[[pytorch_pfn_extras.training.extensions.evaluator.Evaluator], None]]) –

  • eval_func (Optional[Callable[[...], Any]]) –

  • kwargs (Any) –

Return type

None

Methods

__init__(iterator, target[, eval_hook, …])

add_metric(metric_fn)

Adds a custom metric to the evaluator.

eval_func(*args, **kwargs)

evaluate()

Evaluates the model and returns a result dictionary.

finalize()

Finalizes the evaluator object.

get_all_iterators()

Returns a dictionary of all iterators.

get_all_targets()

Returns a dictionary of all target links.

get_iterator(name)

Returns the iterator of the given name.

get_target(name)

Returns the target link of the given name.

initialize(manager)

Initializes up the manager state.

load_state_dict(to_load)

on_error(manager, exc, tb)

Handles the error raised during training before finalization.

state_dict()

Serializes the extension state.

Attributes

default_name

is_async

name

needs_model_state

priority

trigger