fyjl:

model2 = autoencoder(encoder_only=True) # instantiate the second model whose forward return the code layer output
model2_output = model2(input2)
loss2 = loss(model2_output, model1_output)


Where is the decoder? If you don’t use the decoder in model2 to create output2 or for the creation of input2, loss2.backward() has no way of finding the decoder and updating any gradients.
optimizer2.step() will update weights for whatever Parameters you passed into the optimizer construction. If those Parameters had 0 or None gradients, then those weights will not be updated.
I don’t think .eval() guarantees gradient computation is avoided. It depends on how model1 is written.