-
Models created using the sequential or functional API can be saved
using the
save()
method.
model = keras.layers.Sequential([...])
model.compile(...)
model.fit(...)
model.save("my_model.h5")
-
Keras uses the HDF5 format to save:
-
model's architecture (all layers and their hyperparameters)
-
values of all parameters (every layer's weights and biases)
- the optimizer and its hyperparameters and state
- The model can be loaded as follows:
model = keras.models.load_model("my_model.h5")
-
When using model subclassing, this approach can not be used. In this
case, the model parameters can be saved and restored using the
save_weights()
and load_weights()
methods.
-
When training for long hours, it is a good practice to save model at
regular intervals during training instead of just saving at the end
of training. This can be done using callbacks.