I did try that but the weights of the 2nd model are not updated, while the weights of the first model are updated.
criterion = nn.CrossEntropyLoss()
optimizer.zero_grad()
output1 = model1(x)
output2 = model2(output1)
loss =  criterion( output2 , labels )
a = copy.deepcopy(model2.linear1.weight.data)
loss.backward()
optimizer.step()
b = copy.deepcopy(model2.linear1.weight.data)
print(torch.equal(a, b))
The above code prints “True” when run.
However if you check the “model1” instead of the “model2” weights it prints “False”.
Can you please help me find out where I am going wrong. Thanks in advance.