Part I of this series, we have learned different approaches to save architecture only or save weights only or entire Keras model. However, we had covered only small part of saving entire model. In this article we will focus only on saving entire model when we have a custom metric/loss or a custom layer.

Outline of this article is as follows

  • Overview of saving and loading entire model
  • Saving entire model before or after training
  • Saving entire model during training
  • Saving entire model with a custom metric/loss
  • Saving entire model with custom layer

1. Overview of saving and loading entire model

As mentioned in Part I of this series, entire Keras model consist(the following is from TensorFlow website)

An architecture, or configuration, which specifies what layers the model contain, and how they’re connected
set of weights values (the “state of the model”)
An optimizer****state_ (defined by compiling the model)_
A set of losses and metrics (defined by compiling the model)

Entire Keras model can be saved to a disk in two formats (i) TensorFlow SavedModel ( tf ) format, and (ii) H5 format.

Entire Keras model can be saved either during training or before/after training the model. We will see see more details and examples in the following sections.

How to save entire model?

Entire Keras model can be saved using Saved model API by model.save(‘MyModel’,save_format='tf') or model.save('MyModel_h5',save_format='h5') . The tf format is default which means if you don’t provide save_format argument, then the model is saved in TensorFlow SavedModel tf format.

Why do we need to save entire model?

  • Sharing entire model is simple and error prone. You can share it with your team or client so that they can reproduce exactly same result as you are
  • As saving entire model includes optimizer state, you can restart the training where you left off.
  • Entire model can be easily converted to TFLite format so that you can deploy the model on mobile devices
  • Entire model can be converted to TensorFlow.js Layers format, which can be loaded directly into TensorFlow.js for inference or for further training.

#keras #machine-learning #tensorflow #deep-learning

How to Save and Load of Keras Sequential and Functional Models - Part II
20.50 GEEK