Thank you for your reply. I have tried loss1.backward(retain_graph=True), but there is the same error.
If I use the loss = loss1 + loss2 to optimize the net1, net2, shared-net with one optimizer, will the gradient update of net1 be affected by the loss2 of the net2 part?