On this page
torch.nn.modules.module.register_module_forward_hook
torch.nn.modules.module.register_module_forward_hook(hook, *, always_call=False)[source]-
Registers a global forward hook for all the modules
Warning
This adds global state to the
nn.modulemodule and it is only intended for debugging/profiling purposes.The hook will be called every time after
forward()has computed an output. It should have the following signature:hook(module, input, output) -> None or modified outputThe input contains only the positional arguments given to the module. Keyword arguments won’t be passed to the hooks and only to the
forward. The hook can modify the output. It can modify the input inplace but it will not have effect on forward since this is called afterforward()is called.- Parameters
-
- hook (Callable) – The user defined hook to be registered.
- always_call (bool) – If
Truethehookwill be run regardless of whether an exception is raised while calling the Module. Default:False
- Returns
-
a handle that can be used to remove the added hook by calling
handle.remove() - Return type
-
torch.utils.hooks.RemovableHandle
This hook will be executed before specific module hooks registered with
register_forward_hook.
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.nn.modules.module.register_module_forward_hook.html