AttributeError: ‘NoneType’ object has no attribute ‘register_forward_hook’
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 4:

def hook_feature(module, in_, out_):
    features.append(out_.cpu().data.numpy())

model._modules.get("0.conv").register_forward_hook(hook_feature)


Here is my named_modules() output:

...
0.decoder1.dec1conv2
0.decoder1.dec1norm2
0.decoder1.dec1relu2
0.conv
1
1.outc1
1.outc2


What am I doing wrong and how do I fix it? Thanks!

Your modules are stored in an hierarchical way. To get to '0.conv', you need to

models._modules["0"]._modules.get("conv").register_forward_hook(hook_feature)

