Thanks for your reply.

You can use  torch.autograd.grad()  to get the gradients wrt the input directly. (And avoid populating the .grad fields of all the weights in your model).

I checked the usage of torch.autograd.grad(), but I’m still confused.
Suppose there is a input vector input, and a loss L, I want to calculate the grad on the input rather than the whole network wrt L. Can I write input_grad = torch.autograd.grad(L, input)?
After getting the grad using
input_grad = torch.autograd.grad(L, input)
, I got
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
when I tried to update the network using
optimizer_d.zero_grad()
d_loss.backward()
optimizer_d.step().
Why is this happening and how am I suppose to fix this? Thank you.