I want to check the gradient in a fully-connected layer (which is a feature map) w.r.t every image in a batch. Specifically, I want to print the norm of gradient of every image at a specific layer. So I used register_hook in my code:
class Net(nn.Module):
    def __init__(self):
        ...
        
    def forward(self, data, target):
        feat = self.model.forward(data)
        loss = self.loss.forward(feat, target)
        feat.register_hook(self.grad_norm)
        loss.backward()

    def grad_norm(self):
        def hook(grad):
            print(torch.norm(grad))
        return hook

However, as I run this code, error show up:
…
loss.backward()
File “/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py”, line 156, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)
File “/usr/local/lib/python2.7/dist-packages/torch/autograd/init.py”, line 98, in backward
variables, grad_variables, retain_graph)
TypeError: ‘list’ object is not callable
Is there something wrong in my implementation? Thanks!