# Retrieve neural network name inside registered hook

**URL:** <https://discuss.pytorch.org/t/retrieve-neural-network-name-inside-registered-hook/55398>\
**Category:** vision\
**Created:** [September 7, 2019, 1:31am UTC](https://discuss.pytorch.org/t/retrieve-neural-network-name-inside-registered-hook/55398 "2019-09-07T01:31:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vainaijr](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/e19b73/32.png) [@vainaijr](https://discuss.pytorch.org/u/vainaijr)\
**Post date:** [September 7, 2019, 1:31am UTC](https://discuss.pytorch.org/t/retrieve-neural-network-name-inside-registered-hook/55398/1 "2019-09-07T01:31:37Z")

</div>

how do I get the neural network name inside hook function, so that I see output of different neural network under a different name in tensorboard

```auto
writer = SummaryWriter()
def hook(module, input, output):
    # print(input[0].shape, output.shape)
    grid = torchvision.utils.make_grid(output[0][0])
    # print(grid.shape)
    
    # how to change conv_features to neural network name?
    # so it is model.modelone.encoder for a, model.modeltwo.encoder for b
    writer.add_image('conv_features', grid, 0) 

a = model.modelone.encoder.register_forward_hook(hook)
b = model.modeltwo.encoder.register_forward_hook(hook)

```

`module. __class__ , module. __class__. __name__ , module._get_name` do not give neural network name.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 7, 2019, 7:34am UTC](https://discuss.pytorch.org/t/retrieve-neural-network-name-inside-registered-hook/55398/2 "2019-09-07T07:34:50Z")

</div>

Have a look at [this example](https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6) where you can pass additional names to the `get_activation` function.

---

<div class="post-metadata">

**Author:** ![vainaijr](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/e19b73/32.png) [@vainaijr](https://discuss.pytorch.org/u/vainaijr)\
**Post date:** [September 7, 2019, 6:11pm UTC](https://discuss.pytorch.org/t/retrieve-neural-network-name-inside-registered-hook/55398/3 "2019-09-07T18:11:42Z")

</div>

I did something like this to get output of each encoder of each neural network

```auto
writer = SummaryWriter()
activations = {}
def get_activation(name):
  def hook(module, input, output):
      activations[name] = output.detach()
      grid = torchvision.utils.make_grid(output)
      writer.add_image(name, grid, 0)
  return hook

for name, layer in model.named_modules():
  if 'encoder' in name and 'encoder.' not in name:
    print(name, layer)
    layer.register_forward_hook(get_activation(name))

```

---

<div class="post-metadata">

**Author:** ![vainaijr](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/e19b73/32.png) [@vainaijr](https://discuss.pytorch.org/u/vainaijr)\
**Post date:** [September 8, 2019, 4:14pm UTC](https://discuss.pytorch.org/t/retrieve-neural-network-name-inside-registered-hook/55398/4 "2019-09-08T16:14:05Z")

</div>

@ptrblck, this way I am able to visualize what all patterns are learnt by base neural networks, but their output is passed in the form of 10 scores, which is then concatenated in higher level neural network, how do I visualize what are the higher level neural networks learning, currently I do something like this, I add image output from base networks, and plot them

```auto
def forward(self, x):
    img = self.modelone(x)[0] + self.modeltwo(x)[0]
    grid = torchvision.utils.make_grid(img)
    writer.add_image('img_combine', grid)
    img_2d = self.encoder(img)
    img = self.lin_two(self.lin_one(img_2d.view(img_2d.size(0), -1)))
    out = torch.cat((self.modelone(x)[1], self.modeltwo(x)[1], img), dim=-1)
    out = (self.linear((out)))
    return img_2d, out

```

where ModelOne forward looks like

```auto
def forward(self, x):
    x = self.encoder(x)
    return (x, self.lin_two(self.lin_one(x.view(x.size(0), -1))))

```

encoder is conv, batchnorm, relu
