Store weights of multiple Keras models in one variable/array

Viewed 24

I am new to Deep Learning.

I have 3 keras model. I am trying something like this:

weights = [] # or some data type

for i in range(3):
    weights[i] = model[i].get_weights()

I want to store the weights of all models and then use model.set_weights() at some later point in time

1 Answers

You were on the right track:

# storing the weights for your three models
weights = []
# models is the list where you have previously added your three trained models
for model in models:
   weights.append(model.get_weights())

# later on when you want to restore the weights
for i, model in enumerate(models):
   model.set_weights(weights[i])

To clarify even more the code: model.get_weights() gets all the weights of your model. The function model.set_weights() restores all of them. Have a look at the documentation to learn more.

Related