Hello guyz.
I just want to get the middle output of my network and calculate the gradient.
So, I’ve found layer.register_forward_hook function.
My code is below:

global glb_feature_teacher
glb_feature_teacher = torch.tensor(torch.zeros(train_batch, num_emb), requires_grad=True, device=torch.device(device))
def Get_features4teacher(self, input, output):
global glb_feature_teacher
glb_feature_teacher = output.data
return None
t_emb_layer = teacher_net.module.linear1

In the training phase,

output = net(inputs)
t_emb_layer.register_forward_hook(Get_features4teacher)
emb_teacher = torch.tensor(glb_feature_teacher, requires_grad=True, device=torch.device(device))
mse_loss = nn.MSELoss()
loss = mse_loss(emb_teacher, some_vector)
loss.backward()

The code is ran well, and checked whole the middle outputs are extracted well(the value of emb_teacher)
But the problem is, gradient is not calculated.
When i print out the gradient of extracted layer, it prints out None.
That means gradients are not calculated.
The code is below:

grad_of_params_teacher = {}
for name, parameter in teacher_net.named_parameters():
grad_of_params_teacher[name] = parameter.grad
print('teacher: ', grad_of_params_teacher[‘module.linear1.weight’])

output: teacher: [None]
Can you tell me that what is the problem?
I realllly don’t know how to solve this problem.
Help me plz