I have a similar problem to this issue 7 on github however no solution is proposed.
The proposed example code :
slize = [1, 2, 3, 4]
x = torch.randn(10, requires_grad=True)
y = x[slize]
y.sum().backward()
# breaks second time calling backward
y.sum().backward() # rasises RuntimeErroe


In my case large tensor and I want to iterate gradient descent steps on mini batches.
example:
old_log_probs = ... # size [8000, 1]
for epoch in range(epoch):
...
   for batch_idx in rollout.shuffle_index(batch_size=8):
         loss = ppo_loss(      
                        new_log_probs, # size [8, 1]
                        old_log_probs[batch_index],  # size [8, 1]
                        advantages[batch_index],  # size [8, 1]
                        clip=0.2
                    )

Because when we get the subtensor with a list it is a copy and not a view.
Is it possible to get a subtensor with random indexes as a view?