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.

Start for Free
First 2 tasks free. Then, decide to pay $9.99 for the rest
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 have been writing code since 1993, when I was 11, and my first passion project started with a database management software that I wrote for a local hospital. More recently, I wrote an award winning education Chatbot for a multi-billion-revenue company. I solved a recurrent problem for my client where they wanted to make basic cyber safety and privacy education accessible for their users. This bot enabled my client to reach out to their customers with personalised and real-time education. In the last one year, I’ve continued my interest in this field by constantly learning and growing in Machine Learning, NLP and Deep Learning. I'm very excited to share my variety of experience and learnings with you with the help of Rhyme.com.

Frequently Asked Questions

In Rhyme, all sessions 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.

First 2 tasks free. Then, decide to pay $9.99 for the rest