Can I re-register a pytorch hook after removing it?


For one of my studies I want to dynamically register and unregister a pytorch forward hook. For example, I only need to access the activation outputs at certain steps. I have created a handle for the forward hook:

handle_1 = model.fc_1.register_forward_hook(act_hook())

Now if I remove the handle using:


Is there anyway I can re-attach it somehow? Thanks in advance!

Yes, I think calling register_forward_hook another time would register the hook again and would return the corresponding handle.

1 Like

Hi @ptrblck, It actually worked! Thanks so much :smiley: