How to load a pretrained model into another slitly diffrent model?

supose we have a model “saved” that has been trained and saved, and we also have another model “changed” that defined completely the same with “saved” but added some extra modules, so, how can we load the “saved” model’s weights into the “changed” model.

For example:

class A(nn.Module):
def init():
self.stack = nn.sequantial(…Linear and ReLU )
def forward(x):
return self.stack(x)

And
class B(nn.Module):
self.stack = nn.sequantial(…Linear and ReLU )
self.Linear = nn.Linear
def forward(x):
x = self.stack(x)
return self.Linear(x)

So, how can we load a pretrained model defined by class A into class B?

The easy but potentially dangerous method would be to use load_state_dict(state_dict, strict=False) and let PyTorch only load the matching modules. The save approach would be to manipulate the stored state_dict keys to match the new naming and load explicitly the desired data via e.g. modelB.stack.load_state_dict().