I want to save the gradient of FPN. I tried as follows, but the gradient was not saved.

used register_hook
requires_grad of input was set to be True

class _FPN(nn.Module):
....
     self.gradients = []

    def save_gradient(self, grad):
        self.gradients.append(grad)
....
def forward(self, im_data, im_info, gt_boxes, num_boxes):
...
        c1 = self.RCNN_layer0(im_data)
        c2 = self.RCNN_layer1(c1)
        c3 = self.RCNN_layer2(c2)
        c3.register_hook(self.save_gradient)
....

im_data = Variable(im_data, requires_grad = True)

Thanks!