In TensorFlow and Keas, there are several ways to save and load a deep learning model. When you have too many options, sometimes it will be confusing to know which option to select for saving a model. Moreover, if you have a custom metric or a custom layer then the complexity increases even more.
The main goal of this article is to explain different approaches for saving and loading a Keras model. If you are a seasoned machine learning practitioner, then most probably you know which option to select. You could skim through this article to see if there is any value you are going to get by reading this article.
Outline of this article is as follows
Before going into details, let us see what a Keras model consist (the following is from TensorFlow website)
Depending on your requirements, you may want to save the architecture of the model only and share it with someone (team member or client). In another case, you may want to save only weights and resume the training later from where you left off. Finally, you may want to save the entire model (architecture, weights, optimizer state, and training configuration) and share it with others. Good thing is that Keras allows us to do what we wish.
The above flowchart is created to explain different options for saving and loading (i) model architecture only, (ii) model weights only, and (iii) entire model (architecture, weights, optimizer state, and training configuration).
Scope of this article is to describe option (i) and (ii) with more details, and describe option (3) with a simple example. As saving a complex Keras model with custom metrics and custom layers is not simple, we will deal with it in another article (part II of this series).
Model architecture can be saved either in json
or yaml
format whereas model weights or entire model can be saved in tf
format or h5
format. In the following sections, more details on these options are provided with code examples.
#machine-learning #keras #tensorflow #deep-learning