Hi,
You should not make b a Tensor. Because that means that when you are at i=1, b still refer to the computations done during i=0 and so calling backward will try to backward through the graph of i=0 again.
I would just use a list as b and you won’t have any problem.
If you need to ever compute gradients through each f_k, you need to pass retain_graph=True to the grad function.
If you want to use b as a single Tensor after the loop, just call torch.stack or torch.cat on the list b that will contain all your results !