Updating two models at the same time

Hi, I’m new to pytorch and had no luck following similar threads. I’m trying to jointly train two models in the same loop, and the model updates involve a different computation that takes in some combined loss from both model_a and model_b. However, I’m not sure how to go about training them at the same time. Any advice would be greatly appreciated!

loss_a = calc_loss_a(output_a, output_b)

loss_b = calc_loss_b(output_a, output_b)

The error I get from the above is

RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling .backward() or autograd.grad() the first time.

From the advice in some threads, I tried using retain_graph=True, but received this error:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 10]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

Using retain_graph=True is often used as a workaround which is unfortunately usually wrong.
I don’t know how the outputs are calculated, but I guess you might be running into this error, which tries to use stale forward activations while calculating the gradients.

Thank you for such a prompt and helpful reply! In that case, I’m still not very sure what the best practice in this scenario would be if I want the two losses to be back propagated independently.