What’s the correct way of doing the following loop?
# assume gradient is enabled for all tensors
b1_list, b2_list = [], []
for i in range(n):
a1, a2 = some_function()
b1, b2 = some_neural_net(a1, a2)
b1_list.append(b1) # or b1_list.append(b1.clone()) ? or something else?
b2_list.append(b2) # or b2_list.append(b2.clone()) ? or something else?
b1_tensor = torch.stack(b1_list)
b2_tensor = torch.stack(b2_list)
It doesn’t look like b1 or b2 are reused or here, so simply holding on to the references without the clone seems fine, unless there is another part of the code that is being omitted. The autograd graph should be kept around until backward() is called without calling clone.