Created
March 14, 2022 08:49
-
-
Save danesherbs/f39a311aa9c3d14c90650a0f66d1ab2a to your computer and use it in GitHub Desktop.
A PyTorch hook that stores the state of a forward pass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StatefulHook: | |
def __init__(self): | |
self.module = None | |
self.input = None | |
self.output = None | |
def __call__(self, module, input, output): | |
self.module = module | |
self.input = input | |
self.output = output | |
net = ... # some neural net | |
hook = StatefulHook() | |
net.my_layer.register_forward_hook(hook) | |
print(hook.input) | |
print(hook.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment