Can I store multiple models in a normal Python dictionary?

I’m doing some testing combining the outputs of multiple small decoder models (each with their own parameters and such), and as can be expected, it’s a pain to initialize each model manually. Therefore, I was thinking of just looping this process and storing all the models in a dictionary like so:

num_small_decoders = 4
small_decoders = {}
for d in range(num_small_decoders):
    model_name = "model" + str(d)
    model = SmallDecoder()
    model = model.to(device)
    small_decoders[model_name] = model

Is this okay to do?

Yes, it is ok to do that. You could even do something like : small_decoders[model_name] = model.state_dict(), so that you can serialize small_decoders for later use (using JSON or Pickle).

1 Like