Hello. Thank you for answering.
The code is functional if I remove
 target_grad = torch.autograd.grad(recon_loss, wrt, create_graph = True, retain_graph = True)[0]

What I seem to understand is that this computed the gradient of recon_loss with respect to the tensor wrt. It retains also the graph. Why does this interfere later in the code when I create my loss function l_tot and apply backward() to it, l.backward()?