torch.nn.ParameterRegistrationHook
export type ParameterRegistrationHook = (
module: Module,
name: string,
param: Parameter | null
) => Parameter | null | void;Hook called when a parameter is registered in a module.
ParameterRegistrationHook is triggered when register_parameter() is called. It can inspect, modify, or prevent registration of trainable parameters.
Signature:
(module: Module, name: string, param: Parameter | null) => Parameter | null | voidParameters:
module: The module registering the parametername: Name/key for the parameter (e.g., 'weight', 'bias')param: The parameter being registered (or null to unregister)
Return Value:
- Return a modified parameter to change what gets registered
- Return null to prevent parameter registration
- Return void to register unchanged
Use Cases:
- Track parameter registration in model construction
- Implement parameter initialization hooks
- Validate parameter properties (shape, dtype)
- Implement parameter naming conventions
- Control which parameters are trainable
- Monitor model parameter growth
Examples
const hook: ParameterRegistrationHook = (module, name, param) => {
if (param) {
console.log(`Parameter '${name}' shape: ${param.shape}`);
}
return; // Register as-is
};
torch.nn.register_parameter_registration_hook(hook);