Yes you are right, that was a typo. I’ve edited the OP.
I apology if not clear; what I mean is that though model2 is identical to model1, I only care about its weights of the encoder part, and I use the output of the encoder part for loss computation. My question is do loss2.backward() propagate to the whole network (encoder + decoder) of model2, or only the partial network of interest which is the encoder part? Likewise, do optimizer2.step() update weights for the whole network or only the encoder?
I added model1.eval() to the snippet, which I believe avoids gradient computation for model1, and detach() is not necessary. Correct me if wrong, please.