Can I use "torch.autograd.grad" in "training_step" funtion? #10731
-
Hello! I hope to calculate the gradient of the loss on the model input every batch when training. The calculated gradients are then processed by some other functions and added into the loss function. The way I do this in pytorch is like this, with the model_input.requires_grad = True
model.zero_grad()
model.eval()
grads = torch.autograd.grad(loss, model_input, grad_outputs=None, only_inputs=True, retain_graph=False)[0]
model.train() Can I add these codes directly into the My concern is:
Thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 14 replies
-
Yes, I believe. I don't think it'll work with
Yes, partially. I think you need |
Beta Was this translation helpful? Give feedback.
Yes, I believe. I don't think it'll work with
retain_graph=False
there because backward pass uses the graph to compute gradients wrt weights using theloss
returned fromtraining_step
.Yes, partially. I think you need
zero_grad()
aftergrads = torch.autograd.grad(...)
to avoid accumulating gradients wrt weights, but I'm not sure why you needeval()
andtrain()
.