chainer.Reporter

class chainer.Reporter[source]

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 chainer 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:

>>> observation = {}
>>> with report_scope(observation):
...     report({'x': 1}, observer)
...
>>> observation
{'my_observer:x': 1}

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

Variables:observation – Dictionary of observed values.

Methods

__enter__()[source]

Makes this reporter object current.

__exit__(exc_type, exc_value, traceback)[source]

Recovers the previous reporter object to the current.

add_observer(name, observer)[source]

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 – The observer object. Note that the reporter distinguishes the observers by their object ids (i.e., id(owner)), rather than the object equality.
add_observers(prefix, observers)[source]

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 – Iterator of name and observer pairs.
report(values, observer=None)[source]

Reports observed values.

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

Note

As of v2.0.0, if a value is of type Variable, the variable is copied without preserving the computational graph and the new variable object purged from the graph is stored to the observer. This behavior can be changed by setting chainer.config.keep_graph_on_report to True.

Parameters:
  • values (dict) – Dictionary of observed values.
  • observer – 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.
scope(observation)[source]

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.