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

**URL:** <https://discuss.pytorch.org/t/how-to-get-the-output-of-a-specific-input-through-all-layers-of-the-model/134968>\
**Category:** Uncategorized\
**Created:** [October 24, 2021, 2:33am UTC](https://discuss.pytorch.org/t/how-to-get-the-output-of-a-specific-input-through-all-layers-of-the-model/134968 "2021-10-24T02:33:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Sarah](https://discuss.pytorch.org/u/Sarah)\
**Post date:** [October 24, 2021, 2:33am UTC](https://discuss.pytorch.org/t/how-to-get-the-output-of-a-specific-input-through-all-layers-of-the-model/134968/1 "2021-10-24T02:33:20Z")

</div>

Hello,  
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!!

---

<div class="post-metadata">

**Author:** ![my3bikaht](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@my3bikaht](https://discuss.pytorch.org/u/my3bikaht)\
**Post date:** [October 24, 2021, 5:58pm UTC](https://discuss.pytorch.org/t/how-to-get-the-output-of-a-specific-input-through-all-layers-of-the-model/134968/2 "2021-10-24T17:58:39Z")

</div>

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](https://blog.paperspace.com/pytorch-hooks-gradient-clipping-debugging/)

---

<div class="post-metadata">

**Author:** ![Sarah](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Sarah](https://discuss.pytorch.org/u/Sarah)\
**Post date:** [October 26, 2021, 12:14am UTC](https://discuss.pytorch.org/t/how-to-get-the-output-of-a-specific-input-through-all-layers-of-the-model/134968/3 "2021-10-26T00:14:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![my3bikaht](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@my3bikaht](https://discuss.pytorch.org/u/my3bikaht)\
**Post date:** [October 26, 2021, 5:30am UTC](https://discuss.pytorch.org/t/how-to-get-the-output-of-a-specific-input-through-all-layers-of-the-model/134968/4 "2021-10-26T05:30:43Z")

</div>

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

```auto
    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

---

<div class="post-metadata">

**Author:** ![Sarah](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Sarah](https://discuss.pytorch.org/u/Sarah)\
**Post date:** [October 30, 2021, 5:04pm UTC](https://discuss.pytorch.org/t/how-to-get-the-output-of-a-specific-input-through-all-layers-of-the-model/134968/5 "2021-10-30T17:04:54Z")

</div>

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