Hi
The VGG19 has 16 Conv. layer. So during the forward pass I register a hook_register on the outputs (activation maps) of each conv. layer + the last max_poolin layer as well as record the activation maps in a list just after calling the register_hook. As a result, after forward pass operation I have activation maps for 17 layers. However, after calling backward on the model, only the gradients of the output of five last layer are recorded. To put it differently, the function that hook is registered on it is just called 5 times. Why it is not called for all the outputs of the layers which have a registered function? How can I know the hook_registered is exactly called for all the tensor that I want?
Thank you in advance.
This is the function which is called when I pass image through the model,

def save_gradient(self, grad):
print(‘calling gradients’)
print(grad.shape)
self.gradients.append(grad)

def __call__(self, x):
    outputs = []
    self.gradients = []
    for name, module in self.model._modules.items():
        if name not in self.target_layers:
            with torch.no_grad():
                x = module(x.to(self.device))
        else:
            x = module(x.to(self.device))
            x = x.requires_grad_(True)
            x.register_hook(self.save_gradient)
            outputs += [x]
    return outputs, x