torch.nn.ForwardHook
export type ForwardHook = (
module: Module,
input: Tensor[],
output: Tensor | Tensor[]
) => Tensor | Tensor[] | void;Hook called after a module's forward pass.
ForwardHook is executed after the module's forward() method completes. It receives the module, its inputs, and the computed outputs. Can modify or validate the output before it propagates further.
Signature:
(module: Module, input: Tensor[], output: Tensor | Tensor[]) => Tensor | Tensor[] | voidParameters:
module: The module whose forward just completedinput: Original input tensors (may be modified from pre-hooks)output: The computed output from forward (single tensor or array)
Return Value:
- Return modified output to change downstream computation
- Return void/undefined to use original output unchanged
- Useful for output normalization, validation, or debugging
Use Cases:
- Log outputs for inspection/debugging
- Monitor activations during training
- Modify outputs (e.g., clip values, apply constraints)
- Implement custom regularization on outputs
- Validate output properties (shape, range, NaN/Inf)
- Extract intermediate representations
Examples
const hook: ForwardHook = (module, input, output) => {
// Inspect output activation statistics
if (Array.isArray(output)) {
output.forEach(t => console.log('Output min/max:', t.min().item(), t.max().item()));
} else {
console.log('Output shape:', output.shape);
}
// Modify output: clip to [-1, 1]
return torch.clamp(output, -1, 1);
};
layer.register_forward_hook(hook);