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

Hi,

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:

handle_1.remove()

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.

2 Likes

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