Reporter

Reporter

class chainer.Reporter[ソース]

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

変数:observation – Dictionary of observed values.
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.

パラメータ:
  • 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)[ソース]

Registers multiple observers at once.

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

パラメータ:
  • prefix (str) – Prefix of each name of observers.
  • observers – Iterator of name and observer pairs.
report(values, observer=None)[ソース]

Reports observed values.

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

パラメータ:
  • 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)[ソース]

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.

パラメータ:observation (dict) – Observation dictionary. All observations reported inside of the with statement are written to this dictionary.
chainer.get_current_reporter()[ソース]

Returns the current reporter object.

chainer.report(values, observer=None)[ソース]

Reports observed values with the current reporter object.

Any reporter object can be set current by the with statement. This function calls the Report.report() method of the current reporter. If no reporter object is current, this function does nothing.

Example

The most typical example is a use within links and chains. Suppose that a link is registered to the current reporter as an observer (for example, the target link of the optimizer is automatically registered to the reporter of the Trainer). We can report some values from the link as follows:

class MyRegressor(chainer.Chain):
    def __init__(self, predictor):
        super(MyRegressor, self).__init__(predictor=predictor)

    def __call__(self, x, y):
        # This chain just computes the mean absolute and squared
        # errors between the prediction and y.
        pred = self.predictor(x)
        abs_error = F.sum(F.abs(pred - y)) / len(x.data)
        loss = F.mean_squared_error(pred, y)

        # Report the mean absolute and squared errors.
        report({'abs_error': abs_error, 'squared_error': loss}, self)

        return loss

If the link is named 'main' in the hierarchy (which is the default name of the target link in the StandardUpdater), these reported values are named 'main/abs_error' and 'main/squared_error'. If these values are reported inside the Evaluator extension, 'validation/' is added at the head of the link name, thus the item names are changed to 'validation/main/abs_error' and 'validation/main/squared_error' ('validation' is the default name of the Evaluator extension).

パラメータ:
  • 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.
chainer.report_scope(observation)[ソース]

Returns a report scope with the current reporter.

This is equivalent to get_current_reporter().scope(observation), except that it does not make the reporter current redundantly.

Summary and DictSummary

class chainer.Summary[ソース]

Online summarization of a sequence of scalars.

Summary computes the statistics of given scalars online.

add(value)[ソース]

Adds a scalar value.

パラメータ:value – Scalar value to accumulate. It is either a NumPy scalar or a zero-dimensional array (on CPU or GPU).
compute_mean()[ソース]

Computes the mean.

make_statistics()[ソース]

Computes and returns the mean and standard deviation values.

戻り値:Mean and standard deviation values.
戻り値の型:tuple
class chainer.DictSummary[ソース]

Online summarization of a sequence of dictionaries.

DictSummary computes the statistics of a given set of scalars online. It only computes the statistics for scalar values and variables of scalar values in the dictionaries.

add(d)[ソース]

Adds a dictionary of scalars.

パラメータ:d (dict) – Dictionary of scalars to accumulate. Only elements of scalars, zero-dimensional arrays, and variables of zero-dimensional arrays are accumulated.
compute_mean()[ソース]

Creates a dictionary of mean values.

It returns a single dictionary that holds a mean value for each entry added to the summary.

戻り値:Dictionary of mean values.
戻り値の型:dict
make_statistics()[ソース]

Creates a dictionary of statistics.

It returns a single dictionary that holds mean and standard deviation values for every entry added to the summary. For an entry of name 'key', these values are added to the dictionary by names 'key' and 'key.std', respectively.

戻り値:Dictionary of statistics of all entries.
戻り値の型:dict