Hi,
I got a problem when I want to get the grad for input features with a pre-trained model.
The source code is
H_Features = output[0].clone().detach().requires_grad_(True)
V_Features = output[0].clone().detach().requires_grad_(True)
print("Features:")
print(H_Features.is_leaf)
print(V_Features.requires_grad)
H_model = RouteNet()
H_model.load_state_dict(torch.load("./net_H_epoch_39_batch_15_param.pkl"), strict=False)
V_model = RouteNet()
V_model.load_state_dict(torch.load("./net_V_epoch_39_batch_15_param.pkl"), strict=False)
target = torch.zeros((1, 1, placedb.num_bins_x, placedb.num_bins_y))
congestion_H = H_model(H_Features)
congestion_V = V_model(V_Features)
congestion_H.requires_grad = True
congestion_V.requires_grad = True
loss = torch.nn.MSELoss()
H_loss = loss(congestion_H, target)
V_loss = loss(congestion_V, target)
H_loss.requires_grad = True
V_loss.requires_grad = True
print("congestion_H:")
print(congestion_H.requires_grad)
print("H_loss is_leaf:")
print(H_loss.is_leaf)
print(V_loss.requires_grad)
print(H_loss)
print(V_loss)
H_Features.retain_grad()
V_Features.retain_grad()
H_loss.backward(torch.tensor(1.), retain_graph=True)
V_loss.backward(torch.tensor(1.), retain_graph=True)
print("H_Features.grad:")
print(H_Features.grad_fn)
print(V_Features.grad)
H_grad = H_Features.grad
V_grad = V_Features.grad
the result shows even I set all parameters, input features and loss ‘requires_grad=True’, after I use nn.MSELoss(), the loss’s requires_grad is False and the output of “H_loss.is_leaf” is True.
Then I run ‘H_loss.backward()’
Every parameters’ grad is None and the input features’ grad is also None.
I don’t know why it happens and how to solve it.