CCL
March 12, 2020, 7:19am
1
I’m trying to register a forward hook function to the last conv layer of my network. I first printed out the names of the modules via:
for name, _ in model.named_modules():
print(name)
Which gave me "0.conv"
as the module name. However, when I tried to do the following, the above error was triggered by line 2:
def hook_feature(module, in_, out_):
features.append(out_.cpu().data.numpy())
model._modules.get("0.conv").register_forward_hook(hook_feature)
What am I doing wrong and how do I fix it? Thanks!
This seems like there is no module named 0.conv
as the exception says 'NoneType' object has no attribute ‘register_forward_hook’
. Please check if the module name passed to get method is correct.
Can you post the output of print statement showing the network’s module list?
CCL
March 13, 2020, 2:35pm
3
Apologies for the late reply! Here’s the output:
...
0.decoder1.dec1norm1
0.decoder1.dec1relu1
0.decoder1.dec1conv2
0.decoder1.dec1norm2
0.decoder1.dec1relu2
0.conv
1
1.outc1
1.outc2
CCL
March 15, 2020, 9:25am
4
Solved with help from Shai (Stack Overflow): model._modules["0"]._modules.get("conv").register_forward_hook(hook_feature)