EasyDeepLearn

What is a forward hook in PyTorch and when is it useful?

easy

Answer

  • A callback registered on a module that fires during the forward pass, receiving (module, input, output).
  • Useful for extracting intermediate features (e.g., pen-ultimate embeddings for downstream models), debugging shape / value issues, visualizing activations, running feature-map probes, or computing custom regularizers on hidden activations without modifying the model code.
  • Backward hooks exist too — for inspecting gradients per module.
Check yourself — multiple choice
  • Hooks are only for backward passes
  • A callback that fires during forward pass — extract intermediate features / debug / probe
  • Hooks replace loss functions
  • Hooks work only in TensorFlow

Forward hook = callback on module forward — great for extracting features and debugging.

#fundamentals

Practise Deep Learning

214 interview questions in this topic.

Related questions