TensorFlow (Beginner): Save and Restore Models

In this course, you will learn how to save and restore models with TensorFlow. You can save models at any stage during training. While training your models, you can save the progress of your model during or after the training. The saved models can then be shared with other people who can then try to recreate your work. Sometimes you might have a very large model which might take a long time to train. By saving the models during training, one can pause the training and then resume at a later time. This is useful if you training is going to take a long time.

TensorFlow (Beginner): Save and Restore Models

Duration (mins)


5.0 / 5


Task List

We will cover the following tasks in 44 minutes:


We will understand the Rhyme interface and our learning environment. You will get a virtual machine, you will need Jupyter Notebook and TensorFlow for this course and both of these are already installed on your virtual machine. Jupyter Notebooks are very popular with Data Science and Machine Learning Engineers as one can write code in cells and use other cells for documentation.

Importing TensorFlow and the Dataset

In this course, we are going to use TensorFlow’s implementation of Keras. Keras is a high level API to build and train models. We are going to use the MNIST dataset in this course. This dataset has images of handwritten digits. Since we are only interested in understanding saving and reloading models in this course, we are going to use a very small subset of the dataset so that the training process is fast. So, we are going to use 1000 training examples and 1000 test examples.

Building a Model

Instead of writing code to just build a single model, let’s create a method that returns a model. This way, we can reuse the code to make as many instances of the model as we want. Our model has three layers. We compile the model with sparse categorical crossentropy loss and adam optimizer. In this example, we will use accuracy as our only training metric.

Saving Checkpoints

We can save checkpoints during and after training models. This way, you can pick up training where you left off - in case the training was interrupted or you can use a trained model without having to retrain it.

We will use a callback from Keras called ModelCheckpoint to perform this task. We will need to configure this callback and then use it while training our model. Notice that we store only the trained weights in the checkpoint files. We have not stored the network architecture. So, if we want to restore any of the saved checkpoints, we will have to do that on a model with the same architecture that we used while training and saving the checkpoints.

Manually Saving Weights

We will create a new model and train it. Then, we will manually save the weights of this trained model. We will create a new instance of the untrained model. Finally, we will load the manually saved weights to this new, untrained model.

Saving Entire Models

We can save entire models to a file with the weight values, the model’s configuration, and the optimizer’s configuration. This allows us to checkpoint a model and resume training later - from the exact same state - even without having access to the original code. Keras provides a basic save format using the HDF5 standard.

Watch Preview

Preview the instructions that you will follow along in a hands-on session in your browser.

Amit Yadav

About the Host (Amit Yadav)

I am a Software Engineer with many years of experience in writing commercial software. My current areas of interest include computer vision and sequence modelling for automated signal processing using deep learning as well as developing chatbots.

Frequently Asked Questions

In Rhyme, all projects are completely hands-on. You don't just passively watch someone else. You use the software directly while following the host's (Amit Yadav) instructions. Using the software is the only way to achieve mastery. With the "Live Guide" option, you can ask for help and get immediate response.
Nothing! Just join through your web browser. Your host (Amit Yadav) has already installed all required software and configured all data.
You can go to https://rhyme.com/for-companies, sign up for free, and follow this visual guide How to use Rhyme to create your own sessions. If you have custom needs or company-specific environment, please email us at help@rhyme.com
Absolutely. We offer Rhyme for workgroups as well larger departments and companies. Universities, academies, and bootcamps can also buy Rhyme for their settings. You can select sessions and trainings that are mission critical for you and, as well, author your own that reflect your own needs and tech environments. Please email us at help@rhyme.com
Please email us at help@rhyme.com and we'll respond to you within one business day.

No sessions available