'Save multiple Keras Models and load the best one based on accuracy

I need to develop a function to save multiple models from dictionary and load the best one which gave highest accuracy on test set. Thanks in advance!

I have a model like this:

from keras.models import Sequential
from keras.layers import Dense

def create_custom_model(input_dim, output_dim, nodes, n=11, name='model'):
    def create_model():
        # Create model
        #model = Sequential(name=name)
        for i in range(n):
            #nodes=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
            #for node in nodes:
                model = Sequential(name=name)
                model.add(Dense(n, input_dim=input_dim, activation='relu'))
                model.add(Dense(output_dim, activation='softmax'))

        # Compile model
        model.compile(loss='categorical_crossentropy', 
                      optimizer='adam', 
                      metrics=['accuracy'])
        return model
    return create_model

models = [create_custom_model(n_features, n_classes, 8, i, 'model_{}'.format(i)) 
          for i in range(1, 11)]

for create_model in models:
    create_model().summary()
history_dict = {}

# TensorBoard Callback
cb = TensorBoard()

for create_model in models:
    model = create_model()
    print('Model name:', model.name)
    history_callback = model.fit(X_train, Y_train,
                                 batch_size=5,
                                 epochs=50,
                                 verbose=0,
                                 validation_data=(X_test, Y_test),
                                 callbacks=[early_stopping])
    score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
   

    history_dict[model.name] = [history_callback, model] 

Output:

Model name: model_1
Test loss: 0.29368123412132263
Test accuracy: 0.9066666960716248
Model name: model_2
Test loss: 0.1819317787885666
Test accuracy: 0.9466666579246521
Model name: model_3
Test loss: 0.11470139771699905
Test accuracy: 0.9599999785423279
.....
.....

How can I save all model from history_dict and load the best one which best accuracy?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source