torchfunc.hooks.registrators¶
This module allows you for easier hook registration (e.g. based on type
or index
within network).
Example:
# Example forward pre hook
def example_forward_pre(module, inputs):
return inputs + 1
# MNIST classifier
model = torch.nn.Sequential(
torch.nn.Linear(784, 100),
torch.nn.ReLU(),
torch.nn.Linear(100, 50),
torch.nn.ReLU(),
torch.nn.Linear(50, 10),
)
registrator = torchfunc.hooks.registrators.ForwardPre()
# Register forwardPreHook for all torch.nn.Linear submodules
registrator.modules(model, example_forward_pre, types=(torch.nn.Linear))
You could specify indices instead of types (for example all inputs to torch.nn.Linear
will be registered),
and iterate over children
instead of modules
.
-
class
torchfunc.hooks.registrators.
Backward
(hook: Callable)[source]¶ Register backward hook based on module’s type or indices.
-
handles
¶ Handles for registered hooks, each corresponds to specific submodule. Can be used to unregister certain hooks (though discouraged).
- Type
List[torch.utils.hooks.RemovableHandle]
-
children
(network, types: Tuple[Any] = None, indices: List[int] = None)¶ Register
subrecorders
using types and/or indices viachildren
hook.This function will use
children
method oftorch.nn.Module
to iterate over available submodules. If you wish to iterate recursively, usemodules
.Important:
If
types
andindices
are left with their default values, all modules will havesubrecorders
registered.- Parameters
module (torch.nn.Module) – Module (usually neural network) for which inputs will be collected.
types (Tuple[typing.Any], optional) – Module types for which data will be recorded. E.g.
(torch.nn.Conv2d, torch.nn.Linear)
will registersubrecorders
on every module being instance of eitherConv2d
orLinear
. Default:None
indices (Iterable[int], optional) – Indices of modules whose inputs will be registered. Default:
None
- Returns
- Return type
self
-
modules
(module: torch.nn.modules.module.Module, types: Tuple[Any] = None, indices: List[int] = None)¶ Register
hook
using types and/or indices viamodules
hook.This function will use
modules
method oftorch.nn.Module
to iterate over available submodules. If you wish to iterate non-recursively, usechildren
.Important:
If
types
andindices
are left with their default values, all modules will havesubrecorders
registered.- Parameters
module (torch.nn.Module) – Module (usually neural network) for which inputs will be collected.
types (Tuple[typing.Any], optional) – Module types for which data will be recorded. E.g.
(torch.nn.Conv2d, torch.nn.Linear)
will registersubrecorders
on every module being instance of eitherConv2d
orLinear
. Default:None
indices (Iterable[int], optional) – Indices of modules whose inputs will be registered. Default:
None
- Returns
- Return type
self
-
remove
(index) → None¶ Remove hook specified by
index
.- Parameters
index (int) – Index of subhook (usually registered for layer)
-
-
class
torchfunc.hooks.registrators.
Forward
(hook: Callable)[source]¶ Register forward hook based on module’s type or indices.
-
handles
¶ Handles for registered hooks, each corresponds to specific submodule. Can be used to unregister certain hooks (though discouraged).
- Type
List[torch.utils.hooks.RemovableHandle]
-
children
(network, types: Tuple[Any] = None, indices: List[int] = None)¶ Register
subrecorders
using types and/or indices viachildren
hook.This function will use
children
method oftorch.nn.Module
to iterate over available submodules. If you wish to iterate recursively, usemodules
.Important:
If
types
andindices
are left with their default values, all modules will havesubrecorders
registered.- Parameters
module (torch.nn.Module) – Module (usually neural network) for which inputs will be collected.
types (Tuple[typing.Any], optional) – Module types for which data will be recorded. E.g.
(torch.nn.Conv2d, torch.nn.Linear)
will registersubrecorders
on every module being instance of eitherConv2d
orLinear
. Default:None
indices (Iterable[int], optional) – Indices of modules whose inputs will be registered. Default:
None
- Returns
- Return type
self
-
modules
(module: torch.nn.modules.module.Module, types: Tuple[Any] = None, indices: List[int] = None)¶ Register
hook
using types and/or indices viamodules
hook.This function will use
modules
method oftorch.nn.Module
to iterate over available submodules. If you wish to iterate non-recursively, usechildren
.Important:
If
types
andindices
are left with their default values, all modules will havesubrecorders
registered.- Parameters
module (torch.nn.Module) – Module (usually neural network) for which inputs will be collected.
types (Tuple[typing.Any], optional) – Module types for which data will be recorded. E.g.
(torch.nn.Conv2d, torch.nn.Linear)
will registersubrecorders
on every module being instance of eitherConv2d
orLinear
. Default:None
indices (Iterable[int], optional) – Indices of modules whose inputs will be registered. Default:
None
- Returns
- Return type
self
-
remove
(index) → None¶ Remove hook specified by
index
.- Parameters
index (int) – Index of subhook (usually registered for layer)
-
-
class
torchfunc.hooks.registrators.
ForwardPre
(hook: Callable)[source]¶ Register forward pre hook based on module’s type or indices.
-
handles
¶ Handles for registered hooks, each corresponds to specific submodule. Can be used to unregister certain hooks (though discouraged).
- Type
List[torch.utils.hooks.RemovableHandle]
-
children
(network, types: Tuple[Any] = None, indices: List[int] = None)¶ Register
subrecorders
using types and/or indices viachildren
hook.This function will use
children
method oftorch.nn.Module
to iterate over available submodules. If you wish to iterate recursively, usemodules
.Important:
If
types
andindices
are left with their default values, all modules will havesubrecorders
registered.- Parameters
module (torch.nn.Module) – Module (usually neural network) for which inputs will be collected.
types (Tuple[typing.Any], optional) – Module types for which data will be recorded. E.g.
(torch.nn.Conv2d, torch.nn.Linear)
will registersubrecorders
on every module being instance of eitherConv2d
orLinear
. Default:None
indices (Iterable[int], optional) – Indices of modules whose inputs will be registered. Default:
None
- Returns
- Return type
self
-
modules
(module: torch.nn.modules.module.Module, types: Tuple[Any] = None, indices: List[int] = None)¶ Register
hook
using types and/or indices viamodules
hook.This function will use
modules
method oftorch.nn.Module
to iterate over available submodules. If you wish to iterate non-recursively, usechildren
.Important:
If
types
andindices
are left with their default values, all modules will havesubrecorders
registered.- Parameters
module (torch.nn.Module) – Module (usually neural network) for which inputs will be collected.
types (Tuple[typing.Any], optional) – Module types for which data will be recorded. E.g.
(torch.nn.Conv2d, torch.nn.Linear)
will registersubrecorders
on every module being instance of eitherConv2d
orLinear
. Default:None
indices (Iterable[int], optional) – Indices of modules whose inputs will be registered. Default:
None
- Returns
- Return type
self
-
remove
(index) → None¶ Remove hook specified by
index
.- Parameters
index (int) – Index of subhook (usually registered for layer)
-