torch.optim.Optimizer.register_state_dict_post_hook — PyTorch 2.7 documentation (original) (raw)

Optimizer.register_state_dict_post_hook(hook, prepend=False)[source][source]

Register a state dict post-hook which will be called after state_dict() is called.

It should have the following signature:

hook(optimizer, state_dict) -> state_dict or None

The hook will be called with arguments self and state_dict after generating a state_dict on self. The hook may modify the state_dict inplace or optionally return a new one. The registered hook can be used to perform post-processing on the state_dict before it is returned.

Parameters

Returns

a handle that can be used to remove the added hook by callinghandle.remove()

Return type

torch.utils.hooks.RemoveableHandle