Monitor

class Monitor(reducer: Reducer, module: Module | None = None, prehook: str | None = None, posthook: str | None = None, prehook_kwargs: dict[str, Any] | None = None, posthook_kwargs: dict[str, Any] | None = None, train_update: bool = True, eval_update: bool = True)[source]

Bases: Module, ContextualHook

Base class for recording input, output, or state of a Module.

Parameters:
  • reducer (Reducer) – underlying means for reducing samples over time and storing them.

  • module (Module | None, optional) – module to register as the target for monitoring, can be modified after construction. Defaults to None.

  • prehook (str | None, optional) – name of the prehook method, if any, to execute, no prehook when None. Defaults to None.

  • posthook (str | None, optional) – name of the posthook method, if any, to execute, no posthook when None. Defaults to None.

  • prehook_kwargs (dict[str, Any] | None, optional) – keyword arguments passed to register_forward_pre_hook(). Defaults to None.

  • posthook_kwargs (dict[str, Any] | None, optional) – keyword arguments passed to register_forward_hook(). Defaults to None.

  • train_update (bool, optional) – if this monitor should be called when the module being monitored is in train mode. Defaults to True.

  • eval_update (bool, optional) – if this monitor should be called when the module being monitored is in eval mode. Defaults to True.

Important

While Monitor can be used directly, it must be subclassed to be used in cases where monitors are constructed automatically.

See also

See register_forward_pre_hook() and register_forward_hook() for keyword arguments that can be passed with prehook_kwargs and posthook_kwargs respectively.

clear(**kwargs) None[source]

Reinitializes the reducer’s state.

dump(*args, **kwargs) Tensor | None[source]

Returns the reducer’s state over all observations.

property latest: Tensor

Return’s the reducer’s current state.

If peek() has multiple options, this should be considered as the default. Unless overridden, peek() is called without arguments.

Returns:

reducer’s current state.

Return type:

torch.Tensor

classmethod partialconstructor(*args, **kwargs) MonitorConstructor[source]

Returns a function with a common signature for monitor construction.

Raises:

NotImplementedErrorpartialconstructor must be implemented by the subclass.

Returns:

partial constructor for monitor.

Return type:

MonitorConstructor

peek(*args, **kwargs) Tensor | None[source]

Returns the reducer’s current state.

property reducer: Reducer

Underlying reducer used by the monitor.

Returns:

underlying reducer.

Return type:

Reducer

register(module: Module | None = None) None[source]

Registers the monitor as a forward hook/prehook.

Parameters:

module (Module | None, optional) – module with which to register, last registered if None. Defaults to None.

Raises:

RuntimeError – weak reference to the last referenced module is no longer valid or did not exist.

view(*args, **kwargs) Tensor | None[source]

Returns the reducer’s state at a given time.