torch.optim.Optimizer.register_load_state_dict_pre_hook
torch.optim.Optimizer.register_load_state_dict_pre_hook
torch.optim.Optimizer 注册加载状态字典前钩子
-
Optimizer.register_load_state_dict_pre_hook(hook, prepend=False)[source]
```python Optimizer.register_load_state_dict_pre_hook(hook, prepend=False)[source] ``` ```python Optimizer 注册加载状态字典预处理钩子(hook, prepend=False)[源代码] ``` Register a load_state_dict pre-hook which will be called before
load_state_dict()
is called. It should have the following signature:
注册一个在调用load_state_dict()
之前将被调用的 load_state_dict 预钩子。它应该具有以下签名:hook(optimizer, state_dict) -> state_dict or None
The
optimizer
argument is the optimizer instance being used and thestate_dict
argument is a shallow copy of thestate_dict
the user passed in toload_state_dict
. The hook may modify the state_dict inplace or optionally return a new one. If a state_dict is returned, it will be used to be loaded into the optimizer.
```plaintextoptimizer
参数是正在使用的优化器实例,state_dict
参数是用户传递给load_state_dict
的state_dict
的浅拷贝。钩子可以就地修改状态字典,或者可选地返回一个新的状态字典。如果返回了状态字典,它将被用于加载到优化器中。 ```The hook will be called with argument
self
andstate_dict
before callingload_state_dict
onself
. The registered hook can be used to perform pre-processing before theload_state_dict
call is made.
钩子将在调用self
上的load_state_dict
之前,使用参数self
和state_dict
进行调用。已注册的钩子可以在执行load_state_dict
调用之前进行预处理。- Parameters 参数
hook (Callable) – The user defined hook to be registered.
钩子(Callable)- 用户定义的待注册钩子。prepend (bool) – If True, the provided pre
hook
will be fired before all the already registered pre-hooks onload_state_dict
. Otherwise, the providedhook
will be fired after all the already registered pre-hooks. (default: False)
``` prepend(布尔值)- 如果为 True,提供的预操作hook
将在所有已注册的预钩子load_state_dict
之前执行。否则,提供的hook
将在所有已注册的预钩子之后执行。(默认:False) ```
- Returns 返回
a handle that can be used to remove the added hook by calling
handle.remove()
一个可以通过调用handle.remove()
来移除已添加钩子的句柄- Return type 返回类型
torch.utils.hooks.RemoveableHandle