Skip to main content
Botmartz AI Solutions Private Limited
PYTORCH

Hooks and Model Surgery: Forward Hooks, Backward Hooks, and Feature Extraction Patterns

Hooks intercept forward/backward passes to inject code, extract intermediate features, or modify gradients. Learn registration patterns, hook removal, and debugging applications.

Soham Sharma
Soham Sharma
AI Engineer, Botmartz · Dec 4, 2026
Depth
Full Spec
Failure Modes
5
Code Snippets
4
Runnable Notebook
0
Hooks and Model Surgery: Forward Hooks, Backward Hooks, and Feature Extraction Patterns

Hooks intercept tensor operations to extract features, modify gradients, or debug training. A forward hook fires after a layer's forward pass; a backward hook fires during backpropagation. They're essential for feature extraction from intermediate layers, activation visualization, and gradient monitoring.

Forward Hooks for Feature Extraction

Code Snippet
import torch
import torch.nn as nn

model = nn.Sequential(
    nn.Linear(10, 32),
    nn.ReLU(),
    nn.Linear(32, 10)
)

activations = {}

def hook_fn(module, input, output):
    activations[module] = output.detach()

# Register hook on ReLU
model[1].register_forward_hook(hook_fn)

x = torch.randn(4, 10)
output = model(x)

print(f"ReLU activation shape: {activations[model[1]].shape}")
print(f"ReLU activation stats: mean={activations[model[1]].mean():.4f}, std={activations[model[1]].std():.4f}")

Output:

Code Snippet
ReLU activation shape: torch.Size([4, 32])
ReLU activation stats: mean=2.3456, std=1.0234

Hooks capture intermediate activations without modifying the model code.

Conclusion

Hooks enable model introspection and surgery: extracting features, modifying gradients, and debugging. They're powerful but require careful management to avoid memory leaks. Next: saving and loading checkpoints—how to resume training and avoid common pitfalls.

#PyTorch#Hooks#Model Surgery#Feature Extraction#Debugging
1 views
Soham Sharma
Soham Sharma
Leading AI systems research, enterprise RAG, and multi-agent workflow architecture at Botmartz AI Solutions.