Backpropagation for Two Different Neural Network Models with Combined Loss Functions

40 views Asked by At

Problem Description:

I am encountering an issue while training two neural network models, which I'll refer to as M1 and M2, using PyTorch. Each model takes different inputs (I1 for M1 and I2 for M2) and produces outputs (O1 for M1 and O2 for M2). My goal is to perform backpropagation for both models using their respective losses.

The loss functions for M1 and M2 are defined as follows:

  • For M1: L1 = some_func(O1, O2) + other_func(O1)
  • For M2: L2 = some_func(O1, O2) + other_func(O2)

This is my code snippet (Note the first term in both the losses are the same!):

loss_M2 = some_func(O1, O2) + other_func_1(O1)
                
                
self.optimizer_M2.zero_grad()
loss_M2.backward()
self.optimizer_M2.step()

loss_M1 = some_func(O1, O2) + other_func_2(O2)
                

self.optimizer_M1.zero_grad()
loss_M1.backward()
self.optimizer_M1.step()

However, when I attempt to perform backpropagation for both models, I encounter the following error:

RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.

Additionally, when I set retain_graph=True for the first model's backward pass, I encounter the following error:


RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [64, 1]], which is output 0 of AsStridedBackward0, is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

Question:

How can I resolve these errors and successfully perform backpropagation for both models M1 and M2 with their respective losses?

0

There are 0 answers