评价此页

torch.nn.modules.module.register_module_forward_pre_hook#

torch.nn.modules.module.register_module_forward_pre_hook(hook)[source]#

Register a forward pre-hook common to all modules.

警告

此函数会向 nn.module 模块添加全局状态,仅用于调试/剖析目的。

The hook will be called every time before forward() is invoked. It should have the following signature

hook(module, input) -> None or modified input

The 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 input. User can either return a tuple or a single modified value in the hook. We will wrap the value into a tuple if a single value is returned(unless that value is already a tuple).

This hook has precedence over the specific module hooks registered with register_forward_pre_hook.

返回

一个句柄,可用于通过调用 handle.remove() 来移除添加的钩子

返回类型

torch.utils.hooks.RemovableHandle