Saving and Restoring models in TensorFlow
You can save and restore the models and the variables in TensorFlow by one of the following two methods:
- A saver object created from the
tf.train.Saver
class - A
SavedModel
format based object created from thetf.saved_model_builder.SavedModelBuilder
class
Let us see both the methods in action.
Note
You can follow along with the code in the Jupyter notebook ch-11a_Saving_and_Restoring_TF_Models
.
Saving and restoring all graph variables with the saver class
We proceed as follows:
- To use the
saver
class, first an object of this class is created:
saver = tf.train.Saver()
- The simplest way to save all the variables in a graph is to call the
save()
method with the following two parameters: the session object and the path to the file on the disk where the variables will be saved:
with tf.Session() as tfs: ... saver.save(tfs,"saved-models/model.ckpt")
- To restore the variables, the
restore()
method is called:
with tf.Session() as tfs: saver.restore(tfs,"saved...