pytorch_pfn_extras.reporting.Reporter#

class pytorch_pfn_extras.reporting.Reporter#

Bases: object

Object to which observed values are reported.

Reporter is used to collect values that users want to watch. The reporter object holds a mapping from value names to the actually observed values. We call this mapping observations.

When a value is passed to the reporter, an object called observer can be optionally attached. In this case, the name of the observer is added as the prefix of the value name. The observer name should be registered beforehand.

See the following example:

>>> from pytorch_pfn_extras.reporting import Reporter, report, report_scope
>>>
>>> reporter = Reporter()
>>> observer = object()  # it can be an arbitrary (reference) object
>>> reporter.add_observer('my_observer', observer)
>>> observation = {}
>>> with reporter.scope(observation):
...     reporter.report({'x': 1}, observer)
...
>>> observation
{'my_observer/x': 1}

There are also a global API to add values:

>>> reporter = Reporter()
>>> observation = {}
>>> with reporter:
...     with report_scope(observation):
...         report({'x': 1})
...
>>> observation
{'x': 1}

The most important application of Reporter is to report observed values from each link or chain in the training and validation procedures. and some extensions prepare their own Reporter object with the hierarchy of the target module registered as observers. We can use report() function inside any nn.Module to report the observed values (e.g., training loss, accuracy, activation statistics, etc.).

observation#

Dictionary of observed values.

Methods

__init__()

add_observer(name, observer)

Registers an observer of values.

add_observers(prefix, observers)

Registers multiple observers at once.

report(values[, observer])

Reports observed values.

scope(observation)

Creates a scope to report observed values to observation.

__init__()#
Return type:

None

add_observer(name, observer)#

Registers an observer of values.

Observer defines a scope of names for observed values. Values observed with the observer are registered with names prefixed by the observer name.

Parameters:
  • name (str) – Name of the observer.

  • observer (Module) – The observer object. Note that the reporter distinguishes the observers by their object ids (i.e., id(owner)), rather than the object equality.

Return type:

None

add_observers(prefix, observers)#

Registers multiple observers at once.

This is a convenient method to register multiple objects at once.

Parameters:
  • prefix (str) – Prefix of each name of observers.

  • observers (Sequence[Tuple[str, Module]]) – Iterator of name and observer pairs.

Return type:

None

report(values, observer=None)#

Reports observed values.

The values are written with the key, prefixed by the name of the observer object if given.

Note

If a value is of type Tensor, the variable is copied without preserving the computational graph and the new variable object purged from the graph is stored to the observer.

Parameters:
  • values (dict) – Dictionary of observed values.

  • observer (Optional[Module]) – Observer object. Its object ID is used to retrieve the observer name, which is used as the prefix of the registration name of the observed value.

Return type:

None

scope(observation)#

Creates a scope to report observed values to observation.

This is a context manager to be passed to with statements. In this scope, the observation dictionary is changed to the given one.

It also makes this reporter object current.

Parameters:

observation (dict) – Observation dictionary. All observations reported inside of the with statement are written to this dictionary.

Return type:

Generator[None, None, None]