torch.optim.Optimizer.register_state_dict_pre_hook#
- Optimizer.register_state_dict_pre_hook(hook, prepend=False)[source]#
Register a state dict pre-hook which will be called before
state_dict()
is called.它应具有以下签名
hook(optimizer) -> None
The
optimizer
argument is the optimizer instance being used. The hook will be called with argumentself
before callingstate_dict
onself
. The registered hook can be used to perform pre-processing before thestate_dict
call is made.- 参数
hook (Callable) – 用户定义的待注册钩子。
prepend (bool) – If True, the provided pre
hook
will be fired before all the already registered pre-hooks onstate_dict
. Otherwise, the providedhook
will be fired after all the already registered pre-hooks. (default: False)
- 返回
一个句柄,可用于通过调用
handle.remove()
来移除添加的钩子- 返回类型
torch.utils.hooks.RemoveableHandle