Thank you @tumble-weed.
Is the usage of layer.register_forward_hook correct?
I want to calculate loss value from hooked values with register_forward_hook function from middle of network.
And I’ve used your register_backward_hook function, but all the values are zero.
Implemented below:
global glb_grad_student
def Get_grad4student(self, ingrad, outgrad):
   global glb_grad_student
   glb_grad_student = outgrad
   return None
glb_grad_student = torch.tensor(torch.zeros(train_batch, num_emb), requires_grad=True, device=torch.device(device))

in the train function,
        loss.backward()
        s_emb_layer.register_backward_hook(Get_grad4student)#
        print(glb_grad_student)

and the output is
tensor([[0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        ...,
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.]], device='cuda:0', requires_grad=True)

What’s wrong in my code?