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:
loss.backward(retain_graph=True)

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