Closed chenshuang-zhang closed 2 years ago
Hey,
I'm not super sure how torch.autograd.grad(...)
works, but I would say that you can't backprop the model twice without retrain_graph=True
. Also, you don't need to zero_grad()
as there are no gradients in the model at this point. My suggestion would be to try and see what happens, as I'm not super sure what will happen.
Also, maybe this is interesting for you https://github.com/pytorch/functorch
Hey, I'm not super sure how
torch.autograd.grad(...)
works, but I would say that you can't backprop the model twice withoutretrain_graph=True
. Also, you don't need tozero_grad()
as there are no gradients in the model at this point. My suggestion would be to try and see what happens, as I'm not super sure what will happen.Also, maybe this is interesting for you https://github.com/pytorch/functorch
@vturrisi Thank you very much for your help! I will try and see what happens!
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
torch.autograd.grad
function: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
training_step
function in pytorch-lightning?My concern is:
torch.autograd.grad
function) influence the accuracy of model training since I add it in thetraining_step
function?model.zero_grad()
,model.eval()
andmodel.train()
when calculating the gradients on the input?Thank you very much!