How to load 2 pytorch models?

I tried to load two different models from different directories; however, it seems like pytorch would first search the file in the current directory to choose the loaded model’s structure. As I want to load two models, I cannot put two different file in the same directory. How can I handle this case?

You can save your models as and with model names as my_model1 and my_model2 respectively then in you can import these model as -
from model1 import my_model1
from model2 import my_model2
I guess this should work.

Or simply just save the state_dict as shown here, because this way you won’t save an instance of torch.nn.Module.

Have a look at this post for details when saving instances of nn.Module

That’s really helpful. Thanks!