Hi, let’s say I have two networks, “net1” and “net2” with “loss1” and “loss2” representing the loss function of “net1” and “net2”, and “optimizer1” and “optimizer2” are the optimizers of both networks.
My losses are computed as:
loss1 = criterion(outputs1, labels1)
loss2 = criterion(outputs2, labels2)
Now, I want to backprop the loss to net1 and net2. I use:
loss1.backward()
loss2.backward()
optimizer1.step()
optimizer2.step()
Is this a correct way? I am just confused about how is loss1 associated with net1 for computing gradients, and loss2 with net2? I know that the loss1 is computed from outputs of net1, but I want to ask how are the gradients in “loss1.backward()” computed from “net1”.
My goal: compute the loss1 from net1 and just backprop to net1, and same for net2.