How can we use autograd for sequence optimization (in a for loop)?

I want to optimize a sequence in a for loop using Pytorch Autograd.

for i in range(10):
     x = f(x,z[i])

Say the sequence length is 10. I want to optimize x as well as z(z is a tensor array). Note the x will be updated in the loop.

To run this, I have to open:


Is there a better way to do so (To make it run faster)?

I’m not sure why you need to use detect_anomaly, as it’s a debugging tool and is expected to slow down the code. Could you explain the reason a bit more?

Sorry, my mistake.
I am using


Using retain_graph would only be necessary if you need to keep the intermediate tensors alive after a backward operation. I’m still unsure why it’s needed in your case, so would need more information about the actual use case.