How to save a list of pytorch models
This is a newbie question. I have trained 8 pytorch convolutional models and put them in a list called models. I can use them for prediction so they are working. I would like to save them. I can't even work out how to save one however. I tried:

torch.save(models[0].save_dict(), &quot;test.pth&quot;)

but this gives me:

ModuleAttributeError: 'Net' object has no attribute 'save_dict'

type(models[0]) gives me __main__.Net
How can I save this list of models?

To save model weights for one model (example the 1st) you should do
torch.save(models[0].state_dict(), &quot;test0.pth&quot;)

And to load it back you should instantiate a new object and load weights
model0 = Net() # or however you instantiate the model in the first place 

model0.load_state_dict(torch.load(&quot;test0.pth&quot;))

You can save your other models this way.
And don't forget to call model.eval() if you are using it for inference
If you want to save all models weights using a for loop
for i, m in enumerate(models):
    torch.save(m.state_dict(), f&quot;test{i}.pth&quot;)

And to load them all, supposing your model instantiation is something like follow :
models = [Net() for i in range(8)]  # Note that you should adapt this to whatever method you use to create your models in the first place

for i, m in enumerate(models):
    m.load_state_dict(torch.load(f&quot;test{i}.pth&quot;))
    # if you are using it for inference uncomment the line below
    # m.eval()

and that's it your variable models is ready for use
Last but not least you can save the whole at once (but discouraged approach)
torch.save(models, &quot;all_models.pth&quot;)

# to load back
models = torch.load(&quot;all_models.pth&quot;)

However, This will cause error if you try to load models for example from another python script where the definition of your class Net is not available
