• 文件 >
  • torch.nn >
  • torch.nn.modules.module.register_module_forward_hook
捷徑

torch.nn.modules.module.register_module_forward_hook

torch.nn.modules.module.register_module_forward_hook(hook, *, with_kwargs=False, always_call=False)[原始碼][原始碼]

為所有模組註冊全域前向鉤子。

警告

這會將全域狀態新增至 nn.module 模組,且僅適用於除錯/效能分析目的。

每次 forward() 計算出輸出後,都會呼叫這個 hook。它應該具有以下簽名:

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

輸入僅包含傳遞給模組的位置參數。關鍵字參數不會傳遞給 hook,只會傳遞給 forward。您可以選擇性地修改模組的輸出,方法是傳回一個新值,該值將替換來自 forward() 函數的輸出。

參數
  • hook (Callable) – 要註冊的使用者定義 hook。

  • always_call (bool) – 如果 True,則無論在呼叫模組時是否引發異常,都會執行 hook。預設值:False

返回值

一個 handle,可用於通過呼叫 handle.remove() 來移除已新增的 hook

返回類型

torch.utils.hooks.RemovableHandle

此 hook 將在以 register_forward_hook 註冊的特定模組 hook 之前執行。

文件

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources