Skip to content
Discussion options

You must be logged in to vote
  1. Will this gradient calculation (torch.autograd.grad function) influence the accuracy of model training since I add it in the training_step function?

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 the loss returned from training_step.

  1. Do I need to set model.zero_grad(), model.eval() and model.train() when calculating the gradients on the input?

Yes, partially. I think you need zero_grad() after grads = torch.autograd.grad(...) to avoid accumulating gradients wrt weights, but I'm not sure why you need eval() and train().

Replies: 1 comment 14 replies

Comment options

You must be logged in to vote
14 replies
@chenshuang-zhang
Comment options

@tchaton
Comment options

@chenshuang-zhang
Comment options

@tchaton
Comment options

@chenshuang-zhang
Comment options

Answer selected by akihironitta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment