How to get the output of a specific input through all layers of the model?

I have got an autoencoder model, and I want to be able to get the output of each input of every batch in every layer of the module and store it to compare it with something else.
How can I do that
I know that I can use hooks to get the activation of the intermediate layers in my module, now I need to use it for every input!!

There’s another way, without hooks. You can directly adjust forward() method of your model’s class and return intermediate outputs with the actual output as a dict, for example.

As for hooks, take a look here: Debugging and Visualisation in PyTorch using Hooks

Thank you, but can you elaborate more on how to adjust the forward method in the model to get the output of each layer for every input.
I am new to deep learning and PyTorch

(this option ain’t pretty, it’s more like a hack). For example here’s a forward method of resnet class:

    def _forward_impl(self, x):
        #out = {}
        x = self.conv1(x)
        #out['conv1'] = x.detach()
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        #out['maxpool'] = x.detach()
        x = self.layer1(x)
        #out['layer1'] = x.detach()
        x = self.layer2(x)
        #out['layer2'] = x.detach()
        x = self.layer3(x)
        #out['layer3'] = x.detach()
        x = self.layer4(x)
        #out['layer4'] = x.detach()
        x = self.avgpool(x)
        #out['avgpool'] = x.detach()
        x = torch.flatten(x, 1)
        x = self.fc(x)
        return x, out #instead of return x

commented lines will create dict with copies of intermediate values, detached from graph

Thank you, this is really helpful.
I tried it and it worked.