DifferenceMonitor¶
- class DifferenceMonitor(reducer: Reducer, attr: str, module: Module = None, train_update: bool = True, eval_update: bool = True, prepend: bool = False, filter_: Callable[[Any, Any], bool] | None = None, map_: Callable[[Any, Any], tuple[Tensor, ...]] | None = None, op_: Callable[[Tensor, Tensor], Tensor] | None = None)[source]¶
Bases:
MonitorRecords the difference of an attribute in a Module before and after its forward call.
- Parameters:
reducer (Reducer) – underlying means for reducing samples over time and storing them.
attr (str) – attribute or nested attribute to target.
module (Module, optional) – module to register as the target for monitoring, can be modified after construction. 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.prepend (bool, optional) – if this monitor should be called before other registered forward prehooks or posthooks. Defaults to
False.filter (Callable[[Any, Any], bool] | None, optional) – test if the input should be passed to the reducer, ignores
Nonevalues whenNone. Defaults toNone.map (Callable[[Any, Any], tuple[torch.Tensor, ...]] | None, optional) – modifies the input before being passed to the reducer, post-forward value minus pre-forward value wrapped in a tuple if
None. Defaults toNone.op (Callable[[torch.Tensor, torch.Tensor], torch.Tensor] | None, optional) – operation to calculate the difference between post-forward and pre-forward, only used when
map_isNone, subtraction whenNone. Defaults toNone.
Note
The nested attribute should be specified with dot notation. For instance, if the observed module has an attribute
awhich in turn has an attributebthat should be monitored, thenattrshould be ‘a.b’`. Even with nested attributes, the monitor’s hook will be tied to the module with which it is registered.Note
The left-hand argument of
filter_,map_, andop_is the attribute after theforward()call ofmoduleis run, and the right-hand argument is before it is run.By default,
filter_will only reject an input if both the pre and post states areNone. By default,map_will useop_to compare the pre-forward value from the post-forward value. If either isNone(but not both),map_will assume theNonevalue was composed of all-zeros.- classmethod partialconstructor(reducer: Reducer, train_update: bool = True, eval_update: bool = True, prepend: bool = False, filter_: Callable[[Any, Any], bool] | None = None, map_: Callable[[Any, Any], tuple[Tensor, ...]] | None = None, op_: Callable[[Tensor, Tensor], Tensor] | None = None) MonitorConstructor[source]¶
Returns a function with a common signature for monitor construction.
- Parameters:
reducer (Reducer) – underlying means for reducing samples over time and storing them.
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.prepend (bool, optional) – if this monitor should be called before other registered forward prehooks or posthooks. Defaults to
False.filter (Callable[[Any, Any], bool] | None, optional) – test if the input should be passed to the reducer, ignores
Nonevalues whenNone. Defaults toNone.map (Callable[[Any, Any], tuple[torch.Tensor, ...]] | None, optional) – modifies the input before being passed to the reducer, post-forward value minus pre-forward value wrapped in a tuple if
None. Defaults toNone.op (Callable[[torch.Tensor, torch.Tensor], torch.Tensor] | None, optional) – operation to calculate the difference between post-forward and pre-forward, only used when
map_isNone, subtraction whenNone. Defaults toNone.
- Returns:
partial constructor for monitor.
- Return type: