Saving/Loading Models

Say we had some simple data

import numpy as np

X = np.random.rand(1000, 100, 100)
y = np.random.rand(1000).reshape(-1, 1)

X.shape, y.shape
((1000, 100, 100), (1000, 1))

And we trained a model on it

from keras.models import Sequential
from keras.layers import Dense, Input, Flatten

model = Sequential()

model.add(Dense(4, input_shape=(100, 100), activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Flatten())
model.add(Dense(1, activation='relu'))

model.compile(optimizer='adam', loss='mean_squared_error')

model.fit(X, y, verbose=0)
<keras.callbacks.History at 0x1c6247f0>

How do we go about saving it? Furthermore, when we’re done, how would we go about loading it later?

Easy Way: .hdf5

The path of least resistance would be to use the save() method in the Model object

model.save('test.hdf5')

Then we can load the model using models.load_model() function in keras

from keras.models import load_model
loaded_model = load_model('test.hdf5')
loaded_model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_4 (Dense)              (None, 100, 4)            404       
_________________________________________________________________
dense_5 (Dense)              (None, 100, 4)            20        
_________________________________________________________________
flatten_2 (Flatten)          (None, 400)               0         
_________________________________________________________________
dense_6 (Dense)              (None, 1)                 401       
=================================================================
Total params: 825
Trainable params: 825
Non-trainable params: 0
_________________________________________________________________

Trickier Way: .json and .h5

Alternatively, you can serialize the model architecture separately from the values for the weights using to_json() and save_weights() in the model object

with open('test.json', 'w') as f:
    model_json = model.to_json()
    f.write(model_json)
model.save_weights('test.h5')

Subsequent loading involves first loading the architecture

from keras.models import model_from_json

with open('test.json', 'r') as f:
    model_json = f.read()
    second_loaded_model = model_from_json(model_json)

Then loading the weights using the load_weights() method of the model object

second_loaded_model.load_weights('test.h5')
second_loaded_model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_4 (Dense)              (None, 100, 4)            404       
_________________________________________________________________
dense_5 (Dense)              (None, 100, 4)            20        
_________________________________________________________________
flatten_2 (Flatten)          (None, 400)               0         
_________________________________________________________________
dense_6 (Dense)              (None, 1)                 401       
=================================================================
Total params: 825
Trainable params: 825
Non-trainable params: 0
_________________________________________________________________