Record more in Recorder

Hi Everyone,

I want to record the output activations at each layer in Recorder.

I have such a code block to register the forward hook:

outputs = {}

def save_output(name):
    def hook(module, input, output):
        outputs[name] = output
    return hook

for name, module in model.named_modules():
    if list(module.children()) == []:

And I could record the activations in pytorch ignite by:

def log_activations(engine):
    for key, value in outputs.items():

But, I don’t know how to implement it in fastai_v1 using callbacks, I have some questions:

  1. how to deal with the global variable outputs?
  2. where to put the code block that register the forward hooks?

Could you please help me? Thank you so much!

Callbacks are great for this - without messy globals and also handling removing the hooks automatically. I’ve just added some little classes to notebook 005 so you can simply do this:

This is HookCallback BTW:

class HookCallback(LearnerCallback):
    def on_train_begin(self, **kwargs):
        self.hooks = []
        for name,module in learn.model.named_modules():
            if list(module.children()) == []:
                func = self.hook(name,module)

    def on_train_end(self, **kwargs):
        if not self.hooks: return
        for hook in self.hooks: hook.remove()