Hooks in PyTorch

I was trying to debug a CRNN model and in one of the modular class implementation of model inherited from nn.module, I saw forward and backward hooks.

What are they used for, Practically ? Saw some documentation w.r.t gradients flow but couldn’t foresee its practical usecase.

Thanks!

You can use them to inspect the values are they are passed during forward, or inspect the gradients as they flow during backward to see if they are what you expect.

1 Like