I have two models defined as:
model1 = Net1()
model2 = Net2()
opt1 = Adam(model1.parameters(),…)
opt2 = Adam(model2.parameters(),…)
When training, the model2 uses the output of model1 as input, and the two models have a different loss. I tried to train them:
output1 = model1(data)
output2 = model2(output1)
loss1 = Loss(output1)
loss2 = Loss(output2, labels)
loss = loss1+loss2
In this way, I cannot train the model1 well. How can I train them separately? I only need loss1 to train model1. So I also tried:
But I still cannot get the same results when only training model1, i.e., I removed all other codes related to model2.
Thanks in advance.