chainer.training.Extension¶

class chainer.training.Extension[source]

Base class of trainer extensions.

Extension of Trainer is a callable object that takes the trainer object as the argument. It also provides some default configurations as its attributes, e.g. the default trigger and the default priority. This class provides a set of typical default values for these attributes.

There are three ways to define users’ own extensions: inheriting this class, decorating closures by make_extension(), or using any callable including lambda functions as extensions. Decorator can slightly reduce the overhead and is much easier to use, while this class provides more flexibility (for example, it can have methods to configure the behavior). Using a lambda function allows one-line coding for simple purposes, but users have to specify the configurations as arguments to Trainer.extend(). For a callable not inheriting this class, the default configurations of this class are used unless the user explicitly specifies them in Trainer.extend() method.

Variables
• trigger – Default value of trigger for this extension. It is set to (1, 'iteration') by default.

• priority – Default priority of the extension. It is set to PRIORITY_READER by default.

• name – Name of the extension. It is set to None by default. This value will be overwritten when registering an extension to a trainer. See chainer.training.Trainer.extend() for details.

Methods

__call__(trainer)[source]

Invokes the extension.

Implementations should override this operator. This method is called at iterations which the corresponding trigger accepts.

Parameters

trainer (Trainer) – Trainer object that calls this operator.

finalize()[source]

Finalizes the extension.

This method is called at the end of the training loop.

initialize(trainer)[source]

Initializes up the trainer state.

This method is called before entering the training loop. An extension that modifies the state of Trainer can override this method to initialize it.

When the trainer has been restored from a snapshot, this method has to recover an appropriate part of the state of the trainer.

For example, ExponentialShift extension changes the optimizer’s hyperparameter at each invocation. Note that the hyperparameter is not saved to the snapshot; it is the responsibility of the extension to recover the hyperparameter. The ExponentialShift extension recovers it in its initialize method if it has been loaded from a snapshot, or just setting the initial value otherwise.

Parameters

trainer (Trainer) – Trainer object that runs the training loop.

on_error(trainer, exc, tb)[source]

Handles the error raised during training before finalization.

This method is called when an exception is thrown during the training loop, before finalize. An extension that needs different error handling from finalize, can override this method to handle errors.

Parameters
• trainer (Trainer) – Trainer object that runs the training loop.

• exc (Exception) – arbitrary exception thrown during update loop.

• tb (traceback) – traceback object of the exception

serialize(serializer)[source]

Serializes the extension state.

It is called when a trainer that owns this extension is serialized. It serializes nothing by default.

__eq__()

Return self==value.

__ne__()

Return self!=value.

__lt__()

Return self<value.

__le__()

Return self<=value.

__gt__()

Return self>value.

__ge__()

Return self>=value.

Attributes

default_name

Default name of the extension.

It is the name of the class by default. Implementation can override this property, or provide a class attribute to hide it.

name = None
priority = 100
trigger = (1, 'iteration')